#include<cstdio>
#include<algorithm>
#include "message.h"
#include "teatr.h"
using namespace std;
//const int inp[] = {5,2,4,4,3};
int LA[20000000],LB[20000000],_node,N;
long long PT[2000000];
int get(int pos)
{
if(pos==0)return PT[pos];
else return PT[pos]+get(pos-(pos&(-pos)));
}
void add(int pos)
{
if(pos>=2000000)return;
else PT[pos]++,add(pos+(pos&(-pos)));
}
/*int GetN()
{
return 5;
}
int GetElement(int x)
{
return inp[x];
}*/
int main()
{
_node=MyNodeId();
int ILE=13;
N=GetN();
if(_node==0)
{
long long wyn=0;
for(int i=1;i<100;i++)
{
Receive(i);
long long X=GetInt(i);
long long D=GetInt(i);
wyn+=D*1000000000ll+X;
}
printf("%lld\n",wyn);
}
else
{
int A=0;
int B=0;
//mam downa nie umiem lepiej xd
for(int i=1;i<_node;i++)
{
B++;
if(B==ILE)A++,B=A;
}
if(A>=ILE)
{
PutInt(0,0);
PutInt(0,0);
Send(0);
return 0;
}
int dl=N/ILE;
int pA=A*dl;
int kA=(A+1)*dl-1;if(A==ILE-1)kA=N-1;
int pB=B*dl;
int kB=(B+1)*dl-1;if(B==ILE-1)kB=N-1;
for(int i=0;i<=(kA-pA);i++)LA[i]=GetElement(pA+i);
for(int i=0;i<=(kB-pB);i++)LB[i]=GetElement(pB+i);
long long wyn=0;
if(B==A)
{
//printf("%d %d\n",pA,kA);
for(int i=0;i<=(kA-pA);i++)
{
LA[i]=1000001-LA[i];
//printf("%d\n",LA[i]);
wyn+=get(LA[i]-1);
add(LA[i]);
}
}
else
{
sort(LA,LA+kA-pA+1);
sort(LB,LB+kB-pB+1);
int mx=0;
for(int i=0;i<=kB-pB;i++)
{
while(mx<=kA-pA&&LA[mx]<=LB[i])mx++;
wyn+=kA-pA-mx+1;
}
}
PutInt(0,wyn%1000000000);
PutInt(0,wyn/1000000000);
Send(0);
//printf("%lld\n",wyn);
}
}
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 | #include<cstdio> #include<algorithm> #include "message.h" #include "teatr.h" using namespace std; //const int inp[] = {5,2,4,4,3}; int LA[20000000],LB[20000000],_node,N; long long PT[2000000]; int get(int pos) { if(pos==0)return PT[pos]; else return PT[pos]+get(pos-(pos&(-pos))); } void add(int pos) { if(pos>=2000000)return; else PT[pos]++,add(pos+(pos&(-pos))); } /*int GetN() { return 5; } int GetElement(int x) { return inp[x]; }*/ int main() { _node=MyNodeId(); int ILE=13; N=GetN(); if(_node==0) { long long wyn=0; for(int i=1;i<100;i++) { Receive(i); long long X=GetInt(i); long long D=GetInt(i); wyn+=D*1000000000ll+X; } printf("%lld\n",wyn); } else { int A=0; int B=0; //mam downa nie umiem lepiej xd for(int i=1;i<_node;i++) { B++; if(B==ILE)A++,B=A; } if(A>=ILE) { PutInt(0,0); PutInt(0,0); Send(0); return 0; } int dl=N/ILE; int pA=A*dl; int kA=(A+1)*dl-1;if(A==ILE-1)kA=N-1; int pB=B*dl; int kB=(B+1)*dl-1;if(B==ILE-1)kB=N-1; for(int i=0;i<=(kA-pA);i++)LA[i]=GetElement(pA+i); for(int i=0;i<=(kB-pB);i++)LB[i]=GetElement(pB+i); long long wyn=0; if(B==A) { //printf("%d %d\n",pA,kA); for(int i=0;i<=(kA-pA);i++) { LA[i]=1000001-LA[i]; //printf("%d\n",LA[i]); wyn+=get(LA[i]-1); add(LA[i]); } } else { sort(LA,LA+kA-pA+1); sort(LB,LB+kB-pB+1); int mx=0; for(int i=0;i<=kB-pB;i++) { while(mx<=kA-pA&&LA[mx]<=LB[i])mx++; wyn+=kA-pA-mx+1; } } PutInt(0,wyn%1000000000); PutInt(0,wyn/1000000000); Send(0); //printf("%lld\n",wyn); } } |
English