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
#include "message.h"
#include "teatr.h"
#include "bits/stdc++.h"

using namespace std;

typedef long long int LL;

const int N = 1e6 + 7;
const int T = (1 << 20) + 7;

int n, id;
int pref[N];
int tree[T + T + 7];

int ask(int from, int to){
	from += T;
	to += T;
	
	int ret = 0;
	while(from < to){
		if(from & 1)
			ret += tree[from];
		if(!(to & 1))
			ret += tree[to];
		
		from = (from + 1) >> 1;
		to = (to - 1) >> 1;
	}
	
	if(from == to)
		ret += tree[to];
	return ret;
}

void update(int p){
	p += T;
	while(p){
		tree[p]++;
		p >>= 1;
	}
}

int main(){
	id = MyNodeId();
	n = GetN();
	
	if(n < 100){
		if(id != 0)
			return 0;
		
		int ans = 0;
		for(int i = 0; i < n; ++i)
			for(int j = i + 1; j < n; ++j)
				ans += GetElement(i) > GetElement(j);
		
		printf("%d\n", ans);
		return 0;
	}
	
	int sz = n / 100;
	int from = sz * id, to = sz * (id + 1);
	if(id == 99)
		to = max(to, n);
	
	for(int i = 0; i < from; ++i)
		pref[GetElement(i)]++;
	
	for(int i = 1; i < N; ++i)
		pref[i] += pref[i - 1];
	
	LL ans = 0;
	for(int i = from; i < to; ++i){
		int v = GetElement(i);
		ans += pref[N - 1] - pref[v];

		ans += ask(v + 1, N - 1);
		update(v);
	}
	
	if(id > 0){
		PutLL(0, ans);
		Send(0);
		return 0;
	}
	
	for(int i = 1; i < 100; ++i){
		Receive(i);
		ans += GetLL(i);
	}
	
	printf("%lld\n", ans);
	return 0;
}