#include<bits/stdc++.h> using namespace std; #define FOR(i, a, b) for(int i = a; i <= b; i++) #define REP(i, a) FOR(i, 0, a - 1) #define ST first #define ND second #define V vector #define RS resize #define EB emplace_back #define ALL(a) a.begin(), a.end() #define S(a) (int)a.size() template<class T> void db(T a) { cerr << a; } template<class L, class R> void db(pair<L, L> a) { cerr << "(" << a.ST << ", " << a.ND << ")"; } template<class T> void db(V<T> v) { cerr << "{"; REP(i, S(v)) cerr << (i != 0 ? ", " : ""), db(v[i]); cerr << "}"; } template<class T> void dump(const char *s, T a) { cerr << s << ": "; db(a); cerr << "\n"; } template<class T, class... TS> void dump(const char *s, T a, TS... x) { while(*s != ',') cerr<< *s++; cerr << ": "; db(a); dump(s + 1, x...); } #define DEBUG #ifdef DEBUG #define DB(...) dump(#__VA_ARGS__, __VA_ARGS__); #else #define DB(...) #endif using LL = long long; using PII = pair<int, int>; using VI = V<int>; using VLL = V<LL>; using VVI = V<VI>; using VPII = V<PII>; #include "message.h" #include "teatr.h" // int GetN() { return 100000000; } // int GetElement(int i) { return i % int(1e6) + 1; } struct Tree { VI T; int size; Tree(int n) { size = 1; while(size < n) size *= 2; T.RS(size * 2); } void add(int pos) { pos += size; while(pos > 1) { T[pos]++; pos /= 2; } } int que(int pos) { pos += size; int ans = T[pos]; int end = 2 * size - 1; while(pos + 1 < end) { if(pos % 2 == 0) ans += T[pos + 1]; ans += T[end - 1]; end /= 2, pos /= 2; } return ans; } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n = GetN(); int id = MyNodeId(); int node_count = NumberOfNodes(); int d = n / node_count; int x = n - d * node_count; int a = -1, b = -1; REP(i, id + 1) { a = b + 1; b = a + d - (x <= 0); x--; } int s = b - a + 1; if(s == 0) return 0; VI v(s); Tree tree(1e6 + 1); LL inv = 0; REP(i, s) { v[i] = GetElement(a + i); inv += tree.que(v[i] + 1); tree.add(v[i]); } VI c(1e6 + 2); for(int i = 1e6; i >= 1; i--) c[i] = tree.T[i + tree.size] + c[i + 1]; FOR(i, b + 1, n - 1) inv += c[GetElement(i) + 1]; int last = min(n, node_count) - 1; if(id == last) { for(int i = last - 1; i >= 0; i--) { Receive(i); inv += GetLL(i); } cout << inv << "\n"; } else { PutLL(last, inv); Send(last); } }
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 | #include<bits/stdc++.h> using namespace std; #define FOR(i, a, b) for(int i = a; i <= b; i++) #define REP(i, a) FOR(i, 0, a - 1) #define ST first #define ND second #define V vector #define RS resize #define EB emplace_back #define ALL(a) a.begin(), a.end() #define S(a) (int)a.size() template<class T> void db(T a) { cerr << a; } template<class L, class R> void db(pair<L, L> a) { cerr << "(" << a.ST << ", " << a.ND << ")"; } template<class T> void db(V<T> v) { cerr << "{"; REP(i, S(v)) cerr << (i != 0 ? ", " : ""), db(v[i]); cerr << "}"; } template<class T> void dump(const char *s, T a) { cerr << s << ": "; db(a); cerr << "\n"; } template<class T, class... TS> void dump(const char *s, T a, TS... x) { while(*s != ',') cerr<< *s++; cerr << ": "; db(a); dump(s + 1, x...); } #define DEBUG #ifdef DEBUG #define DB(...) dump(#__VA_ARGS__, __VA_ARGS__); #else #define DB(...) #endif using LL = long long; using PII = pair<int, int>; using VI = V<int>; using VLL = V<LL>; using VVI = V<VI>; using VPII = V<PII>; #include "message.h" #include "teatr.h" // int GetN() { return 100000000; } // int GetElement(int i) { return i % int(1e6) + 1; } struct Tree { VI T; int size; Tree(int n) { size = 1; while(size < n) size *= 2; T.RS(size * 2); } void add(int pos) { pos += size; while(pos > 1) { T[pos]++; pos /= 2; } } int que(int pos) { pos += size; int ans = T[pos]; int end = 2 * size - 1; while(pos + 1 < end) { if(pos % 2 == 0) ans += T[pos + 1]; ans += T[end - 1]; end /= 2, pos /= 2; } return ans; } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n = GetN(); int id = MyNodeId(); int node_count = NumberOfNodes(); int d = n / node_count; int x = n - d * node_count; int a = -1, b = -1; REP(i, id + 1) { a = b + 1; b = a + d - (x <= 0); x--; } int s = b - a + 1; if(s == 0) return 0; VI v(s); Tree tree(1e6 + 1); LL inv = 0; REP(i, s) { v[i] = GetElement(a + i); inv += tree.que(v[i] + 1); tree.add(v[i]); } VI c(1e6 + 2); for(int i = 1e6; i >= 1; i--) c[i] = tree.T[i + tree.size] + c[i + 1]; FOR(i, b + 1, n - 1) inv += c[GetElement(i) + 1]; int last = min(n, node_count) - 1; if(id == last) { for(int i = last - 1; i >= 0; i--) { Receive(i); inv += GetLL(i); } cout << inv << "\n"; } else { PutLL(last, inv); Send(last); } } |