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
147
148
149
150
151
152
#include "message.h"
#include "teatr.h"
#include "bits/stdc++.h"

using namespace std;

#define PB push_back
#define FORE(i, t) for(__typeof(t.begin())i=t.begin();i!=t.end();++i)
#define SZ(x) int((x).size())
#define REP(i, n) for(int i=0,_=(n);i<_;++i)
#define FOR(i, a, b) for(int i=(a),_=(b);i<=_;++i)
#define FORD(i, a, b) for(int i=(a),_=(b);i>=_;--i)

#ifdef DEBUG
#define DEB(x) (cerr << x)
#else
#define DEB(x)
#endif

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair <ll, ll> pll;

const int INF = 1e9 + 9;
const ll MAX_NODES = 100;

int me;
int nodes_count;

pll get_range(ll node, ll range_max) { // For range [1..range_max] inclusive.
    ll a = range_max * node / nodes_count + 1LL;
    ll b = range_max * (node + 1LL) / nodes_count;
    return pll(a, b);
}

int n;

ll merge_sort(vi &t, int a, int b) {
//    cout << "merge " << a << ", " << b << "\n";
    if (a >= b) {
        return 0;
    }
    int mid = (a + b) / 2;
    ll result = 0;
    result += merge_sort(t, a, mid);
    result += merge_sort(t, mid + 1, b);
    vi merged;
    int i = a, j = mid + 1;
    for (; i <= mid && j <= b;) {
        if (t[i] > t[j]) {
            merged.PB(t[j]);
            result += mid - i + 1;
            ++j;
        } else {
            merged.PB(t[i]);
            ++i;
        }
    }
    while (i <= mid) {
        merged.PB(t[i]);
        ++i;
    }
    while (j <= b) {
        merged.PB(t[j]);
        ++j;
    }
    REP(i, SZ(merged)) {
        t[a + i] = merged[i];
    }
//    cout << "merge " << a << ", " << b << " => " << result << "\n";
    return result;
}

ll count_inversions(vi &t) {
    return merge_sort(t, 0, SZ(t) - 1);
}

const int MC = 1002;
int cnt[MAX_NODES][MC + 1];
//ll invs[MAX_NODES];

int main() {
    ios::sync_with_stdio(false);

    me = MyNodeId();
    nodes_count = NumberOfNodes();
    n = GetN();

    pll my_range = get_range(me, n);
    int my_start = my_range.first;
    int my_end = my_range.second;

//    cout << "me = " << me << " n = " << n << " range=(" << my_start << ", " << my_end << ")" << "\n";

    vi t;
    FOR (i, my_start, my_end) {
//        cout << "i=" << i << "\n";
//        cout << flush;
        int x = GetElement(i - 1);
        t.PB(x);
    }

    ll inv = count_inversions(t);
//    cout << "inv = " << inv << "\n";


    FORE(it, t) {
        int x = *it;
        if (x <= MC) {
            ++cnt[me][x];
        }
    }

    if (me > 0) {
        PutLL(0, inv);
        FOR(i, 1, MC) {
            PutInt(0, cnt[me][i]);
        }
        Send(0);
        return 0;
    }
    // Zostal juz tylko node == 0

    FOR(node, 1, nodes_count - 1) {
        Receive(node);
        inv += GetLL(node);
        FOR(i, 1, MC) {
            cnt[node][i] = GetInt(node);
        }
    }

    vector <ll> sum(MC + 1);

    REP(node, nodes_count) {
//        cout << "node: " << node << "\n";
        ll prev = 0;
        FOR(i, 1, MC) {
            prev += sum[i];
        }
        FOR(i, 1, MC) {
//            cout << cnt[node][i] << " ";
            prev -= sum[i];
            inv += prev * cnt[node][i];
            sum[i] += cnt[node][i];
        }
//        cout << "\n";
    }

    cout << inv << "\n";

}