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
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>

#include "teatr.h"
#include "message.h"

int get_start(int N, int nodes, int node_id) {
    int segment_size = N/nodes;
    
    return node_id * segment_size;
}

int get_end(int N, int nodes, int node_id) {
    if (node_id == nodes - 1) {
        return N;
    }
    
    int segment_size = N/nodes;
    
    return node_id * segment_size + segment_size;
}

int main() {
    int N = GetN();
    int nodes = NumberOfNodes();
    int node_id = MyNodeId();
    int counts[6] = {0, 0, 0, 0, 0, 0};
    long long result = 0;
    
//     printf("%d %d %d\n", node_id, get_start(N, nodes, node_id), get_end(N, nodes, node_id));
    
    for (int i = get_start(N, nodes, node_id); i < get_end(N, nodes, node_id); i++) {
        int height = GetElement(i);
        
        counts[height]++;
        
        for (int j = height + 1; j < 6; j++) {
            result += counts[j];
        }
        
//         for (int j = 0; j < 6; j++) {
//             printf("%d ", counts[j]);
//         }
//         puts("");
    }
    
    if (node_id < nodes - 1) {
        int receive_from = node_id + 1;
        int counts_temp[6];

        Receive(receive_from);

        for (int i = 0; i < 6; i++) {
            int taller_counts_sum = 0;

            counts_temp[i] = GetInt(receive_from);

            for (int j = 5; j > i; j--) {
                taller_counts_sum += counts[j];
            }

            result += counts_temp[i] * taller_counts_sum;
        }

        for (int i = 0; i < 6; i++) {
            counts[i] += counts_temp[i];
        }

        result += GetLL(receive_from);
    }

//     printf("%d %lld\n", node_id, result);
//     for (int j = 0; j < 6; j++) {
//         printf("%d ", counts[j]);
//     }
//     puts("");

    if (node_id > 0) {
        for (int i = 0; i < 6; i++) {
            PutInt(node_id - 1, counts[i]);
        }

        PutLL(node_id - 1, result);
        Send(node_id - 1);
    }

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