#include "dzialka.h" #include "message.h" #include <iostream> #include <stack> #include <utility> int width; int height; int my_min_y; int my_max_y; int my_height; int ** t; bool * fully_computed; int * incoming; std::stack<std::pair<int, int> > S; int number_of_nodes; void init() { t = new int*[width]; fully_computed = new bool[width]; incoming = new int[width]; for (int i = 0; i < width; i++) { t[i] = new int[my_height]; fully_computed[i] = false; incoming[i] = 0; } for (int i = 0; i < width; i++) { t[i][0] = 0; } for (int i = 0; i < my_height; i++) { t[0][i] = 0; } } void compute_t() { for (int i = 0; i < width; i++) { for (int j = 0; j < my_height; j++) { if (IsUsableCell(i,j + my_min_y)) { if (j == 0) { t[i][j] = 1; } else { t[i][j] = t[i][j-1] + 1; } } else { t[i][j] = 0; fully_computed[i] = true; } } } } void compute_t_again() { if (MyNodeId() == 0) { return; } int end = my_height - 1; if (MyNodeId() == number_of_nodes - 1) { end = my_height; } for (int i = 0; i < width; i++) { for (int j = 0; j < end; j++) { if (t[i][j] == 0) { break; } t[i][j] += incoming[i]; } } } int min(int a, int b) { return a < b ? a : b; } long long compute() { long long result = 0; long long part = 0; for (int j = 0; j < my_height; j++) { S = std::stack<std::pair<int, int> >(); part = 0; for (int i = 0; i < width; i++) { if (t[i][j] == 0) { S = std::stack<std::pair<int, int> >(); part = 0; } else if (S.empty()) { S.push(std::make_pair(t[i][j], 1)); part = t[i][j]; result += part; } else { int count = 1; int h = t[i][j]; while (!S.empty() && S.top().first >= h) { count += S.top().second; part -= ((long long)S.top().first) * S.top().second; S.pop(); } S.push(std::make_pair(h, count)); part += ((long long)h) * count; result += part; } } //printf("ID=%d row=%d result=%lld\n", MyNodeId(), j + my_min_y, result); } return result; } void compute_node_stats() { int h = height / number_of_nodes; if (height % number_of_nodes != 0) { h++; } my_min_y = h * MyNodeId(); my_max_y = min(height, h * (MyNodeId() + 1)); my_height = my_max_y - my_min_y; } int message_size = 1000; void send_to(int target) { for (int j = 0; j < width; j+= message_size) { for (int i = j; i < j + message_size && i < width; i++) { PutInt(target, t[i][my_height-1]); } Send(target); } } void receive(int from) { for (int j = 0; j < width; j += message_size) { Receive(from); for (int i = j; i < j + message_size && i < width; i++) { incoming[i] = GetInt(from); } } } void compute_last_row() { for (int i = 0; i < width; i++) { if (!fully_computed[i]) { t[i][my_height-1] += incoming[i]; } } } /* void print_my_array() { printf("I'm %d\n", MyNodeId()); for (int i = 0; i < my_height; i++) { for (int j = 0; j < width; j++) { printf("%d ", t[j][i]); } printf("\n"); } }*/ void send_heights() { if (MyNodeId() == 0) { send_to(1); } else { for (int i = 1; i < number_of_nodes; i++) { if (MyNodeId() == i) { receive(i - 1); if (MyNodeId() < number_of_nodes - 1) { compute_last_row(); send_to(i + 1); } } } } } void gather_results(long long my_result) { //printf("I'm %d and my result is %lld\n", MyNodeId(), my_result); if (MyNodeId() == 0) { unsigned long long res = my_result; for (int i = 1; i < number_of_nodes; i++) { Receive(i); res += GetLL(i); } std::cout << res << "\n"; } else { PutLL(0, my_result); Send(0); } } int main() { width = GetFieldHeight(); height = GetFieldWidth(); number_of_nodes = min(50, NumberOfNodes()); if (MyNodeId() >= number_of_nodes) { return 0; } if (height < 200 || number_of_nodes == 1) { if (MyNodeId() != 0) { return 0; } my_min_y = 0; my_max_y = height; my_height = height; init(); compute_t(); long long result = compute(); std::cout << result << "\n"; //print_my_array(); } else { compute_node_stats(); init(); compute_t(); send_heights(); compute_t_again(); long long result = compute(); gather_results(result); return 0; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | #include "dzialka.h" #include "message.h" #include <iostream> #include <stack> #include <utility> int width; int height; int my_min_y; int my_max_y; int my_height; int ** t; bool * fully_computed; int * incoming; std::stack<std::pair<int, int> > S; int number_of_nodes; void init() { t = new int*[width]; fully_computed = new bool[width]; incoming = new int[width]; for (int i = 0; i < width; i++) { t[i] = new int[my_height]; fully_computed[i] = false; incoming[i] = 0; } for (int i = 0; i < width; i++) { t[i][0] = 0; } for (int i = 0; i < my_height; i++) { t[0][i] = 0; } } void compute_t() { for (int i = 0; i < width; i++) { for (int j = 0; j < my_height; j++) { if (IsUsableCell(i,j + my_min_y)) { if (j == 0) { t[i][j] = 1; } else { t[i][j] = t[i][j-1] + 1; } } else { t[i][j] = 0; fully_computed[i] = true; } } } } void compute_t_again() { if (MyNodeId() == 0) { return; } int end = my_height - 1; if (MyNodeId() == number_of_nodes - 1) { end = my_height; } for (int i = 0; i < width; i++) { for (int j = 0; j < end; j++) { if (t[i][j] == 0) { break; } t[i][j] += incoming[i]; } } } int min(int a, int b) { return a < b ? a : b; } long long compute() { long long result = 0; long long part = 0; for (int j = 0; j < my_height; j++) { S = std::stack<std::pair<int, int> >(); part = 0; for (int i = 0; i < width; i++) { if (t[i][j] == 0) { S = std::stack<std::pair<int, int> >(); part = 0; } else if (S.empty()) { S.push(std::make_pair(t[i][j], 1)); part = t[i][j]; result += part; } else { int count = 1; int h = t[i][j]; while (!S.empty() && S.top().first >= h) { count += S.top().second; part -= ((long long)S.top().first) * S.top().second; S.pop(); } S.push(std::make_pair(h, count)); part += ((long long)h) * count; result += part; } } //printf("ID=%d row=%d result=%lld\n", MyNodeId(), j + my_min_y, result); } return result; } void compute_node_stats() { int h = height / number_of_nodes; if (height % number_of_nodes != 0) { h++; } my_min_y = h * MyNodeId(); my_max_y = min(height, h * (MyNodeId() + 1)); my_height = my_max_y - my_min_y; } int message_size = 1000; void send_to(int target) { for (int j = 0; j < width; j+= message_size) { for (int i = j; i < j + message_size && i < width; i++) { PutInt(target, t[i][my_height-1]); } Send(target); } } void receive(int from) { for (int j = 0; j < width; j += message_size) { Receive(from); for (int i = j; i < j + message_size && i < width; i++) { incoming[i] = GetInt(from); } } } void compute_last_row() { for (int i = 0; i < width; i++) { if (!fully_computed[i]) { t[i][my_height-1] += incoming[i]; } } } /* void print_my_array() { printf("I'm %d\n", MyNodeId()); for (int i = 0; i < my_height; i++) { for (int j = 0; j < width; j++) { printf("%d ", t[j][i]); } printf("\n"); } }*/ void send_heights() { if (MyNodeId() == 0) { send_to(1); } else { for (int i = 1; i < number_of_nodes; i++) { if (MyNodeId() == i) { receive(i - 1); if (MyNodeId() < number_of_nodes - 1) { compute_last_row(); send_to(i + 1); } } } } } void gather_results(long long my_result) { //printf("I'm %d and my result is %lld\n", MyNodeId(), my_result); if (MyNodeId() == 0) { unsigned long long res = my_result; for (int i = 1; i < number_of_nodes; i++) { Receive(i); res += GetLL(i); } std::cout << res << "\n"; } else { PutLL(0, my_result); Send(0); } } int main() { width = GetFieldHeight(); height = GetFieldWidth(); number_of_nodes = min(50, NumberOfNodes()); if (MyNodeId() >= number_of_nodes) { return 0; } if (height < 200 || number_of_nodes == 1) { if (MyNodeId() != 0) { return 0; } my_min_y = 0; my_max_y = height; my_height = height; init(); compute_t(); long long result = compute(); std::cout << result << "\n"; //print_my_array(); } else { compute_node_stats(); init(); compute_t(); send_heights(); compute_t_again(); long long result = compute(); gather_results(result); return 0; } } |