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
93
94
95
96
97
98
// Jedna instancja wypisuje maksymalny wzrost.

#include "message.h"
#include "teatr.h"
#include "bits/stdc++.h"

#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define debug(x) {cout <<#x <<" = " <<x <<endl; }
#define lint long long int

using namespace std;

int n, id, nodes, MAXK;

int cnt[1000006];
int moje[1000006];
int mcnt[1000006];

int bit[1000006];

int wez(int a) {
   int ans = 0;
   while (a > 0) {
        ans += bit[a];
        a ^= (a & (-a));
   }
   return ans;
}

void dodaj(int a) {
    while (a <= MAXK) {
        bit[a]++;
        a += (a & (-a));
    }
}

lint rob_swoje(int m) {
    lint ans = 0;
    FOR(i,0,m) {
        ans += i - wez(moje[i]);
        dodaj(moje[i]);
    }
    return ans;
}

int main() {
    n = GetN(); id = MyNodeId(); nodes = NumberOfNodes();
    MAXK = 1e6;
    
    FOR(i,0,MAXK+1) {
       cnt[i] = moje[i] = mcnt[i] = bit[i] = 0;
    }
    if (n < 1e6) {
        if (id == 0) {
            // rob sam
            FOR(i,0,n) {
                moje[i] = GetElement(i);
            }
            lint ans = rob_swoje(n);
            printf("%lld\n", ans);
            return 0;
        } 
        else {
            return 0;
        }
    }
    
    int BULK = (n + nodes-1)/nodes;
    int myStart = min(BULK * id, n);
    int myMeta = min(BULK * (id+1), n);

    FOR(i,0,myStart) {
        cnt[GetElement(i)]++;
    }
    FOR(i,myStart,myMeta) {
        int a = GetElement(i);
        moje[i-myStart] = a;
        mcnt[a]++;
    }
    lint my_ans = rob_swoje(myMeta-myStart);
    lint act = cnt[MAXK];
    for (int i = MAXK-1; i>0; i--) {
        my_ans += mcnt[i]*1LL*act;
        act += cnt[i];
    }
	if (id != 0) {
		PutLL(0,my_ans);
		Send(0);
        return 0;
	} else {
       FOR(i,0,nodes-1) {
            int kto = Receive(-1);
            my_ans += GetLL(kto);
       }
       printf("%lld\n", my_ans);
       return 0;
    }
}