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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <iostream>
#include <algorithm>
#include <vector>
#include "message.h"
#include "teatr.h"

using namespace std;

vector<int> a;
vector<int> c;

void merge(int low, int high, int mid);

void mergesort(int low, int high) {
    int mid;
    if (low < high) {
        mid = (low + high) / 2;
        mergesort(low, mid);
        mergesort(mid + 1, high);
        merge(low, high, mid);
    }
}

long long int count_inv; //to store the inversion count_inv
void merge(int low, int high, int mid) {
    int i, j, k;
    i = low;
    k = low;
    j = mid + 1;
// standard merging from merge sort
    while (i <= mid && j <= high) {
        if (a[i] <= a[j]) {
            c[k] = a[i];
            k++;
            i++;
        } else {
            c[k] = a[j];
            k++;
            j++;
            //   cout<<a[i]<<" "<<mid<<" "<<i<<"\n";
            count_inv += (long long) mid - (long long) i + 1L; // This is where the trick occurs, if X > Y,
            //eg. in [3, 4, 5] and [1,2]
            //if(3>1) then 4,5 is obviously greater then 1, thus making count_inv as mid - i+1
        }
    }
    while (i <= mid) {
        c[k] = a[i];
        k++;
        i++;
    }
    while (j <= high) {
        c[k] = a[j];
        k++;
        j++;
    }
    for (i = low; i < k; i++) {
        a[i] = c[i];
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    int N = GetN();
    int ID = MyNodeId();
    count_inv = 0;
    if (N < 1000000) {
        if (ID == 0) {
            a.clear();
            c.clear();
            a.resize(N);
            c.resize(N);
            for (int i = 0; i < N; i++) {
                a[i] = GetElement(i);
            }
            mergesort(0, N - 1);
            cout << count_inv;
        } else {
            return 0;
        }
    } else {
        if (ID < 91) {
            vector<int> v1;
            vector<int> v2;
            int num1 = -1;
            int num2 = -1;
            int l = 0;
            for (int i = 0; i < 13; i++) {
                for (int i2 = i + 1; i2 < 14; i2++) {
                    if (l == ID) {
                        num1 = i;
                        num2 = i2;
                    }
                    l++;
                }
            }
            int l1 = (int)(N/14) * (num1);
            int l2 = (int)(N/14) * (num2);
            int r1 = (int)(N/14) * (num1 + 1);
            int r2 = (int)(N/14) * (num2 + 1);

            if(num2 == 13){
                r2 = N;
            }

            v1.resize(r1 - l1);
            v2.resize(r2 - l2);
            for (int i = 0; i < r1 - l1; i++) {
                v1[i] = GetElement(l1 + i);
            }
            for (int i = 0; i < r2 - l2; i++) {
                v2[i] = GetElement(l2 + i);
            }
            sort(v1.begin(), v1.end());
            sort(v2.begin(), v2.end());
            a.clear();
            c.clear();
            a.resize(v1.size() + v2.size());
            c.resize(v1.size() + v2.size());
            for(int i= 0; i< v1.size(); i++){
                a[i] = v1[i];
            }
            for(int i= 0; i< v2.size(); i++){
                a[i + v1.size()] = v2[i];
            }
            mergesort(0, a.size() - 1);
            //cout<<ID<<" "<<l1<<" "<<r1<<" "<<l2<<" "<<r2<<" "<<count_inv<<"\n";

        }
        if (ID >= 91 && ID < 98) {
            unsigned long long res = 0;
            int left = (int) (N / 14) * (ID - 91);
            int right = (int) (N / 14) * (ID - 91 + 1);
            a.clear();
            c.clear();
            a.resize(right - left);
            c.resize(right - left);
            for (int i = 0; i < right - left; i++) {
                a[i] = GetElement(left + i);
            }
            mergesort(0, right - left - 1);
            res += count_inv;
            //cout<<ID<<" "<<left<<" "<<right<<" "<<count_inv<<"\n";
            count_inv = 0;

            left = (int) (N / 14) * (ID - 91 + 7);
            right = (int) (N / 14) * (ID - 91 + 8);
            if (ID == 97) {
                right = N;
            }
            a.clear();
            c.clear();
            a.resize(right - left);
            c.resize(right - left);
            for (int i = 0; i < right - left; i++) {
                a[i] = GetElement(left + i);
            }
            mergesort(0, right - left - 1);
            //cout<<ID<<" "<<left<<" "<<right<<" "<<count_inv<<"\n";
            res += count_inv;
            count_inv = res;

        }

        if(ID == 98){
            return 0;
        }

        if (ID != 99) {
            PutLL(99, count_inv);
            Send(99);
        } else {
            unsigned long long res = 0;
            for (int i = 0; i < 98; i++) {
                Receive(i);
                res += GetLL(i);
            }
            cout << res;
        }
    }

    return 0;
}