#include "message.h" #include "teatr.h" #include<bits/stdc++.h> using namespace std; int n, node, k, temp; long long answ; long long sbmt; const int powlee = 1024*1024-1; long long drzewo[1024*1024*2+10]; void dodaj(int x) { x+=powlee; while(x>0) { drzewo[x]+=1; x/=2; } } long long licz(int p, int k) { long long reto = 0; p+=powlee; k+=powlee; p--; k++; while(p/2!=k/2){ if(p%2==0) { reto+=drzewo[p+1]; } if(k%2==1) { reto+=drzewo[k-1]; } p/=2;k/=2; } return reto; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); n = GetN(); node = MyNodeId(); k = min(n, (node+1)*1000000); for(int i = node*1000000; i < k; i++) { temp = GetElement(i); answ += licz(temp+1, 1000000); dodaj(temp); } for(int i = k; i<n; i++) { temp = GetElement(i); answ += licz(temp+1, 1000000); } if(node != 0) { PutLL(0, answ); Send(0); } else { for (int i = 1; i < NumberOfNodes(); i++) { Receive(i); long long v = GetLL(i); sbmt+=v; } sbmt+=answ; cout << sbmt; } }
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 | #include "message.h" #include "teatr.h" #include<bits/stdc++.h> using namespace std; int n, node, k, temp; long long answ; long long sbmt; const int powlee = 1024*1024-1; long long drzewo[1024*1024*2+10]; void dodaj(int x) { x+=powlee; while(x>0) { drzewo[x]+=1; x/=2; } } long long licz(int p, int k) { long long reto = 0; p+=powlee; k+=powlee; p--; k++; while(p/2!=k/2){ if(p%2==0) { reto+=drzewo[p+1]; } if(k%2==1) { reto+=drzewo[k-1]; } p/=2;k/=2; } return reto; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); n = GetN(); node = MyNodeId(); k = min(n, (node+1)*1000000); for(int i = node*1000000; i < k; i++) { temp = GetElement(i); answ += licz(temp+1, 1000000); dodaj(temp); } for(int i = k; i<n; i++) { temp = GetElement(i); answ += licz(temp+1, 1000000); } if(node != 0) { PutLL(0, answ); Send(0); } else { for (int i = 1; i < NumberOfNodes(); i++) { Receive(i); long long v = GetLL(i); sbmt+=v; } sbmt+=answ; cout << sbmt; } } |