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
#include "message.h"
#include "teatr.h"
#include "bits/stdc++.h"
#define lowbit(x) ((x)&-(x))
using namespace std;
using i64=long long;
const int D=30,M=505,C=20,V=1e6+5;
int n,m,ov[M],in[M],Q,num;i64 sum;
struct obs{
	int op,bl,br;
	i64 work(){
		int l1,r1,l2,r2,x,y,v;i64 res=0;
		l1=(i64)n*(bl-1)/D,r1=(i64)n*bl/D-1;
		l2=(i64)n*(br-1)/D,r2=(i64)n*br/D-1;
		if(l1>r1||l2>r2)return 0;
		if(op==0){
			vector<int>bit(V);
			for(x=r1;x>=l1;--x){
				v=GetElement(x);
				for(y=v;y<V;y+=lowbit(y))++bit[y];
				for(--v;v;v-=lowbit(v))res+=bit[v];
			}
		}else{
			vector<int>cnt(V);
			for(x=l2;x<=r2;++x)++cnt[GetElement(x)];
			for(x=1;x<=V;++x)cnt[x]+=cnt[x-1];
			for(x=l1;x<=r1;++x)res+=cnt[GetElement(x)-1];
		}
		return res;
	}
}os[M];
int main() {
	int i,j,cnt=NumberOfNodes();
	n=GetN();
	for(i=1;i<=D;++i)os[++m].op=0,os[m].bl=os[m].br=i,ov[m]=ov[m-1]+C;
	for(i=1;i<=D;++i)for(j=i+1;j<=D;++j)os[++m].op=1,os[m].bl=i,os[m].br=j,ov[m]=ov[m-1]+1;
	Q=ov[m]/cnt+1;
	for(i=1,j=0;i<=m;++i)if(ov[i]-ov[j]>Q)in[j=i]=num++;else in[i]=num;
	for(i=1;i<=m;++i)if(in[i]==MyNodeId())sum+=os[i].work();
	if(!MyNodeId()){
		for(i=1;i<cnt;++i)Receive(i),sum+=GetLL(i);
		printf("%lld\n",sum);
	}else PutLL(0,sum),Send(0);
}