//#include "teatr.h" #include "message.h" #include <algorithm> #include <iostream> #include <vector> #include <cassert> using ull = unsigned long long; using namespace std; int GetN() { return 100 * 1000 * 1000; } int GetElement(int i) { return 1 + (((long long)i * 27) + i & (i % 113) + ((i % 37) ^ (i % 20)) * 3571) % 1000000; } struct node { int pos; int l; int r; int val; }; bool comp(const node& a, const node& b) { if (a.val == b.val) return a.l > b.l; return a.val > b.val; } void update(vector<ull>& BIT, int n, int idx) { while (idx <= n) { BIT[idx]++; idx += idx & (-idx); } } ull query(const vector<ull>& BIT, int idx) { ull ans = 0; while (idx) { ans += BIT[idx]; idx -= idx & (-idx); } return ans; } const int MAX_PEOPLE = 1000000; ull answers[MAX_PEOPLE]; //from inclusive, to exclusive ull solveQuery(int from, int to) { const int n = to - from; ull subAnswer = 0; vector<node> nodes(2*n + 1 + MAX_PEOPLE); vector<ull> BIT(n + 1, 0); std::fill(answers, answers + MAX_PEOPLE, 0); for (int i = 1; i <= n; i++) { nodes[i].val = GetElement(from + i - 1); nodes[i].pos = 0; nodes[i].l = 0; nodes[i].r = i; } // iterate for all queries for (int i = n + 1; i <= 2*n; ++i) { const auto valueIndex = i - n; nodes[i].pos = valueIndex; nodes[i].val = nodes[valueIndex].val; nodes[i].l = 1; nodes[i].r = valueIndex; } for (int i = 1; i <= MAX_PEOPLE; ++i) { const auto x = 2 * n + i; nodes[x].pos = -1; nodes[x].val = i; nodes[x].l = 1; nodes[x].r = n; } sort(begin(nodes) + 1, end(nodes), comp); for (int i = 1; i <= (int)nodes.size() - 1; ++i) { if (nodes[i].pos > 0) { auto cnt = query(BIT, nodes[i].r) - query(BIT, nodes[i].l - 1); subAnswer += cnt; } else if(nodes[i].pos == 0) { update(BIT, n, nodes[i].r); } else { auto cnt = query(BIT, nodes[i].r) - query(BIT, nodes[i].l - 1); answers[nodes[i].val - 1] = cnt; } } return subAnswer; } ///////////////////////////// struct batch { int preBatchId; int calcBatchId; int nodesCountInMySegment; batch(int pre_batch_id, int calc_batch_id, int nodes_count_in_my_segment) : preBatchId(pre_batch_id), calcBatchId(calc_batch_id), nodesCountInMySegment(nodes_count_in_my_segment) { } }; const batch& getBatchInfo(int nodeId); int main() { const int n = GetN(); const int myId = MyNodeId(); auto& batchInfo = getBatchInfo(myId); int batchSize = n / 33; int from = batchInfo.preBatchId * batchSize; int to = batchInfo.preBatchId == 32 ? n : (from + batchSize); if(n <= 1e6) { if(myId == 0) { cout << solveQuery(0, n); } return 0; } auto nodeAnswer = solveQuery(from, to); batchSize = (n - to) / batchInfo.nodesCountInMySegment; from = to + batchInfo.calcBatchId * batchSize; to = (batchInfo.calcBatchId == batchInfo.nodesCountInMySegment - 1) ? n : (from + batchSize); if (batchInfo.calcBatchId != 0) { nodeAnswer = 0; } for (int i = from; i < to; ++i) { const auto nextHeight = GetElement(i) - 1; nodeAnswer += answers[nextHeight]; } PutLL(0, nodeAnswer); Send(0); if (MyNodeId() != 0) return 0; nodeAnswer = 0; for(int i = 99; i >= 0; --i) { Receive(i); nodeAnswer += GetLL(i); } cout << nodeAnswer; return 0; } bool initialized = false; batch batching[100] = { // nodes 9, 96 969 700, 16 161 616 batch(0, 0, 6), batch(0, 1, 6), batch(0, 2, 6), batch(0, 3, 6), batch(0, 4, 6), batch(0, 5, 6), // nodes 9, 93 939 400, 15 656 566 batch(1, 0, 6), batch(1, 1, 6), batch(1, 2, 6), batch(1, 3, 6), batch(1, 4, 6), batch(1, 5, 6), // nodes 9, 90 909 100, 18 181 820 batch(2, 0, 5), batch(2, 1, 5), batch(2, 2, 5), batch(2, 3, 5), batch(2, 4, 5), // nodes 8, 87 878 800, 17 575 760 batch(3, 0, 5), batch(3, 1, 5), batch(3, 2, 5), batch(3, 3, 5), batch(3, 4, 5), // nodes 8, 84 848 500, 16 969 700 batch(4, 0, 5), batch(4, 1, 5), batch(4, 2, 5), batch(4, 3, 5), batch(4, 4, 5), // node 7, 81 818 200, 16 363 640 batch(5, 0, 5), batch(5, 1, 5), batch(5, 2, 5), batch(5, 3, 5), batch(5, 4, 5), // node 7, 78 787 900, 15 757 580 batch(6, 0, 5), batch(6, 1, 5), batch(6, 2, 5), batch(6, 3, 5), batch(6, 4, 5), // node 6, 75 757 600, 15 151 520 batch(7, 0, 5), batch(7, 1, 5), batch(7, 2, 5), batch(7, 3, 5), batch(7, 4, 5), // node 6, 72 727 300, 18 181 825 batch(8, 0, 4), batch(8, 1, 4), batch(8, 2, 4), batch(8, 3, 4), // node 5, 69 697 000, 17 424 250 batch(9, 0, 4), batch(9, 1, 4), batch(9, 2, 4), batch(9, 3, 4), // node 5, 66 666 700, 16 666 675 batch(10, 0, 4), batch(10, 1, 4), batch(10, 2, 4), batch(10, 3, 4), // node 4, 63 636 400, 15 909 100 batch(11, 0, 4), batch(11, 1, 4), batch(11, 2, 4), batch(11, 3, 4), // node 4, 60 606 100, 15 151 525 batch(12, 0, 4), batch(12, 1, 4), batch(12, 2, 4), batch(12, 3, 4), // node 3, 57 575 800, 19 191 933 batch(13, 0, 3), batch(13, 1, 3), batch(13, 2, 3), // node 3, 54 545 500, 18 181 833 batch(14, 0, 3), batch(14, 1, 3), batch(14, 2, 3), // node 2, 51 515 200, 17 171 733 batch(15, 0, 3), batch(15, 1, 3), batch(15, 2, 3), // node 2, 48 484 900, 16 161 633 batch(16, 0, 3), batch(16, 1, 3), batch(16, 2, 3), // node 1, 45 454 600, 15 151 533 batch(17, 0, 3), batch(17, 1, 3), batch(17, 2, 3), // node 1, 42 424 300, 14 141 433 batch(18, 0, 3), batch(18, 1, 3), batch(18, 2, 3), // node 1, 39 394 000, 19 697 000 batch(19, 0, 2), batch(19, 1, 2), // node 1, 36 363 700, 18 181 850 batch(20, 0, 2), batch(20, 1, 2), // node 1, 33 333 400, 16 666 700 batch(21, 0, 2), batch(21, 1, 2), // node 1, 30 303 100, 15 151 550 batch(22, 0, 2), batch(22, 1, 2), // node 1, 27 272 800, 13 636 400 batch(23, 0, 2), batch(23, 1, 2), // node 1, 24 242 500, 12 121 250 batch(24, 0, 2), batch(24, 1, 2), // node 1, 21 212 200, 21 212 200 batch(25, 0, 1), // node 1, 18 181 900, 18 181 900 batch(26, 0, 1), // node 1, 15 151 600, 15 151 600 batch(27, 0, 1), // node 1, 12 121 300, 12 121 300 batch(28, 0, 1), // node 1, 6 060 700 batch(29, 0, 1), // node 1, 3 030 400 batch(30, 0, 1), // node 1, 0 batch(31, 0, 1), batch(32, 0, 1), }; const batch& getBatchInfo(int nodeId) { return batching[nodeId]; }
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 | //#include "teatr.h" #include "message.h" #include <algorithm> #include <iostream> #include <vector> #include <cassert> using ull = unsigned long long; using namespace std; int GetN() { return 100 * 1000 * 1000; } int GetElement(int i) { return 1 + (((long long)i * 27) + i & (i % 113) + ((i % 37) ^ (i % 20)) * 3571) % 1000000; } struct node { int pos; int l; int r; int val; }; bool comp(const node& a, const node& b) { if (a.val == b.val) return a.l > b.l; return a.val > b.val; } void update(vector<ull>& BIT, int n, int idx) { while (idx <= n) { BIT[idx]++; idx += idx & (-idx); } } ull query(const vector<ull>& BIT, int idx) { ull ans = 0; while (idx) { ans += BIT[idx]; idx -= idx & (-idx); } return ans; } const int MAX_PEOPLE = 1000000; ull answers[MAX_PEOPLE]; //from inclusive, to exclusive ull solveQuery(int from, int to) { const int n = to - from; ull subAnswer = 0; vector<node> nodes(2*n + 1 + MAX_PEOPLE); vector<ull> BIT(n + 1, 0); std::fill(answers, answers + MAX_PEOPLE, 0); for (int i = 1; i <= n; i++) { nodes[i].val = GetElement(from + i - 1); nodes[i].pos = 0; nodes[i].l = 0; nodes[i].r = i; } // iterate for all queries for (int i = n + 1; i <= 2*n; ++i) { const auto valueIndex = i - n; nodes[i].pos = valueIndex; nodes[i].val = nodes[valueIndex].val; nodes[i].l = 1; nodes[i].r = valueIndex; } for (int i = 1; i <= MAX_PEOPLE; ++i) { const auto x = 2 * n + i; nodes[x].pos = -1; nodes[x].val = i; nodes[x].l = 1; nodes[x].r = n; } sort(begin(nodes) + 1, end(nodes), comp); for (int i = 1; i <= (int)nodes.size() - 1; ++i) { if (nodes[i].pos > 0) { auto cnt = query(BIT, nodes[i].r) - query(BIT, nodes[i].l - 1); subAnswer += cnt; } else if(nodes[i].pos == 0) { update(BIT, n, nodes[i].r); } else { auto cnt = query(BIT, nodes[i].r) - query(BIT, nodes[i].l - 1); answers[nodes[i].val - 1] = cnt; } } return subAnswer; } ///////////////////////////// struct batch { int preBatchId; int calcBatchId; int nodesCountInMySegment; batch(int pre_batch_id, int calc_batch_id, int nodes_count_in_my_segment) : preBatchId(pre_batch_id), calcBatchId(calc_batch_id), nodesCountInMySegment(nodes_count_in_my_segment) { } }; const batch& getBatchInfo(int nodeId); int main() { const int n = GetN(); const int myId = MyNodeId(); auto& batchInfo = getBatchInfo(myId); int batchSize = n / 33; int from = batchInfo.preBatchId * batchSize; int to = batchInfo.preBatchId == 32 ? n : (from + batchSize); if(n <= 1e6) { if(myId == 0) { cout << solveQuery(0, n); } return 0; } auto nodeAnswer = solveQuery(from, to); batchSize = (n - to) / batchInfo.nodesCountInMySegment; from = to + batchInfo.calcBatchId * batchSize; to = (batchInfo.calcBatchId == batchInfo.nodesCountInMySegment - 1) ? n : (from + batchSize); if (batchInfo.calcBatchId != 0) { nodeAnswer = 0; } for (int i = from; i < to; ++i) { const auto nextHeight = GetElement(i) - 1; nodeAnswer += answers[nextHeight]; } PutLL(0, nodeAnswer); Send(0); if (MyNodeId() != 0) return 0; nodeAnswer = 0; for(int i = 99; i >= 0; --i) { Receive(i); nodeAnswer += GetLL(i); } cout << nodeAnswer; return 0; } bool initialized = false; batch batching[100] = { // nodes 9, 96 969 700, 16 161 616 batch(0, 0, 6), batch(0, 1, 6), batch(0, 2, 6), batch(0, 3, 6), batch(0, 4, 6), batch(0, 5, 6), // nodes 9, 93 939 400, 15 656 566 batch(1, 0, 6), batch(1, 1, 6), batch(1, 2, 6), batch(1, 3, 6), batch(1, 4, 6), batch(1, 5, 6), // nodes 9, 90 909 100, 18 181 820 batch(2, 0, 5), batch(2, 1, 5), batch(2, 2, 5), batch(2, 3, 5), batch(2, 4, 5), // nodes 8, 87 878 800, 17 575 760 batch(3, 0, 5), batch(3, 1, 5), batch(3, 2, 5), batch(3, 3, 5), batch(3, 4, 5), // nodes 8, 84 848 500, 16 969 700 batch(4, 0, 5), batch(4, 1, 5), batch(4, 2, 5), batch(4, 3, 5), batch(4, 4, 5), // node 7, 81 818 200, 16 363 640 batch(5, 0, 5), batch(5, 1, 5), batch(5, 2, 5), batch(5, 3, 5), batch(5, 4, 5), // node 7, 78 787 900, 15 757 580 batch(6, 0, 5), batch(6, 1, 5), batch(6, 2, 5), batch(6, 3, 5), batch(6, 4, 5), // node 6, 75 757 600, 15 151 520 batch(7, 0, 5), batch(7, 1, 5), batch(7, 2, 5), batch(7, 3, 5), batch(7, 4, 5), // node 6, 72 727 300, 18 181 825 batch(8, 0, 4), batch(8, 1, 4), batch(8, 2, 4), batch(8, 3, 4), // node 5, 69 697 000, 17 424 250 batch(9, 0, 4), batch(9, 1, 4), batch(9, 2, 4), batch(9, 3, 4), // node 5, 66 666 700, 16 666 675 batch(10, 0, 4), batch(10, 1, 4), batch(10, 2, 4), batch(10, 3, 4), // node 4, 63 636 400, 15 909 100 batch(11, 0, 4), batch(11, 1, 4), batch(11, 2, 4), batch(11, 3, 4), // node 4, 60 606 100, 15 151 525 batch(12, 0, 4), batch(12, 1, 4), batch(12, 2, 4), batch(12, 3, 4), // node 3, 57 575 800, 19 191 933 batch(13, 0, 3), batch(13, 1, 3), batch(13, 2, 3), // node 3, 54 545 500, 18 181 833 batch(14, 0, 3), batch(14, 1, 3), batch(14, 2, 3), // node 2, 51 515 200, 17 171 733 batch(15, 0, 3), batch(15, 1, 3), batch(15, 2, 3), // node 2, 48 484 900, 16 161 633 batch(16, 0, 3), batch(16, 1, 3), batch(16, 2, 3), // node 1, 45 454 600, 15 151 533 batch(17, 0, 3), batch(17, 1, 3), batch(17, 2, 3), // node 1, 42 424 300, 14 141 433 batch(18, 0, 3), batch(18, 1, 3), batch(18, 2, 3), // node 1, 39 394 000, 19 697 000 batch(19, 0, 2), batch(19, 1, 2), // node 1, 36 363 700, 18 181 850 batch(20, 0, 2), batch(20, 1, 2), // node 1, 33 333 400, 16 666 700 batch(21, 0, 2), batch(21, 1, 2), // node 1, 30 303 100, 15 151 550 batch(22, 0, 2), batch(22, 1, 2), // node 1, 27 272 800, 13 636 400 batch(23, 0, 2), batch(23, 1, 2), // node 1, 24 242 500, 12 121 250 batch(24, 0, 2), batch(24, 1, 2), // node 1, 21 212 200, 21 212 200 batch(25, 0, 1), // node 1, 18 181 900, 18 181 900 batch(26, 0, 1), // node 1, 15 151 600, 15 151 600 batch(27, 0, 1), // node 1, 12 121 300, 12 121 300 batch(28, 0, 1), // node 1, 6 060 700 batch(29, 0, 1), // node 1, 3 030 400 batch(30, 0, 1), // node 1, 0 batch(31, 0, 1), batch(32, 0, 1), }; const batch& getBatchInfo(int nodeId) { return batching[nodeId]; } |