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

int drzewo[1000007];

void ins(int a) {
    while (a != 0) {
        drzewo[a]++;
        a -= (a & -a);
    }
}

int get(int a) {
    int ret = 0;
    while (a <= 1000000) {
        ret += drzewo[a];
        a += (a & -a);
    }
    return ret;
}

int main() {
    if (MyNodeId() == 0) {
        long long wynik = 0;
        for (int i = 1; i <= 99; i++) {
            Receive(i);
            long long w = GetLL(i);
            wynik += w;
        }
        printf("%lld", wynik);
    } else {
        int id = MyNodeId();
        int N = GetN();
        int ile = N / (NumberOfNodes() - 1) + 1;
        int skad = (id - 1) * ile;
        int dokad = std::min(id * ile, N);
        if (skad > N) {
            PutLL(0, 0);
            Send(0);
            return 0;
        }
        for (int i = 0; i < skad; i++)
            drzewo[GetElement(i)]++;
        for (int i = 1000000; i >= 1; i--)
            if (i - (i & -i) > 0)
                drzewo[i - (i & -i)] += drzewo[i];
        long long dod = 0;
        for (int i = skad; i < dokad; i++) {
            int w = GetElement(i);
            ins(w);
            dod += get(w + 1);
        }
        PutLL(0, dod);
        Send(0);
    }
    return 0;
}