#include <bits/stdc++.h>
#include "message.h"
#include "teatr.h"
using namespace std;
long long tdp[2200007],z;
void add(int gdzie, int pocz, int kon, int x, int ile)
{
if(x==pocz && kon==x)
{
tdp[gdzie]+=ile;
return;
}
int sr=(pocz+kon)/2;
if (x<=sr)
add(2*gdzie,pocz,sr,x,ile);
else
add(2*gdzie+1,sr+1,kon,x,ile);
tdp[gdzie]=tdp[2*gdzie]+tdp[2*gdzie+1];
}
long long query(int gdzie, int pocz,int kon, int x, int y)
{
if(x<=pocz && y>=kon)
{
return tdp[gdzie];
}
int sr=(pocz + kon)/2;
long long wynik=0;
if(x <= sr)
wynik+=query(2*gdzie,pocz,sr,x,y);
if (y > sr)
wynik+=query(2*gdzie,sr + 1,kon,x,y);
return wynik;
}
int main()
{
if(MyNodeId()>0)
return 0;
long long w=0,kon;
int n,a;
n=GetN();
kon=1;
while(kon<n)
{
kon*=2;
}
a=GetElement(0);
add(1,1,kon,a,1);
for(int i=1; i<n; ++i)
{
a=GetElement(i);
w+=query(1,1,kon,a+1,kon);
add(1,1,kon,a,1);
}
printf("%lld",w);
}
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 | #include <bits/stdc++.h> #include "message.h" #include "teatr.h" using namespace std; long long tdp[2200007],z; void add(int gdzie, int pocz, int kon, int x, int ile) { if(x==pocz && kon==x) { tdp[gdzie]+=ile; return; } int sr=(pocz+kon)/2; if (x<=sr) add(2*gdzie,pocz,sr,x,ile); else add(2*gdzie+1,sr+1,kon,x,ile); tdp[gdzie]=tdp[2*gdzie]+tdp[2*gdzie+1]; } long long query(int gdzie, int pocz,int kon, int x, int y) { if(x<=pocz && y>=kon) { return tdp[gdzie]; } int sr=(pocz + kon)/2; long long wynik=0; if(x <= sr) wynik+=query(2*gdzie,pocz,sr,x,y); if (y > sr) wynik+=query(2*gdzie,sr + 1,kon,x,y); return wynik; } int main() { if(MyNodeId()>0) return 0; long long w=0,kon; int n,a; n=GetN(); kon=1; while(kon<n) { kon*=2; } a=GetElement(0); add(1,1,kon,a,1); for(int i=1; i<n; ++i) { a=GetElement(i); w+=query(1,1,kon,a+1,kon); add(1,1,kon,a,1); } printf("%lld",w); } |
English