#include "message.h" #include "teatr.h" #include "bits/stdc++.h" using namespace std; typedef unsigned long long ull; struct Elem { int value; int count; }; typedef vector<Elem> Stat; ull Merge(const Stat& a, const Stat& b, Stat& c) { ull res = 0; ull sum_b = 0; auto i = a.begin(); auto j = b.begin(); while (i != a.end() || j != b.end()) { if (i == a.end()) { c.push_back(*j); j++; } else if (j == b.end()) { res += sum_b * i->count; c.push_back(*i); i++; } else if (i->value < j->value) { res += sum_b * i->count; c.push_back(*i); i++; } else if (i->value > j->value) { sum_b += j->count; c.push_back(*j); j++; } else { res += sum_b * i->count; sum_b += j->count; c.push_back(Elem{i->value, i->count + j->count}); i++; j++; } } return res; } ull Compute(int l, int r, Stat& c) { if (l + 1 == r) { c.push_back(Elem{GetElement(l), 1}); return 0; } Stat a, b; int m = (l + r) / 2; ull res = Compute(l, m, a) + Compute(m, r, b); ull merged = Merge(a, b, c); return res + merged; } void SendStat(int target, const Stat& c, ull res) { PutLL(target, res); PutInt(target, c.size()); for (Elem e : c) { PutInt(target, e.value); PutInt(target, e.count); } Send(target); } ull ReceiveStat(int source, Stat& c) { Receive(source); ull res = GetLL(source); c.clear(); int size = GetInt(source); c.resize(size); for (int i = 0; i < size; i++) { c[i].value = GetInt(source); c[i].count = GetInt(source); } return res; } int main() { int n = GetN(); int id = MyNodeId(); int nodes = NumberOfNodes(); if (id >= n) return 0; nodes = std::min(nodes, n); auto IthEnd = [=](long long i) { return (n * i) / nodes; }; Stat c; ull res = Compute(IthEnd(id), IthEnd(id + 1), c); for (int i = 1; i < nodes; i <<= 1) { if (id & i) { SendStat(id ^ i, c, res); return 0; } int source = id ^ i; if (source < nodes) { Stat b, a = std::move(c); res += ReceiveStat(source, b); res += Merge(a, b, c);; } } cout << res << "\n"; 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 | #include "message.h" #include "teatr.h" #include "bits/stdc++.h" using namespace std; typedef unsigned long long ull; struct Elem { int value; int count; }; typedef vector<Elem> Stat; ull Merge(const Stat& a, const Stat& b, Stat& c) { ull res = 0; ull sum_b = 0; auto i = a.begin(); auto j = b.begin(); while (i != a.end() || j != b.end()) { if (i == a.end()) { c.push_back(*j); j++; } else if (j == b.end()) { res += sum_b * i->count; c.push_back(*i); i++; } else if (i->value < j->value) { res += sum_b * i->count; c.push_back(*i); i++; } else if (i->value > j->value) { sum_b += j->count; c.push_back(*j); j++; } else { res += sum_b * i->count; sum_b += j->count; c.push_back(Elem{i->value, i->count + j->count}); i++; j++; } } return res; } ull Compute(int l, int r, Stat& c) { if (l + 1 == r) { c.push_back(Elem{GetElement(l), 1}); return 0; } Stat a, b; int m = (l + r) / 2; ull res = Compute(l, m, a) + Compute(m, r, b); ull merged = Merge(a, b, c); return res + merged; } void SendStat(int target, const Stat& c, ull res) { PutLL(target, res); PutInt(target, c.size()); for (Elem e : c) { PutInt(target, e.value); PutInt(target, e.count); } Send(target); } ull ReceiveStat(int source, Stat& c) { Receive(source); ull res = GetLL(source); c.clear(); int size = GetInt(source); c.resize(size); for (int i = 0; i < size; i++) { c[i].value = GetInt(source); c[i].count = GetInt(source); } return res; } int main() { int n = GetN(); int id = MyNodeId(); int nodes = NumberOfNodes(); if (id >= n) return 0; nodes = std::min(nodes, n); auto IthEnd = [=](long long i) { return (n * i) / nodes; }; Stat c; ull res = Compute(IthEnd(id), IthEnd(id + 1), c); for (int i = 1; i < nodes; i <<= 1) { if (id & i) { SendStat(id ^ i, c, res); return 0; } int source = id ^ i; if (source < nodes) { Stat b, a = std::move(c); res += ReceiveStat(source, b); res += Merge(a, b, c);; } } cout << res << "\n"; return 0; } |