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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include "message.h"
#include "teatr.h"

#include <vector>
#include <algorithm>
#include <deque>

template <typename T>
long long merge_sort(std::vector<T>& vec) // returns inversions
{
	int N = vec.size();
	std::vector<T> tmp(N);
	
	long long inversions = 0;
	for (int chunk = 1; chunk < N; chunk *= 2)
	{
		for (int i = 0; i < N; i += chunk*2)
		{
			int l_b = i, l_e = std::min(i+chunk, N);
			if (l_e == N)
				continue;
			int r_b = i+chunk, r_e = std::min(i + 2*chunk, N);
			int pos = i;
			while (l_b != l_e && r_b != r_e)
			{
				if (vec[l_b] <= vec[r_b])
				{
					tmp[pos++] = vec[l_b++];
				}
				else
				{
					inversions += l_e - l_b;
					tmp[pos++] = vec[r_b++];
				}
			}
			while (l_b != l_e)
				tmp[pos++] = vec[l_b++];
			while (r_b != r_e)
				tmp[pos++] = vec[r_b++];
			while (--pos >= 0)
				vec[pos] = tmp[pos];
		}
	}
	return inversions;
}

typedef long long LL;
#define FOR(ii, ll, uu)  for(int ii##lim = (uu), ii = (ll); ii < ii##lim; ++ii)
#define REP(ii, nn) FOR(ii, 0, nn)
#define FORL(ii, ll, uu)  for(LL ii##lim = (uu), ii = (ll); ii < ii##lim; ++ii)
#define REPL(ii, nn) FOR(ii, 0, nn)

using namespace std;

struct partial
{
    LL inversions;
    int count;
    deque<int> histogram;
};

partial process(int from, int till)
{
    int n = till - from;
    vector<int> rows;
    rows.reserve(n);
    REP(i, n)
    {
        rows.push_back(GetElement(from + i));
    }
    partial res;
    res.inversions = merge_sort(rows);
    res.count = n;
    int current = -1;
    int n_current = 0;
    REP(i, n)
    {
        if (rows[i] != current)
        {
            if (n_current)
            {
                res.histogram.push_back(n_current);
                res.histogram.push_back(current);
            }
            current = rows[i];
            n_current = 1;
        }
        else
        {
            ++n_current;
        }
    }
    if (n_current)
    {
        res.histogram.push_back(n_current);
        res.histogram.push_back(current);
    }
    res.histogram.push_back(0);
    return res;
}

void send(int node, const partial& p)
{
    PutLL(node, p.inversions);
    PutInt(node, p.count);
    for(int x : p.histogram)
    {
        PutInt(node, x);
    }
    Send(node);
}

partial receive(int node)
{
    partial res;
    Receive(node);
    res.inversions = GetLL(node);
    res.count = GetInt(node);
    int x;
    do {
        res.histogram.push_back(x = GetInt(node));
    } while (x);
    return res;
}

partial merge_partial(partial left, partial right)
{
    partial res;
    res.inversions = left.inversions + right.inversions;
    res.count = left.count + right.count;
    while (left.histogram.front() && right.histogram.front())
    {
        if (left.histogram[1] < right.histogram[1])
        {
            int count = left.histogram.front();
            res.histogram.push_back(count); left.histogram.pop_front();
            res.histogram.push_back(left.histogram.front()); left.histogram.pop_front();
            left.count -= count;
        }
        else if(left.histogram[1] > right.histogram[1])
        {
            LL count = right.histogram.front();
            res.histogram.push_back(count); right.histogram.pop_front();
            res.histogram.push_back(right.histogram.front()); right.histogram.pop_front();
            right.count -= count;
            res.inversions += left.count * count; // everything in left is inversion
        }
        else // left.histogram[1] == right.histogram[1]
        {
            int count_l = left.histogram.front(); left.histogram.pop_front();
            LL count_r = right.histogram.front(); right.histogram.pop_front();
            res.histogram.push_back(count_l + count_r);
            left.count -= count_l;
            right.count -= count_r;
            
            res.histogram.push_back(left.histogram.front());
            left.histogram.pop_front();
            right.histogram.pop_front();

            res.inversions += left.count * count_r;
        }
    }

    // add tails

    while (left.histogram.front())
    {
        res.histogram.push_back(left.histogram.front());
        left.histogram.pop_front();
    }

    while (right.histogram.front())
    {
        res.histogram.push_back(right.histogram.front());
        right.histogram.pop_front();
    }

    res.histogram.push_back(0);

    return res;
}

#include <iostream>

ostream& operator<<(ostream& o, const partial& p)
{
    o << "inversions: " << p.inversions << ", count: " << p.count << ", [";
    for (auto i : p.histogram)
        o << i << ", ";
    return o << ']';
}

int main(int argc, char const *argv[])
{
    long long N = GetN();
    int my_id = MyNodeId();
    int non = NumberOfNodes();

    LL from = (my_id * N / non);
    LL till = ((my_id + 1) * N / non);

    partial mine = process(from, till);
    // cout << "mine: " << mine << endl;

    if (my_id > 0)
    {
        send(0, mine);
        return 0;
    }

    // my_id == 0

    FOR(node, 1, non)
    {
        partial theirs = receive(node);
        // cout << "theirs: " << theirs << endl;
        mine = merge_partial(mine, theirs);
        // cout << "mine after: " << mine << endl;
    }

    cout << mine.inversions << endl;

    return 0;
}