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
#include "message.h"
#include "teatr.h"
#include <cstdio>

const int U = 1<<20;

int e[U+U];

int main() {
	const int my_node_id = MyNodeId();
	const int num_nodes = NumberOfNodes();
	const int n = GetN();
  const int begin = ((long long)n) * my_node_id / num_nodes;
	const int end = ((long long)n) * (my_node_id + 1) / num_nodes;
	for (int i = 0; i < begin; i++)
		e[U + GetElement(i)]++;
	for (int i = U; i > 0; i--) {
	  e[i] = e[i+i] + e[i+i+1];
	}
	long long ret = 0;
	for (int i = begin; i < end; i++) {
		for (int x = GetElement(i) + U; x; x/=2) {
			if (x % 2 == 0) ret += e[x+1];	
		  e[x]++;
		}
	}
	if (my_node_id) {
		PutLL(0, ret);
		Send(0);
	} else {
		for (int i = 1; i < num_nodes; i++) {
		  int x = Receive(-1);
			ret += GetLL(x);
		}
		printf("%lld\n", ret);
	}
	return 0;
}