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

using namespace std;

typedef unsigned long long ull;

int main() {
    const int NODE_ID = MyNodeId();
    const int NODE_SIZE = 1000000;
    const int N = GetN();
    const int NODES = (int) ceil((float) N / (float) NODE_SIZE);

    if (NODE_ID * NODE_SIZE > N) {
        return 0;
    }
    const int OFFSET = NODE_ID * NODE_SIZE;

    int size = NODES == 1 ? N : NODE_ID < NODES - 1 ? NODE_SIZE : N - OFFSET;

    vector<int> heights;
    heights.reserve(size);
    for (int i = 0; i < size; i++) {
        int height = GetElement(OFFSET + i);
        heights.push_back(height);
    }
    vector<int> buckets = {0, 0, 0, 0, 0, 0};

    ull result = 0;

    for (int height : heights) {
        buckets[height]++;
        for (int i = height + 1; i < buckets.size(); i++) {
            result += buckets[i];
        }
    }

    if (NODES == 1) {
        cout << result << endl;
        return 0;
    }

    for (int i = NODE_ID + 1; i < NODES; i++) {
        for (int bucket : buckets) {
            PutInt(i, bucket);
        }
        Send(i);
    }

    for (int i = 0; i < NODE_ID; i++) {
        vector<int> buckets2;
        Receive(i);
        for (int j = 0; j < 6; j++) {
            buckets2.push_back(GetInt(i));
        }
        for (int i = 0; i < 6; i++) {
            for (int j = i + 1; j < 6; j++) {
                result += buckets[i] * buckets2[j];
            }
        }
    }
    if (NODE_ID != 0) {
        PutLL(0, result);
        Send(0);
    } else {
        for (int i = 1; i < NODES; i++) {
            Receive(i);
            result += GetLL(i);
        }
        cout << result << endl;
    }
    return 0;
}