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
#include <iostream>
#include <cstdio>
#include "teatr.h"
#include "message.h"
using namespace std;
const int N = 1e6+5, L = 1e6, D = (1<<21), pods = (1<<20) - 1;
int n, cnt[N], p, k, d[D], a;
void add(int x)
{
	x += pods;
	while(x)
	{
		d[x]++;
		x/=2;
	}
}
int suma(int x, int y)
{
	x+=pods;
	y+=pods;
	int ans = d[x];
	if(x!=y) ans += d[y];
	while(x/2!=y/2)
	{
		if(x%2==0) ans+=d[x+1];
		if(y%2==1) ans+=d[y-1];
		x/=2;
		y/=2;
	}
	return ans;
}
int main()
{
	n = GetN();
	int nr = MyNodeId();
	if(nr==0)
	{
		for(int i=0;i<n;i++) cnt[GetElement(i)]++;
		int sum = 0;
		int it = 1;
		p = 1;
		for(int i=1;i<=L;i++)
		{
			if(sum >= L) 
			{
				if(it==100) break;
				PutInt(it, p);
				PutInt(it, i - 1);
				Send(it);
				it++;
				sum = 0;
				p = i;
			}
			sum += cnt[i];
		}
		k = L;
		while(it!=100)
		{
			PutInt(it, L+1);
			PutInt(it, L);
			Send(it);
			it++;
		}
	}
	else
	{
		Receive(0);
		p = GetInt(0);
		k = GetInt(0);
	}
	int dod = 0;
	long long ans = 0;
	while(n--)
	{
		a = GetElement(n);
		if(a < p) dod++;
		else if(a <= k)
		{
			ans += dod;
			if(a!=p) ans += suma(p, a - 1);
			if(a!=k) add(a);
		}
	}
	if(nr!=99)
	{
		PutLL(99, ans);
		Send(99);
	}
	else
	{
		for(int i=0;i<99;i++)
		{
			Receive(i);
			ans += GetLL(i);
		}
		printf("%lld", ans);
	}
	return 0;
}