#include <cstdio> #include "message.h" #include "teatr.h" #define LSB(i) ((i) & -(i)) typedef long long LL; const int N = 1000001; int counts[N]; int tree[N]; void add(int v, int k) { while (k <= N) { tree[k] += v, k += LSB(k); } } int get(int k) { int sum = 0; while (k > 0) sum += tree[k], k -= LSB(k); return sum; } int main() { LL n = GetN(); LL id = MyNodeId(); LL non = NumberOfNodes(); LL beg = id*n/non; LL end = (id+1)*n/non; for(int i = end; i < n; ++i) { ++counts[GetElement(i)]; } for(int i = 1; i < N; ++i) add(counts[i], i); LL result = 0; for(int i = end-1; i >= beg; --i) { int k = GetElement(i); result += get(k-1); add(1, k); } PutLL(0, result); Send(0); if(id == 0) { result = 0; for(int i = 0; i < non; ++i) { int k = Receive(-1); result += GetLL(k); } printf("%lld\n", result); } }
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 | #include <cstdio> #include "message.h" #include "teatr.h" #define LSB(i) ((i) & -(i)) typedef long long LL; const int N = 1000001; int counts[N]; int tree[N]; void add(int v, int k) { while (k <= N) { tree[k] += v, k += LSB(k); } } int get(int k) { int sum = 0; while (k > 0) sum += tree[k], k -= LSB(k); return sum; } int main() { LL n = GetN(); LL id = MyNodeId(); LL non = NumberOfNodes(); LL beg = id*n/non; LL end = (id+1)*n/non; for(int i = end; i < n; ++i) { ++counts[GetElement(i)]; } for(int i = 1; i < N; ++i) add(counts[i], i); LL result = 0; for(int i = end-1; i >= beg; --i) { int k = GetElement(i); result += get(k-1); add(1, k); } PutLL(0, result); Send(0); if(id == 0) { result = 0; for(int i = 0; i < non; ++i) { int k = Receive(-1); result += GetLL(k); } printf("%lld\n", result); } } |