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
#include "teatr.h"
#include "message.h"
#include <bits/stdc++.h>
#define pb push_back

using namespace std;
typedef long long ll;
/*
vector<int> in = {5, 2, 4, 4, 3};

int GetElement(int i) {
    return in[i];
}

int GetN() {
    return in.size();
}*/

ll cnt = 0;

void mergesort(vector<int> &v, int L, int R) {
    if (L >= R) return;
    int M = (L+R) / 2;
    mergesort(v, L, M);
    mergesort(v, M+1, R);
    int a = L;
    int b = M+1;
    vector<int> v2;
    while(a <= M && b <= R) {
        if(v[a] <= v[b]) {
            v2.pb(v[a]);
            a++;
        }
        else {
            v2.pb(v[b]);
            b++;
            cnt += (M - a + 1);
        }
    }
    while(a <= M) v2.pb(v[a++]);
    while(b <= R) v2.pb(v[b++]);

    for(int i = 0; i < v2.size(); i++) v[i+L] = v2[i];
}

int main(){
    ios_base::sync_with_stdio(0);
    if (MyNodeId() == 0) {
        int n = GetN();
        vector<int> v(n);
        for (int i = 0; i < n; i++) {
            v[i] = GetElement(i);
        }
        mergesort(v, 0, n - 1);
        cout << cnt;
    }
}