#include "message.h" #include "teatr.h" #include "bits/stdc++.h" #define ll long long using namespace std; const int M = 1 << 20; struct Tree{ int t[2 * M + 7]; vector<int> changed; void add(int v){ v += M; changed.push_back(v); t[v]++; while(v != 1){ v /= 2; t[v] = t[2 * v] + t[2 * v + 1]; changed.push_back(v); } } int sum(int a, int b){ a += M; b += M; int s = t[a]; if(a != b) s += t[b]; while(a / 2 != b / 2){ if(a % 2 == 0) s += t[a + 1]; if(b % 2 == 1) s += t[b - 1]; a /= 2; b /= 2; } return s; } void make_empty(){ for(int i = 0; i < changed.size(); i++) t[changed[i]] = 0; changed.clear(); } }; int main() { int N = GetN(), potN = 1, licz = 0; while(potN < N){ potN *= 2; licz++; } int v = MyNodeId(), myN = potN; for(int i = 0; i < v; i++) myN /= 2; if(myN != 0){ Tree* drzewo = new Tree(); ll wyn = 0; for(int i = 0; i < potN; i += myN){ //cout << "Przegladam [" << i << ", " << i + myN - 1 << "]" << endl; /// zalatwiam przedzial [i, i + myN - 1]: dziele go na dwa i licze na drzewie int pol = i + (myN - 1) / 2; /// teraz mam p in [i, pol], q in [pol + 1, i + myN - 1] for(int q = pol + 1; q <= min(i + myN - 1, N - 1); q++){ (*drzewo).add(GetElement(q)); } for(int p = i; p <= min(pol, N - 1); p++){ int w = GetElement(p); wyn += (ll) (*drzewo).sum(0, w - 1); } (*drzewo).make_empty(); } if(v > 0){ PutLL(0, wyn); Send(0); } else{ for(int i = 1; i <= licz; i++){ Receive(i); wyn += GetLL(i); } cout << wyn << '\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 | #include "message.h" #include "teatr.h" #include "bits/stdc++.h" #define ll long long using namespace std; const int M = 1 << 20; struct Tree{ int t[2 * M + 7]; vector<int> changed; void add(int v){ v += M; changed.push_back(v); t[v]++; while(v != 1){ v /= 2; t[v] = t[2 * v] + t[2 * v + 1]; changed.push_back(v); } } int sum(int a, int b){ a += M; b += M; int s = t[a]; if(a != b) s += t[b]; while(a / 2 != b / 2){ if(a % 2 == 0) s += t[a + 1]; if(b % 2 == 1) s += t[b - 1]; a /= 2; b /= 2; } return s; } void make_empty(){ for(int i = 0; i < changed.size(); i++) t[changed[i]] = 0; changed.clear(); } }; int main() { int N = GetN(), potN = 1, licz = 0; while(potN < N){ potN *= 2; licz++; } int v = MyNodeId(), myN = potN; for(int i = 0; i < v; i++) myN /= 2; if(myN != 0){ Tree* drzewo = new Tree(); ll wyn = 0; for(int i = 0; i < potN; i += myN){ //cout << "Przegladam [" << i << ", " << i + myN - 1 << "]" << endl; /// zalatwiam przedzial [i, i + myN - 1]: dziele go na dwa i licze na drzewie int pol = i + (myN - 1) / 2; /// teraz mam p in [i, pol], q in [pol + 1, i + myN - 1] for(int q = pol + 1; q <= min(i + myN - 1, N - 1); q++){ (*drzewo).add(GetElement(q)); } for(int p = i; p <= min(pol, N - 1); p++){ int w = GetElement(p); wyn += (ll) (*drzewo).sum(0, w - 1); } (*drzewo).make_empty(); } if(v > 0){ PutLL(0, wyn); Send(0); } else{ for(int i = 1; i <= licz; i++){ Receive(i); wyn += GetLL(i); } cout << wyn << '\n'; } } return 0; } |