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>
using namespace std;

#include "message.h"
#include "teatr.h"

const int x = 1 << 20;
int tree[x<<1];

int main() {
	int mni = MyNodeId();
	int non = NumberOfNodes();
	int n = GetN();

	int step = n / non;

	int first = mni * step;
	int last = (mni + 1) * step; if (mni == non-1) last = n;

	for (int i=0; i<first; ++i) {
		++tree[x + GetElement(i)];
	}

	for (int i=x-1; i>0; --i)
		tree[i] = tree[i<<1] + tree[(i<<1)+1];

	long long res = 0;
	for (int i=first; i<last; ++i) {
		int v = GetElement(i);
		int a = v + x;
		while (a > 1) {
			++tree[a];
			a >>= 1;
		}
		int b = v + 1 + x;
		while (b > 1) {
			if (b & 1) {
				res += tree[b];
				++b;
			}
			b >>= 1;
		}
	}

	if (mni > 0) {
		PutLL(0, res);
		Send(0);
		return 0;
	}

	for (int i=1; i<non; ++i) {
		res += GetLL(Receive(-1));
	}

	printf("%lld\n", res);

	return 0;
}