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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include "message.h"
#include "teatr.h"

#define FOR(a, b, c) for(int a = b; a < c; ++a)

typedef long long LL;

using namespace std;

const int H = 1 << 20;
const int MAXVAL = 1000007;

int segment_tree[H << 1];

void insertTree(int v)
{
	++segment_tree[v];

	while(v > 1)
	{
		v >>= 1;
		segment_tree[v] = segment_tree[v << 1] + segment_tree[1 + (v << 1)];
	}
}

int sumTree(int b, int e)
{
	int result = 0;

	while(b < e)
	{
		if(b & 1)
		{
			result += segment_tree[b];
			++b;
		}

		if(!(e & 1))
		{
			result += segment_tree[e];
			--e;
		}

		b >>= 1;
		e >>= 1;
	}

	if(b == e)
	{
		result += segment_tree[b];
	}

	return result;
}

void updateTree()
{
	for(int i = H - 1; i > 0; --i)
	{
		segment_tree[i] = segment_tree[i << 1] + segment_tree[1 + (i << 1)];
	}
}

int main()
{
	int id = MyNodeId();

	int n = GetN();

	int start = 1000000 * id;
	int end = min(n, start + 1000000);

	LL result = 0;

	FOR(i, 0, n)
	{
		int a = GetElement(i);

		if(i == start)
		{
			updateTree();
		}

		if(i >= start && i < end)
		{
			LL temp = sumTree(a + H + 1, MAXVAL + H);
			result += temp;
			insertTree(a + H);
		}
		else
		{
			++segment_tree[H + a];
		}
	}

	if(id == 0)
	{
		FOR(i, 1, 100)
		{
			Receive(i);

			result += GetLL(i);
		}

		cout << result;
	}
	else
	{
		PutLL(0, result);
		Send(0);
	}

	return 0;
}