#include <bits/stdc++.h> #include <unistd.h> #include "message.h" using namespace std; using u64 = uint64_t; using i64 = int64_t; using ci64 = int64_t const; using vi = vector<i64>; using vvi = vector<vector<i64>>; using cvi = vector<i64> const; using cvvi = vector<vector<i64>> const; using D = double; // {{{ utils vi split_range(i64 l, i64 r, i64 n) { vi res; for (i64 i = 1; i <= n; i++) res.push_back(l + (r - l) * i / n); return res; } pair<i64, i64> get_split(i64 l, i64 r, i64 n, i64 k) { auto const split = split_range(l, r, n); if (k == 0) return make_pair(0, split[0] - 1); return make_pair(split[k - 1], split[k] - 1); } // }}} int const v_max = 1000000; i64 n; i64 node_id; i64 nodes_cnt; pair<i64, i64> my_split() { return get_split(0, n, nodes_cnt, node_id); } pair<i64, i64> max_split() { return {0, n - 1}; } // print tuples {{{ template <typename T1, typename T2> ostream& operator<<(ostream& os, const pair<T1, T2>& t) { return os << '[' << t.first << ',' << t.second << ']'; } void print_tpl_helper(ostream&, bool) {} template <typename T, typename ...Ts> void print_tpl_helper(ostream& os, bool first, const T& v, const Ts& ...vs) { if (!first){os << ',';} os << v; print_tpl_helper(os, false, vs...); } template <typename ...Ts> ostream& print_struct(ostream& os, const char* classname, const Ts& ...vs) { // todo: named fields os << classname << '('; print_tpl_helper(os, true, vs...); return os << ')'; } struct Example { char symbol; u64 val; friend ostream& operator<<(ostream& os, const Example& elem) { return print_struct(os, "Op", elem.symbol, elem.val); } }; // }}} // print containers {{{ template <typename It> void print(ostream& os, It begin, It end, u64 len, u64 limit = 17) { u64 count = 0; os << "{"; while (begin != end && count < limit) { os << "(" << *begin << ")"; count++; begin++; } if (begin != end) os << "... " << len << " total"; os << "}"; } #define MAKE_PRINTER_1(container) \ template <typename T> ostream& operator<<(ostream& os, const container<T>& t) { print(os, t.begin(), t.end(), t.size()); return os; } #define MAKE_PRINTER_2(container) \ template <typename T1, typename T2> \ ostream& operator<<(ostream& os, const container<T1, T2>& t) { \ print(os, t.begin(), t.end(), t.size()); \ return os; \ } MAKE_PRINTER_1(vector) MAKE_PRINTER_2(map) MAKE_PRINTER_1(set) MAKE_PRINTER_2(unordered_map) MAKE_PRINTER_1(unordered_set) #undef MAKE_PRINTER_1 #undef MAKE_PRINTER_2 // }}} // read/write {{{ template <typename T> struct identity { const T& operator()(const T& t) const { return t; } }; #define PRINTERS(FNAME, OUTP) \ template <typename T> void FNAME(const T& t) { OUTP << t << ' '; } \ void FNAME##ln() { OUTP << '\n'; } \ template <typename T> void FNAME##ln(const T& t) { OUTP << t << '\n'; } \ template <typename T, typename F = identity<typename T::value_type>> \ void FNAME##v(const T& t, F f = F()) { for (const auto& e : t) FNAME(f(e)); FNAME##ln(); } PRINTERS(print, cout) #ifdef DEBUG_PRINTS PRINTERS(dprint, cerr) #else # define dprint(...) # define dprintv(...) # define dprintln(...) #endif /// }}} // range, len, max {{{ i64 max(i64 a, i64 b) { return a < b ? b : a; } vi range(i64 start, i64 end) { vi v; for (i64 i = start; i < end; i++) v.push_back(i); return v; } vi range(i64 end) { return range(0, end); } template <typename DS> i64 len(DS t) { return (i64)t.size(); } // }}} // {{{ teatr.h #ifdef MY_COMPUTER int const _n = 10000; //int GetN() { return 1000 * 1000 * 100; } //int GetElement(int i) { // // 2497220719002543 // using ll = long long; //// return 1 + i / 100; //return 1 + (((ll)i * 27) + i & (i %113) + ((i % 37) ^ (i % 20)) * 3571) % 1000000; //} //int GetN() { return 100 * 1000 * 1000; } //int GetElement(int i) { //if (i < 100 * 1000 * 1000 - 3) return 1 + i / 100; //else return 100 * 1000 * 1000 - i; //// 299999394 //} //int GetN() { return int(1e8); } // 2499912950050000 //int GetElement(int i) { return (i * 1ll * i) % int(1e6) + 1; } // int GetN() { return int(1e8); } int GetElement(int i) { return (int(1e8) - i - 1) / 100 + 1; } // 4999995000000000 , dunno if good // //int GetN() { // ++mock_counter; // if (mock_counter == mock_counter_limit) { // usleep(1000); // mock_counter = 0; // } // if (len(mock_data)) // return len(mock_data); // return _n; //} //int GetElement(int k) { // ++mock_counter; // if (mock_counter == mock_counter_limit) { // usleep(1000); // mock_counter = 0; // } // if (k < len(mock_data)) // return mock_data[k]; // return 1 + ((i64)k * 13213ll + 123ll) % v_max; //} #else //i64 mock_counter; //i64 const mock_counter_limit = 29000; // //void slow_down() { // ++mock_counter; // if (mock_counter == mock_counter_limit) { // usleep(1000); // mock_counter = 0; // } //} // //int GetN() { // slow_down(); // return 100 * 1000 * 1000; //} //int GetElement(int i) { // slow_down(); // using ll = long long; // return 1 + i / 100; //} #include "teatr.h" #endif // }}} void read_benchmark() { // {{{ i64 s = 0; n = GetN(); for (i64 i = 0; i < n; i++) s = s + GetElement(i) * 123ll % 12321ll; } // }}} #ifdef WITHOUT_RPA // {{{ message int NumberOfNodes() { return 4; } int MyNodeId() { return 0; } void PutChar(int target, char value); void PutInt(int target, int value); void PutLL(int target, long long value); void Send(int target); int Receive(int source); char GetChar(int source); int GetInt(int source); long long GetLL(int source); #endif // }}} // {{{ comm void send(i64 t, i64 v) { #ifdef WITHOUT_RPA print("would send to"); print(t); print("value"); println(v); #else dprint(node_id); dprint("sending to"); dprint(t); dprint("value"); dprintln(v); PutLL(t, v); Send(t); #endif } void send(i64 t, vi v) { #ifdef WITHOUT_RPA print("would send to"); print(t); print("value"); println(v); #else dprint(node_id); dprint("sending to"); dprint(t); dprint("value"); dprintln(v); for (auto e : v) PutLL(t, e); Send(t); #endif } void send(i64 t, pair<i64, i64> v) { #ifdef WITHOUT_RPA print("would send to"); print(t); print("value"); println(v); #else dprint(node_id); dprint("sending to"); dprint(t); dprint("value"); dprintln(v); PutLL(t, v.first); PutLL(t, v.second); Send(t); #endif } i64 retrieve_i64(i64 t, bool receive = true) { #ifdef WITHOUT_RPA print("would retrieve from"); println(t); print("with cereive"); println(receive); return t; #else dprint(node_id); dprint("retrieving from"); dprintln(t); if (receive) Receive(t); i64 const res = GetLL(t); dprint(node_id); dprint("retrieved from"); dprint(t); dprint("value"); dprintln(res); return res; #endif } pair<i64, i64> retrieve_pii(i64 t) { i64 a = retrieve_i64(t); auto res = pair<i64, i64>{a, retrieve_i64(t, false)}; dprint(node_id); dprint("retrieved from"); dprint(t); dprint("value"); dprintln(res); return res; } void retrieve(i64 t, vi & res, i64 c) { // dprint(node_id); dprint("retrieving from"); dprint(t); dprint(c); dprintln("values"); i64 const old_size = len(res); res.resize(len(res) + c); Receive(t); for (i64 i = old_size; i < len(res); i++) { res[i] = retrieve_i64(t, false); } dprint(node_id); dprint("retrieved from"); dprint(t); dprint(c); dprint("values from"); dprintln(res); } vi retrieve_vi(i64 t, i64 c) { vi res; retrieve(t, res, c); return res; } // }}} vi elements; pair<i64, i64> my_split_; void init_element_cache() { my_split_ = my_split(); elements.resize(my_split_.second - my_split_.first + 1); for (i64 i = my_split_.first; i <= my_split_.second; ++i) elements[i - my_split_.first] = GetElement(i); } i64 get_element(i64 k) { if (elements.empty()) return GetElement(k); if (k < my_split_.first || k > my_split_.second) return GetElement(k); return elements[k - my_split_.first]; } vi node_pos_stats; void calc_node_pos_stats() { node_pos_stats.clear(); elements.clear(); pair<i64, i64> i_interval = my_split(); vi counts(v_max + 1, 0); for (i64 i = i_interval.first; i <= i_interval.second; ++i) { elements.push_back(get_element(i)); ++counts[elements.back()]; } i64 c = 0; vi const limits = split_range(0, i_interval.second - i_interval.first + 1, nodes_cnt); dprint(node_id); dprint("calcs node pos stats with interval"); dprint(i_interval); dprint("limits"); dprint(limits); dprint("counts"); dprintln(counts); i64 limit_i = 0; for (i64 v = 1; v <= v_max; v++) { c += counts[v]; while (limit_i < len(limits) && c >= limits[limit_i]) { node_pos_stats.push_back(v); ++limit_i; } } } vi tree; i64 tree_sum; vi tree_stage; void add_to_tree(i64 e) { tree_stage.push_back(e); } i64 get_from_tree(i64 e) { i64 res = 0; do { res += tree[e]; e -= e & (~(e - 1)); } while (e > 0); return tree_sum - res; } void flush_tree() { for (auto e : tree_stage) { do { ++tree[e]; e += e & (~(e - 1)); } while (e <= v_max); } tree_sum += len(tree_stage); tree_stage.clear(); } vi counts_in_row; i64 calc_for_row_column(pair<i64, i64> value_interval, pair<i64, i64> index_interval, bool count = false, bool dont_count_in_index_interval = false) { i64 res = 0; i64 value_count = 0; tree = vi(v_max + 1, 0); tree_sum = 0; tree_stage.clear(); i64 col_num = 0; if (count) counts_in_row.resize(nodes_cnt); auto const count_range = split_range(0, n, nodes_cnt); for (i64 i = index_interval.first; i <= index_interval.second; ++i) { i64 const e = get_element(i); //println(e); if (count_range[col_num] == i) { ++col_num; flush_tree(); } if (e < value_interval.first || e > value_interval.second) continue; if (count) { ++counts_in_row[col_num]; } ++value_count; if (value_interval.first != value_interval.second) { res += get_from_tree(e); add_to_tree(e); if (!dont_count_in_index_interval) flush_tree(); } } flush_tree(); if (value_interval.first == value_interval.second) res = 0; dprint(node_id); dprint("res for values"); dprint(value_interval); dprint("indexes"); dprint(index_interval); dprint("is"); dprintln(res); return res; } pair<i64, i64> row_assignment; i64 node_result; void calc_results_and_counts() { node_result += calc_for_row_column({0, v_max}, my_split()); node_result += calc_for_row_column(row_assignment, max_split(), true, true); dprint(node_id); dprint("result"); dprint(node_result); dprint("counts"); dprintln(counts_in_row); } void worker_workflow() { init_element_cache(); calc_node_pos_stats(); send(0, node_pos_stats); row_assignment = retrieve_pii(0); calc_results_and_counts(); send(0, node_result); send(0, counts_in_row); } vi pos_stats_from_nodes; void root_get_pos_stats_from_nodes() { dprintln("root_get_pos_stats_from_nodes starts"); pos_stats_from_nodes = node_pos_stats; for (i64 i = 1; i < nodes_cnt; i++) retrieve(i, pos_stats_from_nodes, nodes_cnt); dprintln("root_get_pos_stats_from_nodes succeeded"); } vector<pair<i64, i64>> row_assignments; void calc_row_assignments_from_pos_stats() { row_assignments = {}; sort(pos_stats_from_nodes.begin(), pos_stats_from_nodes.end()); i64 prev = 0; for (i64 i = 0; i < nodes_cnt; ++i) { i64 const cur = pos_stats_from_nodes[(i + 1) * nodes_cnt - 1]; i64 const next = i + 1 < nodes_cnt ? pos_stats_from_nodes[(i + 2) * nodes_cnt - 1] : 0; if (prev != cur && cur == next) { row_assignments.push_back(make_pair(prev + 1, cur - 1)); row_assignments.push_back(make_pair(cur, cur)); } else { if (prev != cur) row_assignments.push_back(make_pair(prev + 1, cur)); } prev = cur; } while (len(row_assignments) < nodes_cnt) row_assignments.push_back({0, 0}); dprint("row assignments"); dprintln(row_assignments); } i64 result; vvi count_matrix; void root_get_results_from_nodes() { result = node_result; count_matrix = {counts_in_row}; for (i64 i = 1; i < nodes_cnt; i++) { result += retrieve_i64(i); count_matrix.push_back(retrieve_vi(i, nodes_cnt)); } reverse(count_matrix.begin(), count_matrix.end()); } vvi counts; void aggregate_result_for_matrix() { counts = vvi(nodes_cnt + 1, vi(nodes_cnt + 1, 0)); dprintln(result); dprintln(count_matrix); for (i64 i = 0; i < nodes_cnt; ++i) { for (i64 j = 0; j < nodes_cnt; ++j) { result += counts[i][j] * count_matrix[i][j]; counts[i + 1][j + 1] = count_matrix[i][j] + counts[i][j + 1] + counts[i + 1][j] - counts[i][j]; } } dprintln(result); // for (auto c : counts) println(c); } void root_workflow() { dprint("root"); dprint("starts work with nodes"); dprint(nodes_cnt); dprint("and n"); dprintln(n); init_element_cache(); calc_node_pos_stats(); root_get_pos_stats_from_nodes(); calc_row_assignments_from_pos_stats(); for (i64 i = 1; i < nodes_cnt; i++) send(i, row_assignments[i]); row_assignment = row_assignments[0]; calc_results_and_counts(); root_get_results_from_nodes(); aggregate_result_for_matrix(); } void go() { node_id = MyNodeId(); nodes_cnt = NumberOfNodes(); n = GetN(); if (n <= nodes_cnt * nodes_cnt) { if (node_id == 0) println(calc_for_row_column(make_pair(0, v_max), make_pair(0, n - 1))); // nodes_cnt = 1; // if (node_id != 0) return; } if (node_id == 0) { root_workflow(); println(result); } else worker_workflow(); } // {{{ tests void test_aggregate() { nodes_cnt = 5; result = 0; count_matrix = { {2, 3, 2, 4, 5}, {10, 10, 20, 30, 10}, {100, 100, 100, 200, 100}, {1000, 1000, 1000, 1000, 1000}, {10000, 10000, 10000, 10000, 10000} }; aggregate_result_for_matrix(); println(result); nodes_cnt = 3; result = 0; count_matrix = { {3, 1, 3}, {2, 4, 6}, {1, 1, 1} }; aggregate_result_for_matrix(); println(result); } #ifdef MY_COMPUTER2 void test_node_pos_stats() { // nodes_cnt = 3; // node_id = 0; // mock_data = {1, 2, 3, 3, 3, 3, 3, 8, 9}; // mock_data.resize(len(mock_data) * nodes_cnt); // n = GetN(); // calc_node_pos_stats(); // println(node_pos_stats); nodes_cnt = 1; mock_data = {1, 2, 3, 4, 5}; n = GetN(); calc_node_pos_stats(); println(node_pos_stats); } void test_counts() { mock_data = {100, 5, 2, 1000, 1, 4, 4}; nodes_cnt = 3; n = 9; mock_data.resize(n); calc_for_row_column({1, 2000}, {0, 8}, true); println(split_range(0, n, nodes_cnt)); } void test_calc_for_row_column() { mock_data = {100, 5, 2, 1000, 1, 4, 4, 3, 1}; println(calc_for_row_column({2, 200}, {1, 7})); test_counts(); } void tests_calc_row_assignments_from_pos_stats() { // nodes_cnt = 2; // pos_stats_from_nodes = {2, 2, 2, 2}; // calc_row_assignments_from_pos_stats(); // println(row_assignments); // nodes_cnt = 4; // pos_stats_from_nodes = {1, 2, 2, 2, 2, 2, 8, 8, 8, 8, 8, 8, 8, 8, 8, 13}; // calc_row_assignments_from_pos_stats(); // println(row_assignments); nodes_cnt = 1; pos_stats_from_nodes = {13}; calc_row_assignments_from_pos_stats(); println(row_assignments); } void test_tree() { tree = vi(v_max + 1, 0); add_to_tree(2); add_to_tree(5); add_to_tree(7); println(get_from_tree(1)); println(get_from_tree(2)); println(get_from_tree(3)); println(get_from_tree(6)); println(get_from_tree(9)); } // }}} #endif int main () { // {{{ ios_base::sync_with_stdio(0); cin.tie(0); go(); } //
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 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 | #include <bits/stdc++.h> #include <unistd.h> #include "message.h" using namespace std; using u64 = uint64_t; using i64 = int64_t; using ci64 = int64_t const; using vi = vector<i64>; using vvi = vector<vector<i64>>; using cvi = vector<i64> const; using cvvi = vector<vector<i64>> const; using D = double; // {{{ utils vi split_range(i64 l, i64 r, i64 n) { vi res; for (i64 i = 1; i <= n; i++) res.push_back(l + (r - l) * i / n); return res; } pair<i64, i64> get_split(i64 l, i64 r, i64 n, i64 k) { auto const split = split_range(l, r, n); if (k == 0) return make_pair(0, split[0] - 1); return make_pair(split[k - 1], split[k] - 1); } // }}} int const v_max = 1000000; i64 n; i64 node_id; i64 nodes_cnt; pair<i64, i64> my_split() { return get_split(0, n, nodes_cnt, node_id); } pair<i64, i64> max_split() { return {0, n - 1}; } // print tuples {{{ template <typename T1, typename T2> ostream& operator<<(ostream& os, const pair<T1, T2>& t) { return os << '[' << t.first << ',' << t.second << ']'; } void print_tpl_helper(ostream&, bool) {} template <typename T, typename ...Ts> void print_tpl_helper(ostream& os, bool first, const T& v, const Ts& ...vs) { if (!first){os << ',';} os << v; print_tpl_helper(os, false, vs...); } template <typename ...Ts> ostream& print_struct(ostream& os, const char* classname, const Ts& ...vs) { // todo: named fields os << classname << '('; print_tpl_helper(os, true, vs...); return os << ')'; } struct Example { char symbol; u64 val; friend ostream& operator<<(ostream& os, const Example& elem) { return print_struct(os, "Op", elem.symbol, elem.val); } }; // }}} // print containers {{{ template <typename It> void print(ostream& os, It begin, It end, u64 len, u64 limit = 17) { u64 count = 0; os << "{"; while (begin != end && count < limit) { os << "(" << *begin << ")"; count++; begin++; } if (begin != end) os << "... " << len << " total"; os << "}"; } #define MAKE_PRINTER_1(container) \ template <typename T> ostream& operator<<(ostream& os, const container<T>& t) { print(os, t.begin(), t.end(), t.size()); return os; } #define MAKE_PRINTER_2(container) \ template <typename T1, typename T2> \ ostream& operator<<(ostream& os, const container<T1, T2>& t) { \ print(os, t.begin(), t.end(), t.size()); \ return os; \ } MAKE_PRINTER_1(vector) MAKE_PRINTER_2(map) MAKE_PRINTER_1(set) MAKE_PRINTER_2(unordered_map) MAKE_PRINTER_1(unordered_set) #undef MAKE_PRINTER_1 #undef MAKE_PRINTER_2 // }}} // read/write {{{ template <typename T> struct identity { const T& operator()(const T& t) const { return t; } }; #define PRINTERS(FNAME, OUTP) \ template <typename T> void FNAME(const T& t) { OUTP << t << ' '; } \ void FNAME##ln() { OUTP << '\n'; } \ template <typename T> void FNAME##ln(const T& t) { OUTP << t << '\n'; } \ template <typename T, typename F = identity<typename T::value_type>> \ void FNAME##v(const T& t, F f = F()) { for (const auto& e : t) FNAME(f(e)); FNAME##ln(); } PRINTERS(print, cout) #ifdef DEBUG_PRINTS PRINTERS(dprint, cerr) #else # define dprint(...) # define dprintv(...) # define dprintln(...) #endif /// }}} // range, len, max {{{ i64 max(i64 a, i64 b) { return a < b ? b : a; } vi range(i64 start, i64 end) { vi v; for (i64 i = start; i < end; i++) v.push_back(i); return v; } vi range(i64 end) { return range(0, end); } template <typename DS> i64 len(DS t) { return (i64)t.size(); } // }}} // {{{ teatr.h #ifdef MY_COMPUTER int const _n = 10000; //int GetN() { return 1000 * 1000 * 100; } //int GetElement(int i) { // // 2497220719002543 // using ll = long long; //// return 1 + i / 100; //return 1 + (((ll)i * 27) + i & (i %113) + ((i % 37) ^ (i % 20)) * 3571) % 1000000; //} //int GetN() { return 100 * 1000 * 1000; } //int GetElement(int i) { //if (i < 100 * 1000 * 1000 - 3) return 1 + i / 100; //else return 100 * 1000 * 1000 - i; //// 299999394 //} //int GetN() { return int(1e8); } // 2499912950050000 //int GetElement(int i) { return (i * 1ll * i) % int(1e6) + 1; } // int GetN() { return int(1e8); } int GetElement(int i) { return (int(1e8) - i - 1) / 100 + 1; } // 4999995000000000 , dunno if good // //int GetN() { // ++mock_counter; // if (mock_counter == mock_counter_limit) { // usleep(1000); // mock_counter = 0; // } // if (len(mock_data)) // return len(mock_data); // return _n; //} //int GetElement(int k) { // ++mock_counter; // if (mock_counter == mock_counter_limit) { // usleep(1000); // mock_counter = 0; // } // if (k < len(mock_data)) // return mock_data[k]; // return 1 + ((i64)k * 13213ll + 123ll) % v_max; //} #else //i64 mock_counter; //i64 const mock_counter_limit = 29000; // //void slow_down() { // ++mock_counter; // if (mock_counter == mock_counter_limit) { // usleep(1000); // mock_counter = 0; // } //} // //int GetN() { // slow_down(); // return 100 * 1000 * 1000; //} //int GetElement(int i) { // slow_down(); // using ll = long long; // return 1 + i / 100; //} #include "teatr.h" #endif // }}} void read_benchmark() { // {{{ i64 s = 0; n = GetN(); for (i64 i = 0; i < n; i++) s = s + GetElement(i) * 123ll % 12321ll; } // }}} #ifdef WITHOUT_RPA // {{{ message int NumberOfNodes() { return 4; } int MyNodeId() { return 0; } void PutChar(int target, char value); void PutInt(int target, int value); void PutLL(int target, long long value); void Send(int target); int Receive(int source); char GetChar(int source); int GetInt(int source); long long GetLL(int source); #endif // }}} // {{{ comm void send(i64 t, i64 v) { #ifdef WITHOUT_RPA print("would send to"); print(t); print("value"); println(v); #else dprint(node_id); dprint("sending to"); dprint(t); dprint("value"); dprintln(v); PutLL(t, v); Send(t); #endif } void send(i64 t, vi v) { #ifdef WITHOUT_RPA print("would send to"); print(t); print("value"); println(v); #else dprint(node_id); dprint("sending to"); dprint(t); dprint("value"); dprintln(v); for (auto e : v) PutLL(t, e); Send(t); #endif } void send(i64 t, pair<i64, i64> v) { #ifdef WITHOUT_RPA print("would send to"); print(t); print("value"); println(v); #else dprint(node_id); dprint("sending to"); dprint(t); dprint("value"); dprintln(v); PutLL(t, v.first); PutLL(t, v.second); Send(t); #endif } i64 retrieve_i64(i64 t, bool receive = true) { #ifdef WITHOUT_RPA print("would retrieve from"); println(t); print("with cereive"); println(receive); return t; #else dprint(node_id); dprint("retrieving from"); dprintln(t); if (receive) Receive(t); i64 const res = GetLL(t); dprint(node_id); dprint("retrieved from"); dprint(t); dprint("value"); dprintln(res); return res; #endif } pair<i64, i64> retrieve_pii(i64 t) { i64 a = retrieve_i64(t); auto res = pair<i64, i64>{a, retrieve_i64(t, false)}; dprint(node_id); dprint("retrieved from"); dprint(t); dprint("value"); dprintln(res); return res; } void retrieve(i64 t, vi & res, i64 c) { // dprint(node_id); dprint("retrieving from"); dprint(t); dprint(c); dprintln("values"); i64 const old_size = len(res); res.resize(len(res) + c); Receive(t); for (i64 i = old_size; i < len(res); i++) { res[i] = retrieve_i64(t, false); } dprint(node_id); dprint("retrieved from"); dprint(t); dprint(c); dprint("values from"); dprintln(res); } vi retrieve_vi(i64 t, i64 c) { vi res; retrieve(t, res, c); return res; } // }}} vi elements; pair<i64, i64> my_split_; void init_element_cache() { my_split_ = my_split(); elements.resize(my_split_.second - my_split_.first + 1); for (i64 i = my_split_.first; i <= my_split_.second; ++i) elements[i - my_split_.first] = GetElement(i); } i64 get_element(i64 k) { if (elements.empty()) return GetElement(k); if (k < my_split_.first || k > my_split_.second) return GetElement(k); return elements[k - my_split_.first]; } vi node_pos_stats; void calc_node_pos_stats() { node_pos_stats.clear(); elements.clear(); pair<i64, i64> i_interval = my_split(); vi counts(v_max + 1, 0); for (i64 i = i_interval.first; i <= i_interval.second; ++i) { elements.push_back(get_element(i)); ++counts[elements.back()]; } i64 c = 0; vi const limits = split_range(0, i_interval.second - i_interval.first + 1, nodes_cnt); dprint(node_id); dprint("calcs node pos stats with interval"); dprint(i_interval); dprint("limits"); dprint(limits); dprint("counts"); dprintln(counts); i64 limit_i = 0; for (i64 v = 1; v <= v_max; v++) { c += counts[v]; while (limit_i < len(limits) && c >= limits[limit_i]) { node_pos_stats.push_back(v); ++limit_i; } } } vi tree; i64 tree_sum; vi tree_stage; void add_to_tree(i64 e) { tree_stage.push_back(e); } i64 get_from_tree(i64 e) { i64 res = 0; do { res += tree[e]; e -= e & (~(e - 1)); } while (e > 0); return tree_sum - res; } void flush_tree() { for (auto e : tree_stage) { do { ++tree[e]; e += e & (~(e - 1)); } while (e <= v_max); } tree_sum += len(tree_stage); tree_stage.clear(); } vi counts_in_row; i64 calc_for_row_column(pair<i64, i64> value_interval, pair<i64, i64> index_interval, bool count = false, bool dont_count_in_index_interval = false) { i64 res = 0; i64 value_count = 0; tree = vi(v_max + 1, 0); tree_sum = 0; tree_stage.clear(); i64 col_num = 0; if (count) counts_in_row.resize(nodes_cnt); auto const count_range = split_range(0, n, nodes_cnt); for (i64 i = index_interval.first; i <= index_interval.second; ++i) { i64 const e = get_element(i); //println(e); if (count_range[col_num] == i) { ++col_num; flush_tree(); } if (e < value_interval.first || e > value_interval.second) continue; if (count) { ++counts_in_row[col_num]; } ++value_count; if (value_interval.first != value_interval.second) { res += get_from_tree(e); add_to_tree(e); if (!dont_count_in_index_interval) flush_tree(); } } flush_tree(); if (value_interval.first == value_interval.second) res = 0; dprint(node_id); dprint("res for values"); dprint(value_interval); dprint("indexes"); dprint(index_interval); dprint("is"); dprintln(res); return res; } pair<i64, i64> row_assignment; i64 node_result; void calc_results_and_counts() { node_result += calc_for_row_column({0, v_max}, my_split()); node_result += calc_for_row_column(row_assignment, max_split(), true, true); dprint(node_id); dprint("result"); dprint(node_result); dprint("counts"); dprintln(counts_in_row); } void worker_workflow() { init_element_cache(); calc_node_pos_stats(); send(0, node_pos_stats); row_assignment = retrieve_pii(0); calc_results_and_counts(); send(0, node_result); send(0, counts_in_row); } vi pos_stats_from_nodes; void root_get_pos_stats_from_nodes() { dprintln("root_get_pos_stats_from_nodes starts"); pos_stats_from_nodes = node_pos_stats; for (i64 i = 1; i < nodes_cnt; i++) retrieve(i, pos_stats_from_nodes, nodes_cnt); dprintln("root_get_pos_stats_from_nodes succeeded"); } vector<pair<i64, i64>> row_assignments; void calc_row_assignments_from_pos_stats() { row_assignments = {}; sort(pos_stats_from_nodes.begin(), pos_stats_from_nodes.end()); i64 prev = 0; for (i64 i = 0; i < nodes_cnt; ++i) { i64 const cur = pos_stats_from_nodes[(i + 1) * nodes_cnt - 1]; i64 const next = i + 1 < nodes_cnt ? pos_stats_from_nodes[(i + 2) * nodes_cnt - 1] : 0; if (prev != cur && cur == next) { row_assignments.push_back(make_pair(prev + 1, cur - 1)); row_assignments.push_back(make_pair(cur, cur)); } else { if (prev != cur) row_assignments.push_back(make_pair(prev + 1, cur)); } prev = cur; } while (len(row_assignments) < nodes_cnt) row_assignments.push_back({0, 0}); dprint("row assignments"); dprintln(row_assignments); } i64 result; vvi count_matrix; void root_get_results_from_nodes() { result = node_result; count_matrix = {counts_in_row}; for (i64 i = 1; i < nodes_cnt; i++) { result += retrieve_i64(i); count_matrix.push_back(retrieve_vi(i, nodes_cnt)); } reverse(count_matrix.begin(), count_matrix.end()); } vvi counts; void aggregate_result_for_matrix() { counts = vvi(nodes_cnt + 1, vi(nodes_cnt + 1, 0)); dprintln(result); dprintln(count_matrix); for (i64 i = 0; i < nodes_cnt; ++i) { for (i64 j = 0; j < nodes_cnt; ++j) { result += counts[i][j] * count_matrix[i][j]; counts[i + 1][j + 1] = count_matrix[i][j] + counts[i][j + 1] + counts[i + 1][j] - counts[i][j]; } } dprintln(result); // for (auto c : counts) println(c); } void root_workflow() { dprint("root"); dprint("starts work with nodes"); dprint(nodes_cnt); dprint("and n"); dprintln(n); init_element_cache(); calc_node_pos_stats(); root_get_pos_stats_from_nodes(); calc_row_assignments_from_pos_stats(); for (i64 i = 1; i < nodes_cnt; i++) send(i, row_assignments[i]); row_assignment = row_assignments[0]; calc_results_and_counts(); root_get_results_from_nodes(); aggregate_result_for_matrix(); } void go() { node_id = MyNodeId(); nodes_cnt = NumberOfNodes(); n = GetN(); if (n <= nodes_cnt * nodes_cnt) { if (node_id == 0) println(calc_for_row_column(make_pair(0, v_max), make_pair(0, n - 1))); // nodes_cnt = 1; // if (node_id != 0) return; } if (node_id == 0) { root_workflow(); println(result); } else worker_workflow(); } // {{{ tests void test_aggregate() { nodes_cnt = 5; result = 0; count_matrix = { {2, 3, 2, 4, 5}, {10, 10, 20, 30, 10}, {100, 100, 100, 200, 100}, {1000, 1000, 1000, 1000, 1000}, {10000, 10000, 10000, 10000, 10000} }; aggregate_result_for_matrix(); println(result); nodes_cnt = 3; result = 0; count_matrix = { {3, 1, 3}, {2, 4, 6}, {1, 1, 1} }; aggregate_result_for_matrix(); println(result); } #ifdef MY_COMPUTER2 void test_node_pos_stats() { // nodes_cnt = 3; // node_id = 0; // mock_data = {1, 2, 3, 3, 3, 3, 3, 8, 9}; // mock_data.resize(len(mock_data) * nodes_cnt); // n = GetN(); // calc_node_pos_stats(); // println(node_pos_stats); nodes_cnt = 1; mock_data = {1, 2, 3, 4, 5}; n = GetN(); calc_node_pos_stats(); println(node_pos_stats); } void test_counts() { mock_data = {100, 5, 2, 1000, 1, 4, 4}; nodes_cnt = 3; n = 9; mock_data.resize(n); calc_for_row_column({1, 2000}, {0, 8}, true); println(split_range(0, n, nodes_cnt)); } void test_calc_for_row_column() { mock_data = {100, 5, 2, 1000, 1, 4, 4, 3, 1}; println(calc_for_row_column({2, 200}, {1, 7})); test_counts(); } void tests_calc_row_assignments_from_pos_stats() { // nodes_cnt = 2; // pos_stats_from_nodes = {2, 2, 2, 2}; // calc_row_assignments_from_pos_stats(); // println(row_assignments); // nodes_cnt = 4; // pos_stats_from_nodes = {1, 2, 2, 2, 2, 2, 8, 8, 8, 8, 8, 8, 8, 8, 8, 13}; // calc_row_assignments_from_pos_stats(); // println(row_assignments); nodes_cnt = 1; pos_stats_from_nodes = {13}; calc_row_assignments_from_pos_stats(); println(row_assignments); } void test_tree() { tree = vi(v_max + 1, 0); add_to_tree(2); add_to_tree(5); add_to_tree(7); println(get_from_tree(1)); println(get_from_tree(2)); println(get_from_tree(3)); println(get_from_tree(6)); println(get_from_tree(9)); } // }}} #endif int main () { // {{{ ios_base::sync_with_stdio(0); cin.tie(0); go(); } // |