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
#include <bits/stdc++.h>
#include "message.h"
#include "teatr.h"
using namespace std;

using ll = long long;

const int DIV = 10;

// int GetN() { return int(1e8); }
// int GetElement(int i) { return (i * 1ll * i) % int(1e6) + 1; }

inline pair<int,int> endpoints(int n, int i) {
    return make_pair((n * i) / DIV, (n * (i + 1)) / DIV);
}

vector<int> getElements(int a, int b) {
    vector<int> elems(b - a);
    for (int i = a; i < b; i++) {
        elems[i - a] = GetElement(i);
    }
    return elems;
}

ll myMergesort(vector<int> &a, int be, int en) {
    if (en <= be + 1) {
        return 0;
    }
    
    int mid = (be + en) / 2;
    ll res = 0;
    
    res += myMergesort(a, be, mid);
    res += myMergesort(a, mid, en);
    
    vector<int> tmp(en - be);
    int i = be, j = mid, k = 0;
    
    while (i < mid && j < en) {
        if (a[i] <= a[j]) {
            tmp[k] = a[i];
            k++;
            i++;
        }
        else {
            tmp[k] = a[j];
            res += mid - i;
            k++;
            j++;
        }
    }
    
    while (i < mid) {
        tmp[k] = a[i];
        k++;
        i++;
    }
    while (j < en) {
        tmp[k] = a[j];
        k++;
        j++;
    }
    
    for (k = be; k < en; k++) {
        a[k] = tmp[k - be];
    }
    
    return res;
}

ll calc1(vector<int> &a) {
    return myMergesort(a, 0, a.size());
}

ll calc2(vector<int> &a, vector<int> &b) {
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    
    ll res = 0;
    int i = 0, j = 0;
    int na = a.size(), nb = b.size();
    
    while (i < na && j < nb) {
        if (a[i] <= b[j]) {
            i++;
        }
        else {
            res += na - i;
            j++;
        }
    }
    
    return res;
}

ll solve(int n, int id) {
    int i0 = id / DIV;
    int j0 = id % DIV;
    int i = min(i0, j0);
    int j = max(i0, j0);
    pair<int,int> ends = endpoints(n, i);
    
    vector<int> a = getElements(ends.first, ends.second);
    ll res = 0;
    
    if (i != j) {
        pair<int,int> ends2 = endpoints(n, j);
        int be = ends2.first;
        int en = ends2.second;
        int mid = (be + en) / 2;
        
        if (i0 < j0) {
            vector<int> b = getElements(be, mid);
//             vector<int> b = getElements(be, en);
            res = calc2(a, b);
        }
        else {
            vector<int> b = getElements(mid, en);
            res = calc2(a, b);
        }
    }
    else {
        res = calc1(a);
    }
    
    return res;
}

int main() {
    int n = GetN();
    int id = MyNodeId();
    
    ll res = solve(n, id);
    
    if (id != 0) {
        PutLL(0, res);
        Send(0);
    }
    else {
        for (int i = 1; i < NumberOfNodes(); i++) {
            Receive(i);
            res += GetLL(i);
        }
        
        cout << res << "\n";
    }
}