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

int me, nodes, n;
int low;
int high;

int c[5];

int main() {
    n = GetN();
    nodes = NumberOfNodes();
    me = MyNodeId();
    if (me >= n) {
        return 0;
    }
    nodes = std::min(n, nodes);
    low = n * me / nodes;
    high = std::min(n, n * (me + 1) / nodes);
    long long res = 0;
    for (int i = low ; i < high ; i++) {
        int x = GetElement(i);
        c[x - 1]++;
        for (int j = x ; j < 5 ; j++) res += c[j];
    }
    if (me != 0) {
        Receive(me - 1);
        long long res_prev = GetLL(me - 1);
        res += res_prev;
        int c_prev[5];
        for (int i = 0 ; i < 5 ; i++) {
            c_prev[i] = GetInt(me - 1);
        }
        for (int i = 0 ; i < 5 ; i++)  {
            for (int j = i + 1 ; j < 5 ; j++) {
                res += (long long)c_prev[j] * c[i];
            }
        }
        for (int i =0  ; i < 5 ; i++) c[i] += c_prev[i];
    }
    if (me != nodes - 1) {
        PutLL(me + 1, res);
        for (int i = 0  ; i < 5 ;i++) {
            PutInt(me + 1, c[i]);
        }
        Send(me + 1);
    }
    if (me == nodes - 1) {
        std::cout << res;
    }
}