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

const int tree_size = 16384;
int higher;
int rt[2 * tree_size];
long long result;

void insert(int crd)
{
	crd+=tree_size;
	for(; crd!=1; crd/=2)
		rt[crd]++;
}

int query(int low, int high)
{
	low+=tree_size; high+=tree_size;
	int result=rt[low];
	if(low!=high)result+=rt[high];
	for(; low/2!=high/2; low/=2, high/=2)
	{
		if(!(low%2)) result+=rt[low+1];
		if(high%2) result+=rt[high-1];
	}
	return result;
}


int main()
{
    int node_id = MyNodeId();
    const int min_height = node_id * 10000 +1;
    const int max_height = min_height + 10000;
    int size = GetN();

    for(int i = 0; i < size; ++i)
    {
        int h = GetElement(i);

        if(h >= max_height) ++higher;
        else if(h >= min_height && h < max_height)
        {
            result += higher + query(h - min_height+1, max_height-min_height);
            insert(h - min_height);
        }
    }
    if (node_id == 0)
    {
        for(int i = 1; i < 100; ++i)
        {
            Receive(i);
            result += GetLL(i);
        }
        printf("%lld\n", result);
    }
    else
    {
        PutLL(0, result);
        Send(0);
    }

    return 0;
}