#include<bits/stdc++.h> #include "teatr.h" #include "message.h" using namespace std; int n; int mynode; vector<int> podzial; vector<int> lewe, prawe; vector<int> srodek; int blok = 0; int naile = 13; const int N = 1<<20; int tab[N*2]; int wyzsi[1000009]; void uptd(int pos){ ++pos; pos+=N; int pos2=N+N-1; while(pos!=pos2){ if(pos%2==1){ ++tab[pos]; pos/=2; ++pos; } else pos/=2; pos2/=2; } ++tab[pos]; return; } int poli(int pos){ pos+=N; int ret=0; while(pos>0){ ret+=tab[pos]; pos/=2; } return ret; } int main(){ n = GetN(); mynode = MyNodeId(); blok = max(blok, (n+naile-1)/naile); if(1LL*blok*naile<n)cerr<<"nonono"; //cout<<mynode<<endl; for(int i=0;i<=naile;++i){ if(i*blok<=n)podzial.push_back(blok*i); else podzial.push_back(n); } if(mynode<naile*(naile-1)/2){ int x=0; int y=1; for(int i=0;i<mynode;++i){ ++y; if(y>=naile){ ++x; y=x+1; } } for(int i = podzial[x]; i < podzial[x+1]; ++i){ int a=GetElement(i); //lewe.push_back(a); ++tab[a]; } for(int i=1000000;i>=0;--i){ wyzsi[i]=wyzsi[i+1]+tab[i+1]; } long long wyn=0; for(int i = podzial[y]; i < podzial[y+1]; ++i){ int a=GetElement(i); wyn+=wyzsi[a]; } if(mynode == 0){ for(int i=1;i<100;++i){ Receive(i); long long addd=GetLL(i); //cerr<<i<<" ! "<<addd<<endl; wyn+=addd; } cout<<wyn<<endl; return 0; } else{ PutLL(0, wyn); Send(0); } return 0; } else{ long long wyn=0; int x=mynode-naile*(naile-1)/2; if(x>=naile){ PutLL(0, wyn); Send(0); return 0; } for(int i = podzial[x+1]-1; i >= podzial[x]; --i){ int a=GetElement(i); //srodek.push_back(a); uptd(a); wyn+=poli(a); } PutLL(0, wyn); Send(0); return 0; } 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 | #include<bits/stdc++.h> #include "teatr.h" #include "message.h" using namespace std; int n; int mynode; vector<int> podzial; vector<int> lewe, prawe; vector<int> srodek; int blok = 0; int naile = 13; const int N = 1<<20; int tab[N*2]; int wyzsi[1000009]; void uptd(int pos){ ++pos; pos+=N; int pos2=N+N-1; while(pos!=pos2){ if(pos%2==1){ ++tab[pos]; pos/=2; ++pos; } else pos/=2; pos2/=2; } ++tab[pos]; return; } int poli(int pos){ pos+=N; int ret=0; while(pos>0){ ret+=tab[pos]; pos/=2; } return ret; } int main(){ n = GetN(); mynode = MyNodeId(); blok = max(blok, (n+naile-1)/naile); if(1LL*blok*naile<n)cerr<<"nonono"; //cout<<mynode<<endl; for(int i=0;i<=naile;++i){ if(i*blok<=n)podzial.push_back(blok*i); else podzial.push_back(n); } if(mynode<naile*(naile-1)/2){ int x=0; int y=1; for(int i=0;i<mynode;++i){ ++y; if(y>=naile){ ++x; y=x+1; } } for(int i = podzial[x]; i < podzial[x+1]; ++i){ int a=GetElement(i); //lewe.push_back(a); ++tab[a]; } for(int i=1000000;i>=0;--i){ wyzsi[i]=wyzsi[i+1]+tab[i+1]; } long long wyn=0; for(int i = podzial[y]; i < podzial[y+1]; ++i){ int a=GetElement(i); wyn+=wyzsi[a]; } if(mynode == 0){ for(int i=1;i<100;++i){ Receive(i); long long addd=GetLL(i); //cerr<<i<<" ! "<<addd<<endl; wyn+=addd; } cout<<wyn<<endl; return 0; } else{ PutLL(0, wyn); Send(0); } return 0; } else{ long long wyn=0; int x=mynode-naile*(naile-1)/2; if(x>=naile){ PutLL(0, wyn); Send(0); return 0; } for(int i = podzial[x+1]-1; i >= podzial[x]; --i){ int a=GetElement(i); //srodek.push_back(a); uptd(a); wyn+=poli(a); } PutLL(0, wyn); Send(0); return 0; } return 0; } |