#include <cstdio> #include <vector> #include <algorithm> #include "dzialka.h" #include "message.h" long long int merge(int dist1, int dist2, const std::vector<int>& v1_1, const std::vector<int>& v1_2, std::vector<int>& v1, const std::vector<int>& v2_1, const std::vector<int>& v2_2, std::vector<int>& v2) { int len = v1_1.size(); for (int i = 0; i < len; ++i) { v1.push_back((v1_1[i] == dist1) ? (v1_1[i] + v1_2[i]) : v1_1[i]); v2.push_back((v2_2[i] == dist2) ? (v2_2[i] + v2_1[i]) : v2_2[i]); } long long int sum = 0; for (int j = 0; j < len; ++j) { long long min1 = v1_2[j]; long long min2 = v2_1[j]; for (int i = j; i < len; ++i) { if (min1 > v1_2[i]) min1 = v1_2[i]; if (min2 > v2_1[i]) min2 = v2_1[i]; sum += min1 * min2; if (min1 == 0 || min2 == 0) { break; } } } return sum; } long long int calculate(int y1, int x1, int y2, int x2, std::vector<int>& rb, std::vector<int>& rt, std::vector<int>& rl, std::vector<int>& rr) { long long int sum = 0; if ((y2 - y1 > 1) && (x2 - x1 > 1)) { std::vector<int> resultB_1; std::vector<int> resultT_1; std::vector<int> resultL_1; std::vector<int> resultR_1; std::vector<int> resultB_2; std::vector<int> resultT_2; std::vector<int> resultL_2; std::vector<int> resultR_2; if (y2 - y1 > x2 - x1) { sum += calculate(y1, x1, y1 + (y2 - y1) / 2, x2, resultB_1, resultT_1, resultL_1, resultR_1); sum += calculate(y1 + (y2 - y1) / 2, x1, y2, x2, resultB_2, resultT_2, resultL_2, resultR_2); rr.insert(rr.end(), resultR_1.begin(), resultR_1.end()); rr.insert(rr.end(), resultR_2.begin(), resultR_2.end()); rl.insert(rl.end(), resultL_1.begin(), resultL_1.end()); rl.insert(rl.end(), resultL_2.begin(), resultL_2.end()); sum += merge((y2 - y1) / 2, y2 - y1 - (y2 - y1) / 2, resultB_1, resultB_2, rb, resultT_1, resultT_2, rt); } else { sum += calculate(y1, x1, y2, x1 + (x2 - x1) / 2, resultB_1, resultT_1, resultL_1, resultR_1); sum += calculate(y1, x1 + (x2 - x1) / 2, y2, x2, resultB_2, resultT_2, resultL_2, resultR_2); rt.insert(rt.end(), resultT_1.begin(), resultT_1.end()); rt.insert(rt.end(), resultT_2.begin(), resultT_2.end()); rb.insert(rb.end(), resultB_1.begin(), resultB_1.end()); rb.insert(rb.end(), resultB_2.begin(), resultB_2.end()); sum += merge((x2 - x1) / 2, x2 - x1 - (x2 - x1) / 2, resultL_1, resultL_2, rl, resultR_1, resultR_2, rr); } } else if (y2 == y1 || x2 == x1) { } else if (y2 - y1 == 1) { int firstZero = -1; int lastZero = -1; long long int cnt = 0; for (int i = x1; i < x2; ++i) { rt.push_back(IsUsableCell(y1, i)); if (rt.back() == 0) { if (firstZero == -1) { firstZero = rt.size() - 1; } lastZero = rt.size() - 1; sum += cnt * (cnt + 1) / 2; cnt = 0; } else { cnt++; } } sum += cnt * (cnt + 1) / 2; rb.assign(rt.begin(), rt.end()); rl.push_back(firstZero != -1 ? firstZero : rt.size()); rr.push_back(lastZero != -1 ? (rt.size() - lastZero - 1) : rt.size()); } else /* x2 - x1 == 1 */ { int firstZero = -1; int lastZero = -1; long long int cnt = 0; for (int i = y1; i < y2; ++i) { rl.push_back(IsUsableCell(i, x1)); if (rl.back() == 0) { if (firstZero == -1) { firstZero = rl.size() - 1; } lastZero = rl.size() - 1; sum += cnt * (cnt + 1) / 2; cnt = 0; } else { cnt++; } } sum += cnt * (cnt + 1) / 2; rr.assign(rl.begin(), rl.end()); rb.push_back(firstZero != -1 ? firstZero : rl.size()); rt.push_back(lastZero != -1 ? (rl.size() - lastZero - 1) : rl.size()); } return sum; } bool prepareRange(int& x1, int& y1, int& x2, int& y2) { int id = MyNodeId(); int totH = GetFieldHeight(); int totW = GetFieldWidth(); int rangeH = (totH + 9) / 10; int rangeW = (totW + 9) / 10; x1 = rangeW * (id % 10); y1 = rangeH * (id / 10); x2 = std::max(std::min(x1 + rangeW, totW), x1); y2 = std::max(std::min(y1 + rangeH, totH), y1); return (x1 < x2 && y1 < y2); } const int MAX_INTEGERS_IN_MESSAGE = 0x4000; long long int receiveResult(int id, std::vector<int>& rb, std::vector<int>& rt, std::vector<int>& rl, std::vector<int>& rr) { Receive(id); long long int total = GetLL(id); int rbSize = GetInt(id); int rtSize = GetInt(id); int rlSize = GetInt(id); int rrSize = GetInt(id); for (int i = 0; i < rbSize; ++i) { if (i % MAX_INTEGERS_IN_MESSAGE == 0) { Receive(id); } rb.push_back(GetInt(id)); } for (int i = 0; i < rtSize; ++i) { if (i % MAX_INTEGERS_IN_MESSAGE == 0) { Receive(id); } rt.push_back(GetInt(id)); } for (int i = 0; i < rlSize; ++i) { if (i % MAX_INTEGERS_IN_MESSAGE == 0) { Receive(id); } rl.push_back(GetInt(id)); } for (int i = 0; i < rrSize; ++i) { if (i % MAX_INTEGERS_IN_MESSAGE == 0) { Receive(id); } rr.push_back(GetInt(id)); } return total; } void sendResult(int id, long long int total, std::vector<int>& rb, std::vector<int>& rt, std::vector<int>& rl, std::vector<int>& rr) { PutLL(id, total); PutInt(id, rb.size()); PutInt(id, rt.size()); PutInt(id, rl.size()); PutInt(id, rr.size()); Send(id); for (int i = 0; i < rb.size(); ++i) { if (i != 0 && (i % MAX_INTEGERS_IN_MESSAGE == 0)) { Send(id); } PutInt(id, rb[i]); } Send(id); for (int i = 0; i < rt.size(); ++i) { if (i != 0 && (i % MAX_INTEGERS_IN_MESSAGE == 0)) { Send(id); } PutInt(id, rt[i]); } Send(id); for (int i = 0; i < rl.size(); ++i) { if (i != 0 && (i % MAX_INTEGERS_IN_MESSAGE == 0)) { Send(id); } PutInt(id, rl[i]); } Send(id); for (int i = 0; i < rr.size(); ++i) { if (i != 0 && (i % MAX_INTEGERS_IN_MESSAGE == 0)) { Send(id); } PutInt(id, rr[i]); } Send(id); } bool isIdActive(int id) { int w = GetFieldWidth(); int h = GetFieldHeight(); return ((id % 10 < w) && (id / 10 < h)); } long long int receiveAndMerge(int id, std::vector<int>& rb, std::vector<int>& rt, std::vector<int>& rl, std::vector<int>& rr, bool horisontal) { if (!isIdActive(id)) { return 0; } std::vector<int> resultB_1; std::vector<int> resultT_1; std::vector<int> resultL_1; std::vector<int> resultR_1; std::swap(resultB_1, rb); std::swap(resultT_1, rt); std::swap(resultR_1, rr); std::swap(resultL_1, rl); std::vector<int> resultB_2; std::vector<int> resultT_2; std::vector<int> resultL_2; std::vector<int> resultR_2; long long int total = receiveResult(id, resultB_2, resultT_2, resultL_2, resultR_2); if (!horisontal) { rr.insert(rr.end(), resultR_1.begin(), resultR_1.end()); rr.insert(rr.end(), resultR_2.begin(), resultR_2.end()); rl.insert(rl.end(), resultL_1.begin(), resultL_1.end()); rl.insert(rl.end(), resultL_2.begin(), resultL_2.end()); total += merge(resultR_1.size(), resultR_2.size(), resultB_1, resultB_2, rb, resultT_1, resultT_2, rt); } else { rt.insert(rt.end(), resultT_1.begin(), resultT_1.end()); rt.insert(rt.end(), resultT_2.begin(), resultT_2.end()); rb.insert(rb.end(), resultB_1.begin(), resultB_1.end()); rb.insert(rb.end(), resultB_2.begin(), resultB_2.end()); total += merge(resultB_1.size(), resultB_2.size(), resultL_1, resultL_2, rl, resultR_1, resultR_2, rr); } return total; } void collectAndSend(std::vector<int>& rb, std::vector<int>& rt, std::vector<int>& rl, std::vector<int>& rr, long long int& total) { int id = MyNodeId(); int mmm = 2; for (int mmm = 2; mmm <= 16; mmm *= 2) { if ((id % 10) % mmm == 0) { if ((id % 10) + (mmm / 2) < 10) { total += receiveAndMerge(id + mmm / 2, rb, rt, rl, rr, true); } } else { sendResult(id - (id % 10) % mmm, total, rb, rt, rl, rr); return; } if ((id/10) % mmm == 0) { if ((id / 10) + (mmm / 2) < 10) { total += receiveAndMerge(id + 10*(mmm/2), rb, rt, rl, rr, false); } } else { sendResult(id - 10 * ((id / 10) % mmm), total, rb, rt, rl, rr); return; } } } int main() { int x1, y1, x2, y2; int id = MyNodeId(); if (prepareRange(x1, y1, x2, y2)) { std::vector<int> resultB; std::vector<int> resultT; std::vector<int> resultL; std::vector<int> resultR; long long total = calculate(y1, x1, y2, x2, resultB, resultT, resultL, resultR); collectAndSend(resultB, resultT, resultL, resultR, total); if (MyNodeId() == 0) { printf("%lld\n", total); } } 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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 | #include <cstdio> #include <vector> #include <algorithm> #include "dzialka.h" #include "message.h" long long int merge(int dist1, int dist2, const std::vector<int>& v1_1, const std::vector<int>& v1_2, std::vector<int>& v1, const std::vector<int>& v2_1, const std::vector<int>& v2_2, std::vector<int>& v2) { int len = v1_1.size(); for (int i = 0; i < len; ++i) { v1.push_back((v1_1[i] == dist1) ? (v1_1[i] + v1_2[i]) : v1_1[i]); v2.push_back((v2_2[i] == dist2) ? (v2_2[i] + v2_1[i]) : v2_2[i]); } long long int sum = 0; for (int j = 0; j < len; ++j) { long long min1 = v1_2[j]; long long min2 = v2_1[j]; for (int i = j; i < len; ++i) { if (min1 > v1_2[i]) min1 = v1_2[i]; if (min2 > v2_1[i]) min2 = v2_1[i]; sum += min1 * min2; if (min1 == 0 || min2 == 0) { break; } } } return sum; } long long int calculate(int y1, int x1, int y2, int x2, std::vector<int>& rb, std::vector<int>& rt, std::vector<int>& rl, std::vector<int>& rr) { long long int sum = 0; if ((y2 - y1 > 1) && (x2 - x1 > 1)) { std::vector<int> resultB_1; std::vector<int> resultT_1; std::vector<int> resultL_1; std::vector<int> resultR_1; std::vector<int> resultB_2; std::vector<int> resultT_2; std::vector<int> resultL_2; std::vector<int> resultR_2; if (y2 - y1 > x2 - x1) { sum += calculate(y1, x1, y1 + (y2 - y1) / 2, x2, resultB_1, resultT_1, resultL_1, resultR_1); sum += calculate(y1 + (y2 - y1) / 2, x1, y2, x2, resultB_2, resultT_2, resultL_2, resultR_2); rr.insert(rr.end(), resultR_1.begin(), resultR_1.end()); rr.insert(rr.end(), resultR_2.begin(), resultR_2.end()); rl.insert(rl.end(), resultL_1.begin(), resultL_1.end()); rl.insert(rl.end(), resultL_2.begin(), resultL_2.end()); sum += merge((y2 - y1) / 2, y2 - y1 - (y2 - y1) / 2, resultB_1, resultB_2, rb, resultT_1, resultT_2, rt); } else { sum += calculate(y1, x1, y2, x1 + (x2 - x1) / 2, resultB_1, resultT_1, resultL_1, resultR_1); sum += calculate(y1, x1 + (x2 - x1) / 2, y2, x2, resultB_2, resultT_2, resultL_2, resultR_2); rt.insert(rt.end(), resultT_1.begin(), resultT_1.end()); rt.insert(rt.end(), resultT_2.begin(), resultT_2.end()); rb.insert(rb.end(), resultB_1.begin(), resultB_1.end()); rb.insert(rb.end(), resultB_2.begin(), resultB_2.end()); sum += merge((x2 - x1) / 2, x2 - x1 - (x2 - x1) / 2, resultL_1, resultL_2, rl, resultR_1, resultR_2, rr); } } else if (y2 == y1 || x2 == x1) { } else if (y2 - y1 == 1) { int firstZero = -1; int lastZero = -1; long long int cnt = 0; for (int i = x1; i < x2; ++i) { rt.push_back(IsUsableCell(y1, i)); if (rt.back() == 0) { if (firstZero == -1) { firstZero = rt.size() - 1; } lastZero = rt.size() - 1; sum += cnt * (cnt + 1) / 2; cnt = 0; } else { cnt++; } } sum += cnt * (cnt + 1) / 2; rb.assign(rt.begin(), rt.end()); rl.push_back(firstZero != -1 ? firstZero : rt.size()); rr.push_back(lastZero != -1 ? (rt.size() - lastZero - 1) : rt.size()); } else /* x2 - x1 == 1 */ { int firstZero = -1; int lastZero = -1; long long int cnt = 0; for (int i = y1; i < y2; ++i) { rl.push_back(IsUsableCell(i, x1)); if (rl.back() == 0) { if (firstZero == -1) { firstZero = rl.size() - 1; } lastZero = rl.size() - 1; sum += cnt * (cnt + 1) / 2; cnt = 0; } else { cnt++; } } sum += cnt * (cnt + 1) / 2; rr.assign(rl.begin(), rl.end()); rb.push_back(firstZero != -1 ? firstZero : rl.size()); rt.push_back(lastZero != -1 ? (rl.size() - lastZero - 1) : rl.size()); } return sum; } bool prepareRange(int& x1, int& y1, int& x2, int& y2) { int id = MyNodeId(); int totH = GetFieldHeight(); int totW = GetFieldWidth(); int rangeH = (totH + 9) / 10; int rangeW = (totW + 9) / 10; x1 = rangeW * (id % 10); y1 = rangeH * (id / 10); x2 = std::max(std::min(x1 + rangeW, totW), x1); y2 = std::max(std::min(y1 + rangeH, totH), y1); return (x1 < x2 && y1 < y2); } const int MAX_INTEGERS_IN_MESSAGE = 0x4000; long long int receiveResult(int id, std::vector<int>& rb, std::vector<int>& rt, std::vector<int>& rl, std::vector<int>& rr) { Receive(id); long long int total = GetLL(id); int rbSize = GetInt(id); int rtSize = GetInt(id); int rlSize = GetInt(id); int rrSize = GetInt(id); for (int i = 0; i < rbSize; ++i) { if (i % MAX_INTEGERS_IN_MESSAGE == 0) { Receive(id); } rb.push_back(GetInt(id)); } for (int i = 0; i < rtSize; ++i) { if (i % MAX_INTEGERS_IN_MESSAGE == 0) { Receive(id); } rt.push_back(GetInt(id)); } for (int i = 0; i < rlSize; ++i) { if (i % MAX_INTEGERS_IN_MESSAGE == 0) { Receive(id); } rl.push_back(GetInt(id)); } for (int i = 0; i < rrSize; ++i) { if (i % MAX_INTEGERS_IN_MESSAGE == 0) { Receive(id); } rr.push_back(GetInt(id)); } return total; } void sendResult(int id, long long int total, std::vector<int>& rb, std::vector<int>& rt, std::vector<int>& rl, std::vector<int>& rr) { PutLL(id, total); PutInt(id, rb.size()); PutInt(id, rt.size()); PutInt(id, rl.size()); PutInt(id, rr.size()); Send(id); for (int i = 0; i < rb.size(); ++i) { if (i != 0 && (i % MAX_INTEGERS_IN_MESSAGE == 0)) { Send(id); } PutInt(id, rb[i]); } Send(id); for (int i = 0; i < rt.size(); ++i) { if (i != 0 && (i % MAX_INTEGERS_IN_MESSAGE == 0)) { Send(id); } PutInt(id, rt[i]); } Send(id); for (int i = 0; i < rl.size(); ++i) { if (i != 0 && (i % MAX_INTEGERS_IN_MESSAGE == 0)) { Send(id); } PutInt(id, rl[i]); } Send(id); for (int i = 0; i < rr.size(); ++i) { if (i != 0 && (i % MAX_INTEGERS_IN_MESSAGE == 0)) { Send(id); } PutInt(id, rr[i]); } Send(id); } bool isIdActive(int id) { int w = GetFieldWidth(); int h = GetFieldHeight(); return ((id % 10 < w) && (id / 10 < h)); } long long int receiveAndMerge(int id, std::vector<int>& rb, std::vector<int>& rt, std::vector<int>& rl, std::vector<int>& rr, bool horisontal) { if (!isIdActive(id)) { return 0; } std::vector<int> resultB_1; std::vector<int> resultT_1; std::vector<int> resultL_1; std::vector<int> resultR_1; std::swap(resultB_1, rb); std::swap(resultT_1, rt); std::swap(resultR_1, rr); std::swap(resultL_1, rl); std::vector<int> resultB_2; std::vector<int> resultT_2; std::vector<int> resultL_2; std::vector<int> resultR_2; long long int total = receiveResult(id, resultB_2, resultT_2, resultL_2, resultR_2); if (!horisontal) { rr.insert(rr.end(), resultR_1.begin(), resultR_1.end()); rr.insert(rr.end(), resultR_2.begin(), resultR_2.end()); rl.insert(rl.end(), resultL_1.begin(), resultL_1.end()); rl.insert(rl.end(), resultL_2.begin(), resultL_2.end()); total += merge(resultR_1.size(), resultR_2.size(), resultB_1, resultB_2, rb, resultT_1, resultT_2, rt); } else { rt.insert(rt.end(), resultT_1.begin(), resultT_1.end()); rt.insert(rt.end(), resultT_2.begin(), resultT_2.end()); rb.insert(rb.end(), resultB_1.begin(), resultB_1.end()); rb.insert(rb.end(), resultB_2.begin(), resultB_2.end()); total += merge(resultB_1.size(), resultB_2.size(), resultL_1, resultL_2, rl, resultR_1, resultR_2, rr); } return total; } void collectAndSend(std::vector<int>& rb, std::vector<int>& rt, std::vector<int>& rl, std::vector<int>& rr, long long int& total) { int id = MyNodeId(); int mmm = 2; for (int mmm = 2; mmm <= 16; mmm *= 2) { if ((id % 10) % mmm == 0) { if ((id % 10) + (mmm / 2) < 10) { total += receiveAndMerge(id + mmm / 2, rb, rt, rl, rr, true); } } else { sendResult(id - (id % 10) % mmm, total, rb, rt, rl, rr); return; } if ((id/10) % mmm == 0) { if ((id / 10) + (mmm / 2) < 10) { total += receiveAndMerge(id + 10*(mmm/2), rb, rt, rl, rr, false); } } else { sendResult(id - 10 * ((id / 10) % mmm), total, rb, rt, rl, rr); return; } } } int main() { int x1, y1, x2, y2; int id = MyNodeId(); if (prepareRange(x1, y1, x2, y2)) { std::vector<int> resultB; std::vector<int> resultT; std::vector<int> resultL; std::vector<int> resultR; long long total = calculate(y1, x1, y2, x2, resultB, resultT, resultL, resultR); collectAndSend(resultB, resultT, resultL, resultR, total); if (MyNodeId() == 0) { printf("%lld\n", total); } } return 0; } |