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
// Jedna instancja wypisuje maksymalny wzrost.

#include "message.h"
#include "teatr.h"
#include "bits/stdc++.h"

using namespace std;

#define SIZE 1000000
//#define SIZE 3
#define jebac_to exit

int ele[SIZE];

void o_kurwa() {
	if (MyNodeId() != 0)
		jebac_to(0);

	int n = GetN();
	long long wyn = 0;
	map<int, int> mn;
	for (int i = 0; i < n; i++) {
		int el = GetElement(i)-1;
		for (auto i = mn.upper_bound(el); i != mn.end(); i++)
			wyn += i->second;
		mn[el]++;
	}

	printf("%lld", wyn);
	
	// jeśli mamy szczęście to możemy
	jebac_to(0);
}

int main() {
	int node = MyNodeId();
	int n = GetN();
	int mn[5] = {0,0,0,0,0};
	long long wyn = 0;

	for (int i = node*SIZE; i < (node+1)*SIZE && i < n; i++) {
		int el = GetElement(i)-1;
		if (el >= 5)
			o_kurwa();
		for (int i = el+1; i < 5; i++)
			wyn += mn[i];
		mn[el]++;
		ele[i-(node*SIZE)] = el;
	}

	int mnn[5];
	memcpy(mnn, mn, sizeof(int)*5);

	if (node != 0) {
		Receive(node-1);
		for (int i = 0; i < 5; i++) {
			long long val = GetLL(node-1);
			for (int j = 0; j < i; j++) {
				wyn += val * mn[j];
			}
			mnn[i] += val;
		}
		wyn += GetLL(node-1);
	}

	if (node != 99) {
		for (int i = 0; i < 5; i++)
			PutLL(node+1, mnn[i]);
		PutLL(node+1, wyn);
		Send(node+1);
	} else
		printf("%lld\n", wyn);
}