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
#include <bits/stdc++.h>
#include "message.h"
#include "teatr.h"
using namespace std;
#define VAR(n,v) decltype(v) n=(v)
#define REP(i,n) for(int i=1; i<=(n); ++i)
#define FOR(i,a,b) for(VAR(i,a); i<(b); ++i)
#define FORE(it,t) FOR(it,t.begin(),t.end())
typedef vector<int> vi;
#define pb push_back
typedef pair<int,int> pii;
#define mp make_pair
#define st first
#define nd second
typedef long long ll;
#define INF 1000000001
#define sz size()
#define ALL(t) (t).begin(),(t).end()
#define SC(a) scanf("%d", &a)
#define GET(a) int a; SC(a)
#define ISDEBUG 1
#define dprintf(...) if(ISDEBUG) \
	{printf("\033[31m"); printf(__VA_ARGS__); printf("\033[0m");}
template <class It> void dptab(It b, It e, const char* f="%d ")
	{if(ISDEBUG) {for(It it=b; it!=e; ++it) dprintf(f, *it); dprintf("\n"); }}


ll result;
int counter[5];

int main() {
	int n = GetN();

	int node = MyNodeId();
	int begin = 1000000*MyNodeId();
	int end = min(n, begin + 1000000);

	FOR(i,begin,end) {
		int x = GetElement(i) - 1;
		if(x >= 5)
			return 1;
		counter[x]++;
		FOR(j,x+1,5)
			result += counter[j];
	}

	if(node != 0) {
		//get message
		int prev_counter[5];
		Receive(node - 1);
		ll prev_result = GetLL(node - 1);
		FOR(i,0,5)
			prev_counter[i] = GetInt(node - 1);

		result += prev_result;
		FOR(i,0,5)
			FOR(j,i+1,5)
				result += (ll) counter[i] * prev_counter[j];
		FOR(i,0,5)
			counter[i] += prev_counter[i];
	}

	if(node != 99) {
		//prepare message
		PutLL(node + 1, result);
		FOR(i,0,5)
			PutInt(node + 1, counter[i]);
		Send(node + 1);
	}

	if(node == 99) {
		printf("%lld\n", result);
	}
	return 0;
}