#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; #define FOR(i,a,n) for (auto i ## __ = (n), i = (a); i <= i ## __; ++i) #define FORD(i,a,n) for (auto i ## __ = (n), i = (a); i >= i ## __; --i) #define REP(i,n) FOR(i,0, n - 1) #define ALL(h) begin(h), end(h) #define EB emplace_back #define X first #define Y second #define V vector #define tpv typedef V< typedef long long LL; typedef pair<int, int> PII; tpv int> VI; tpv VI> VVI; tpv PII> VPII; tpv LL> VLL; constexpr char nl = '\n'; #define endl nl #define ris return *this #define tem template<class t tem, class u> inline void mini(t& a, u&& b) { if (b < a) a = b; } tem, class u> inline void maxi(t& a, u&& b) { if (b > a) a = b; } int ceil2(int h) { return h < 2 ? 1 : 1 << (sizeof(h) * 8 - __builtin_clz(h - 1)); } tem> struct Dump { t a, b; }; tem> auto dump(t&& h) -> Dump<decltype(begin(h))> { return {ALL(h)}; } tem> auto stub(t* h) -> decltype(cerr << *h, 0); tem> char stub(...); #define enif(o) tem> typename enable_if<sizeof stub<t>(0) o 1, debug&>::type operator<<(t h) #define dor > debug& operator<< struct debug { #ifdef DEBUG #define deb debug() ~debug() { cerr << nl; } enif(!=) { cerr << boolalpha << h; ris; } enif(==) { *this << '{'; for (auto a = begin(h), b = end(h); a != b;) *this << *a++ << &" "[a == b]; ris << '}'; } tem, class u dor(pair<t, u> p) { ris << '(' << p.X << ", " << p.Y << ')'; } tem dor(Dump<t> d) { *this << "{\n"; for (t a = d.a, c = a; a != d.b; ++a) *this << " " << distance(c, a) << ": " << *a << nl; ris << '}'; } #else operator int() { return 0; } #define deb 0 and debug() tem dor(t&&) { ris; } #endif }; #define imie(h...) #h ": " << (h) << " " #define LOG(h...) deb << imie(h) #define DOG(h...) deb << #h ": " << dump(h) << " " // End of templates void bucket_sort(VI& v) { VI cnt(1e6 + 1); for (int x : v) ++cnt[x]; FOR (i, 1, (int)cnt.size() - 1) cnt[i] += cnt[i - 1]; VI res(v.size()); for (int x : v) res[cnt[x - 1]++] = x; v = res; } LL count_inv(VI const& a, VI const& b) { LL res = 0; int i = 0, j = 0; while (i < (int)a.size() and j < (int)b.size()) { if (a[i] <= b[j]) ++i; else { ++j; res += (int)a.size() - i; } } return res; } LL merge_sort(VI& v, int beg, int end) { if (beg + 1 >= end) return 0; LL res = 0; int mid = (beg + end) >> 1; res += merge_sort(v, beg, mid); res += merge_sort(v, mid, end); // LL prediction = res + count_inv({v.begin() + beg, v.begin() + mid}, {v.begin() + mid, v.begin() + end}); VI tmp(v.begin() + beg, v.begin() + mid); int i = 0, j = mid, k = beg; for (;;) { if (i == (int)tmp.size()) break; // all is in its place if (j == end) { copy(tmp.begin() + i, tmp.end(), v.begin() + k); break; } if (tmp[i] <= v[j]) v[k++] = tmp[i++]; else { v[k++] = v[j++]; res += (int)tmp.size() - i; } } // assert(prediction == res); return res; } LL count_inv(VI& v) { return merge_sort(v, 0, v.size()); } #include "message.h" #include "teatr.h" // // int GetN() { return 1000; } // int GetN() { return 100*1000*1000; } // int GetElement(int i) { return 1 + (GetN() - i) % 1000000; } PII segment_interval(int segment_id, int segments_no, int n) { int beg = LL(segment_id) * n / segments_no; int end = LL(segment_id + 1) * n / segments_no; return {beg, end}; } VI read_segment(int segment_id, int segments_no, int n) { int beg, end; tie(beg, end) = segment_interval(segment_id, segments_no, n); LOG(MyNodeId()) imie(beg) imie(end); VI v(end - beg); FOR (i, beg, end - 1) v[i - beg] = GetElement(i); return v; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N = GetN(); int non = NumberOfNodes(); int pid = MyNodeId(); // TODO: below segment choosing does not work int k = sqrt(non); int seg_a = (pid / k) % k; int seg_b = pid % k; LOG(pid) imie(seg_a) imie(seg_b) imie(k); if (seg_a > seg_b) swap(seg_a, seg_b); LL res = 0; if (seg_a == seg_b) { auto v = read_segment(seg_a, k, N); // LOG(pid) imie(v); res += count_inv(v); LOG(pid) imie(res); } else { auto a = read_segment(seg_a, k, N); auto b = read_segment(seg_b, k, N); bucket_sort(a); bucket_sort(b); // LOG(pid) imie(a) imie(b); res += count_inv(a, b); LOG(pid) imie(res); } if (pid > 0) { PutInt(0, seg_a); PutInt(0, seg_b); PutLL(0, res); Send(0); return 0; } // pid == 0 V<VLL> results(k, VLL(k)); results[seg_a][seg_b] = res; REP (i, non - 1) { int sender = Receive(-1); int a = GetInt(sender); int b = GetInt(sender); res = GetLL(sender); results[a][b] = res; } res = 0; REP (i, k) FOR (j, i, k - 1) res += results[i][j]; cout << res << nl; 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 | #define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; #define FOR(i,a,n) for (auto i ## __ = (n), i = (a); i <= i ## __; ++i) #define FORD(i,a,n) for (auto i ## __ = (n), i = (a); i >= i ## __; --i) #define REP(i,n) FOR(i,0, n - 1) #define ALL(h) begin(h), end(h) #define EB emplace_back #define X first #define Y second #define V vector #define tpv typedef V< typedef long long LL; typedef pair<int, int> PII; tpv int> VI; tpv VI> VVI; tpv PII> VPII; tpv LL> VLL; constexpr char nl = '\n'; #define endl nl #define ris return *this #define tem template<class t tem, class u> inline void mini(t& a, u&& b) { if (b < a) a = b; } tem, class u> inline void maxi(t& a, u&& b) { if (b > a) a = b; } int ceil2(int h) { return h < 2 ? 1 : 1 << (sizeof(h) * 8 - __builtin_clz(h - 1)); } tem> struct Dump { t a, b; }; tem> auto dump(t&& h) -> Dump<decltype(begin(h))> { return {ALL(h)}; } tem> auto stub(t* h) -> decltype(cerr << *h, 0); tem> char stub(...); #define enif(o) tem> typename enable_if<sizeof stub<t>(0) o 1, debug&>::type operator<<(t h) #define dor > debug& operator<< struct debug { #ifdef DEBUG #define deb debug() ~debug() { cerr << nl; } enif(!=) { cerr << boolalpha << h; ris; } enif(==) { *this << '{'; for (auto a = begin(h), b = end(h); a != b;) *this << *a++ << &" "[a == b]; ris << '}'; } tem, class u dor(pair<t, u> p) { ris << '(' << p.X << ", " << p.Y << ')'; } tem dor(Dump<t> d) { *this << "{\n"; for (t a = d.a, c = a; a != d.b; ++a) *this << " " << distance(c, a) << ": " << *a << nl; ris << '}'; } #else operator int() { return 0; } #define deb 0 and debug() tem dor(t&&) { ris; } #endif }; #define imie(h...) #h ": " << (h) << " " #define LOG(h...) deb << imie(h) #define DOG(h...) deb << #h ": " << dump(h) << " " // End of templates void bucket_sort(VI& v) { VI cnt(1e6 + 1); for (int x : v) ++cnt[x]; FOR (i, 1, (int)cnt.size() - 1) cnt[i] += cnt[i - 1]; VI res(v.size()); for (int x : v) res[cnt[x - 1]++] = x; v = res; } LL count_inv(VI const& a, VI const& b) { LL res = 0; int i = 0, j = 0; while (i < (int)a.size() and j < (int)b.size()) { if (a[i] <= b[j]) ++i; else { ++j; res += (int)a.size() - i; } } return res; } LL merge_sort(VI& v, int beg, int end) { if (beg + 1 >= end) return 0; LL res = 0; int mid = (beg + end) >> 1; res += merge_sort(v, beg, mid); res += merge_sort(v, mid, end); // LL prediction = res + count_inv({v.begin() + beg, v.begin() + mid}, {v.begin() + mid, v.begin() + end}); VI tmp(v.begin() + beg, v.begin() + mid); int i = 0, j = mid, k = beg; for (;;) { if (i == (int)tmp.size()) break; // all is in its place if (j == end) { copy(tmp.begin() + i, tmp.end(), v.begin() + k); break; } if (tmp[i] <= v[j]) v[k++] = tmp[i++]; else { v[k++] = v[j++]; res += (int)tmp.size() - i; } } // assert(prediction == res); return res; } LL count_inv(VI& v) { return merge_sort(v, 0, v.size()); } #include "message.h" #include "teatr.h" // // int GetN() { return 1000; } // int GetN() { return 100*1000*1000; } // int GetElement(int i) { return 1 + (GetN() - i) % 1000000; } PII segment_interval(int segment_id, int segments_no, int n) { int beg = LL(segment_id) * n / segments_no; int end = LL(segment_id + 1) * n / segments_no; return {beg, end}; } VI read_segment(int segment_id, int segments_no, int n) { int beg, end; tie(beg, end) = segment_interval(segment_id, segments_no, n); LOG(MyNodeId()) imie(beg) imie(end); VI v(end - beg); FOR (i, beg, end - 1) v[i - beg] = GetElement(i); return v; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N = GetN(); int non = NumberOfNodes(); int pid = MyNodeId(); // TODO: below segment choosing does not work int k = sqrt(non); int seg_a = (pid / k) % k; int seg_b = pid % k; LOG(pid) imie(seg_a) imie(seg_b) imie(k); if (seg_a > seg_b) swap(seg_a, seg_b); LL res = 0; if (seg_a == seg_b) { auto v = read_segment(seg_a, k, N); // LOG(pid) imie(v); res += count_inv(v); LOG(pid) imie(res); } else { auto a = read_segment(seg_a, k, N); auto b = read_segment(seg_b, k, N); bucket_sort(a); bucket_sort(b); // LOG(pid) imie(a) imie(b); res += count_inv(a, b); LOG(pid) imie(res); } if (pid > 0) { PutInt(0, seg_a); PutInt(0, seg_b); PutLL(0, res); Send(0); return 0; } // pid == 0 V<VLL> results(k, VLL(k)); results[seg_a][seg_b] = res; REP (i, non - 1) { int sender = Receive(-1); int a = GetInt(sender); int b = GetInt(sender); res = GetLL(sender); results[a][b] = res; } res = 0; REP (i, k) FOR (j, i, k - 1) res += results[i][j]; cout << res << nl; return 0; } |