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

long long int merge(int *tab, int *tab2, int start_1, int koniec_1, int start_2, int koniec_2){
	long long int inwersje = 0;
	
	for(int i = start_1; i <= koniec_2; i++){
		tab2[i] = tab[i];
	}
	
	int i = start_1;
	int j = start_2;
	int k = start_1;
	int l = start_1;
	
	while(i <= koniec_1 && j <= koniec_2){
		while(tab2[l] >= tab2[j] && l <= koniec_1){
			l++;
		}
		if(tab2[i] < tab2[j]){
			tab[k++] = tab2[j++];
			inwersje += (koniec_1 - l + 1);
		}else{
			tab[k++] = tab2[i++];
		}
	}
	
	while(i <= koniec_1){
		tab[k++] = tab2[i++];
	}
	
	while(j <= koniec_2){
		tab[k++] = tab2[j++];
	}
	
	return inwersje;
}

long long int merge_sort(int *tab, int *tab2, int start, int koniec){
	if(start < koniec){
		int m = (start + koniec) / 2;
		
		long long int a = merge_sort(tab, tab2, start, m);
		long long int b = merge_sort(tab, tab2, m + 1, koniec);
		
		long long int c = merge(tab, tab2, start, m, m + 1, koniec);
		
		return a + b + c;
	}
	return (long long int)(0);
}

long long int compute(vector<int> &P) {
	int n = P.size();
	int *tab = new int[n + 1];
	int *tab2 = new int[n + 1];

	for(int i = 1; i <= n; i++){
		tab[i] = -P[i - 1];
	}
	
	long long int res = merge_sort(tab, tab2, 1, n);
	delete [] tab;
	delete [] tab2;
	return res;
}

int main() {
	int k = NumberOfNodes();
	int id = MyNodeId();
	int n = GetN();
	
	if(n <= 4000000) {
		if(MyNodeId() != 0) return 0;
		vector <int> data;
		for(int i = 0; i < n; i++) {
			data.push_back(GetElement(i));
		}
		cout << compute(data) << endl;
		return 0;
	}
	
	int s = n / k;
	int start = id * s;
	int koniec = (id + 1) * s;
	if(id == k - 1) koniec = n;
	long long int s1, s2, s3, s4, s5, oth;
	s1 = s2 = s3 = s4 = s5 = oth = 0;
	vector <int> data;
	
	for(int i = start; i < koniec; i++) {
		int in = GetElement(i);
		if(in == 1) s1++;
		if(in == 2) s2++;
		if(in == 3) s3++;
		if(in == 4) s4++;
		if(in == 5) s5++;
		if(in > 5) oth++;
		data.push_back(in);
	}
	
	long long int res = compute(data);
	
	if(MyNodeId() != 0) {
		PutLL(0, oth); Send(0);
		PutLL(0, res); Send(0);
		PutLL(0, s1); Send(0);
		PutLL(0, s2); Send(0);
		PutLL(0, s3); Send(0);
		PutLL(0, s4); Send(0);
		PutLL(0, s5); Send(0);
	} else {
		long long int T[k][8];
		T[0][0] = oth;
		T[0][1] = res;
		T[0][2] = s1;
		T[0][3] = s2;
		T[0][4] = s3;
		T[0][5] = s4;
		T[0][6] = s5;
		for(int i = 0; i < k; i++) {
			T[i][7] = 0;
		}
		
		int end = 0;
		while(end < 7 * (k - 1)) {
			int rec = Receive(-1);
			T[rec][T[rec][7]] = GetLL(rec);
			T[rec][7]++;
			end++;
		}
		
		bool ok = true;
		for(int j = 0; j < k; j++) {
			if(T[j][6] > 0) {
				ok = false;
				break;
			}
		}
		
		long long int answer = 0;
		for(int j = 0; j < k; j++) {
			answer += T[j][1];
		}
		
		long long int S1, S2, S3, S4;
		S1 = S2 = S3 = S4 = 0;
		for(int j = 1; j < k; j++) {
			S1 += T[j][2];
			S2 += T[j][3];
			S3 += T[j][4];
			S4 += T[j][5];
		}
		
		for(int j = 0; j < k - 1; j++) {
			answer += (S1 + S2 + S3 + S4) * T[j][6];
			answer += (S1 + S2 + S3) * T[j][5];
			answer += (S1 + S2) * T[j][4];
			answer += S1 * T[j][3];
			S1 -= T[j + 1][2];
			S2 -= T[j + 1][3];
			S3 -= T[j + 1][4];
			S4 -= T[j + 1][5];
		}
		
		cout << answer << endl;
	}
}