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
#include "message.h"
#include "teatr.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
typedef pair<int,int> pii;
typedef long long ll;
typedef double ld;
typedef vector<int> vi;
#define fi first
#define se second
#define fe first
#define SZ 1000555
ll bs[SZ];
int main()
{
	ll n=GetN(),x=13,m=MyNodeId(),qz[233];
	qz[0]=0;
	for(int i=1;i<=x;++i) qz[i]=n/x*i;
	qz[x]=n;
	//[qz[i],qz[i+1])
	map<pii,int> al; int G=0;
	al[pii(-2,-2)]=0;
	for(int i=0;i<x;++i) al[pii(i,i)]=++G;
	for(int i=0;i<x;++i)
		for(int j=i+1;j<x;++j)
			al[pii(i,j)]=++G;
	int a=-1,b=-1;
	for(auto t:al)
		if(t.se==m) a=t.fi.fi,b=t.fi.se;
	if(a==-1) return 0;
	if(a==-2) //receiver
	{
		ll su=0;
		for(int i=1;i<=G;++i)
			Receive(i),su+=GetLL(i);
		cout<<su<<"\n";
		return 0;
	}
	if(a==b)
	{
		ll su=0;
		for(int g=qz[a+1]-1;g>=qz[a];--g)
		{
			int v=GetElement(g);
			for(int t=v-1;t>=1;t-=t&-t) su+=bs[t];
			for(int t=v;t<SZ;t+=t&-t) ++bs[t];
		}
		PutLL(0,su),Send(0);
		return 0;
	}
	if(a!=b)
	{
		ll su=0;
		for(int g=qz[b+1]-1;g>=qz[b];--g)
			++bs[GetElement(g)];
		for(int i=1;i<SZ;++i) bs[i]+=bs[i-1];
		for(int g=qz[a+1]-1;g>=qz[a];--g)
			su+=bs[GetElement(g)-1];
		PutLL(0,su),Send(0);
		return 0;
	}
}