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
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <bits/stdc++.h>
#include "message.h"
#include "teatr.h"
using namespace std;

#define LL long long
#define ll long long
//#define int long long

const int MAXA = 1000*1000;
const int MAXN = 100*1000*1000;

int getN() {
    static int N = -1;
    if (N == -1) {
        N = GetN();
    }
    return N;
}

const int SP = 10;
const int P = SP * SP;

int getElement(int k) {
    if (k >= getN()) {
        return MAXA;
    }

    return GetElement(k);
}

vector<int> countGreater(int from, int to) {
    vector<int> res(MAXA + 1);

    for (int i = from; i < to; ++i) {
        int x = getElement(i);
        res[x-1]++;
    }

    for (int i = MAXA - 1; i >= 0; --i) {
        res[i] += res[i+1];
    }

    return res;
}

LL mergeSort(vector<int> &v, int from, int to) {
    static vector<int> buf(MAXN / P + 1);

    if (to - from < 2) {
        return 0;
    }

    int mid = (from + to) / 2;

    LL res = 0;
    res += mergeSort(v, from, mid);
    res += mergeSort(v, mid, to);

    int p = from;
    int q = mid;
    int r = 0;

    while (p < mid && q < to) {
        if (v[p] <= v[q]) {
            buf[r++] = v[p++];
        } else {
            buf[r++] = v[q++];

            res += mid - p;
        }
    }

    while (p < mid) {
        buf[r++] = v[p++];
    }

    while (q < to) {
        buf[r++] = v[q++];
    }

    for (int i = from; i < to; i++) {
        v[i] = buf[i-from];
    }

    return res;
}

LL countInversions(int from, int to) {
    vector<int> v(to - from);

    for (int i = from; i < to; ++i) {
        v[i - from] = getElement(i);
    }

    return mergeSort(v, 0, v.size());
}

void solve(LL inversions, vector<int> greater) {
    int myInnerId = MyNodeId() % SP;
    int myBlock = MyNodeId() / SP;

    LL res = inversions;

    for (int i = myBlock * MAXN / SP + (myInnerId + 1) * MAXN / P; i < (myBlock + 1) * MAXN / SP; ++i){
        int z = getElement(i);
        res += greater[z];
    }

    greater = countGreater(myBlock * MAXN / SP, (myBlock + 1) * MAXN / SP);
    for (int i = myBlock + 1; i < SP; ++i) {
        int id = MyNodeId() + (i - myBlock) * SP;
        int from = id * MAXN / P;
        int to = (id + 1) * MAXN / P;

        for (int i = from; i < to; ++i) {
            int z = getElement(i);
            res += greater[z];
        }
    }

    if (MyNodeId() == 0) {
        for (int i = 1; i < P; ++i) {
            int p = Receive(-1);
            res += GetLL(p);
        }
        cout << res << "\n";
    } else {;
        PutLL(0, res);
        Send(0);
    }    
}

signed main() {
    ios_base::sync_with_stdio(0);

    int myId = MyNodeId();

    if (myId >= P) return 0;

    LL inversions = countInversions(myId * MAXN / P, (myId + 1) * MAXN / P);
    vector<int> greater = countGreater(myId * MAXN / P, (myId + 1) * MAXN / P);

    solve(inversions, greater);
    return 0;
}