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
225
226
227
228
229
#include <bits/stdc++.h>
using namespace std;
//~ #warning stupid DLOCAL
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge {c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

//~ int GetFieldHeight() { return 55000; }
//~ int GetFieldWidth() { return 55000; }
//~ int IsUsableCell(int row, int col) { return row != col; }
#include "dzialka.h"
#include "message.h"

typedef long long ll;

int nodes, my_id;

int main() {
	nodes = NumberOfNodes();
	my_id = MyNodeId();
	
	//~ if(nodes == 100) nodes = 10;
	//~ else if(nodes == 40) nodes = 5;
	//~ else assert(false);
	//~ if(my_id >= nodes) return 0;
	
	const int H = GetFieldHeight();
	const int W = GetFieldWidth();
	
	int low = my_id * (long long) H / nodes;
	int high = (my_id + 1) * (long long) H / nodes - 1;
	
	vector<int> toSend(W, H), right(W, H);
	
	for(int col = 0; col < W; ++col) {
		for(int row = low; row <= high; ++row) {
			if(!IsUsableCell(row, col)) {
				toSend[col] = row; // first 0
				break;
			}
		}
	}
	
	const int parts = 10;
	
	if(my_id != nodes - 1) {
		for(int x = 0; x < parts; ++x) {
			const int low = (long long) x * W / parts;
			const int high = (long long) (x + 1) * W / parts - 1;
			Receive(my_id + 1);
			for(int i = low; i <= high; ++i) {
				right[i] = GetInt(my_id + 1);
			}
		}
	}
	
	if(my_id) {
		for(int x = 0; x < parts; ++x) {
			const int low = (long long) x * W / parts;
			const int high = (long long) (x + 1) * W / parts - 1;
			for(int i = low; i <= high; ++i) {
				PutInt(my_id - 1, min(right[i], toSend[i]));
			}
			Send(my_id - 1);
		}
	}
	
	
	#define prev prev_compile
	#define next next_compile
	vector<int> next(W + 3), prev(W + 3);
	//~ const int sufit = (H + 31) / 32;
	//~ vector<unsigned int> mask(sufit, 0);
	//~ vector<vector<int>> inv(H + 1);
	
	long long answer = 0;
	
	//~ vector<pair<int,int>> values(W);
	
	//~ for(int col = 0; col < W; ++col)
		//~ right[col] += high + 1;
	
	vector<int> values[2];
	int bit = 0;
	values[0].resize(W);
	values[1].resize(W);
	for(int i = 0; i < W; ++i)
		values[bit][i] = i;
	sort(values[bit].begin(), values[bit].end(), [&] (int a, int b) {
		return right[a] > right[b];
	});
	
	debug() << imie(right) imie(values[bit]);
	
	for(int row = high; row >= low; --row) {
		for(int i = 0; i <= W; ++i) next[i] = i + 1;
		for(int i = 1; i <= W + 1; ++i) prev[i] = i - 1;
		
		if(0)for(int col = 0; col < W; ++col) {
			if(!IsUsableCell(row, col)) right[col] = row; // first 0
			//~ if(IsUsableCell(row, col)) ++right[col];
			//~ else right[col] = 0;
		}
		int ipom = 0, ilast = W;
		for(int iii = 0; iii < W; ++iii) {
			int i = values[bit][iii];
			const int value = right[i];
			if(IsUsableCell(row, i)) {
				values[bit^1][ipom++] = i;
				++i;
				debug() << imie(my_id) "x" imie(row) imie(i-1) imie(value - row) imie(i - prev[i]) imie(next[i] - i);
				answer += (long long) (value - row) * (i - prev[i]) * (next[i] - i);
				debug() << imie(answer);
				next[prev[i]] = next[i];
				prev[next[i]] = prev[i];
			}
			else {
				right[i] = row;
				values[bit^1][--ilast] = i;
			}
		}
		assert(ipom == ilast);
		//~ for(int i = 0; i < W; ++i)
			//~ if(right[values[bit][i]] == row)
				//~ values[bit^1][ipom++] = values[bit][i];
		//~ assert(ipom == W);
		bit ^= 1;
		//~ for(int i = 0; i < W; ++i) {
			//~ inv[right[i]].push_back(i + 1);
			//~ mask[right[i]/32] |= 1u << (right[i] % 32);
		//~ }
		
		debug() << "--";
		
		//~ for(int i = 0; i < W; ++i)
			//~ values[i] = {right[i], i + 1};
		//~ sort(values.rbegin(), values.rend());
		
		if(0)for(int i : values[bit]) {
			int value = right[i];
			
		}
		
		//~ for(int value = H; value >= row; --value) {
			//~ for(int i : inv[value]) {
				//~ answer += (long long) (value - row) * (i - prev[i]) * (next[i] - i);
							//~ next[prev[i]] = next[i];
							//~ prev[next[i]] = prev[i];
			//~ }
			//~ inv[value].clear();
		//~ }
		
		//~ for(int tmp = sufit - 1; tmp >= 0; --tmp)
			//~ if(mask[tmp]) {
				//~ for(int bit = 31; bit >= 0; --bit)
					//~ if(mask[tmp] & (1u << bit)) {
						//~ int value = tmp * 32 + bit;
						//~ for(int i : inv[value]) {
							//~ answer += (long long) value * (i - prev[i]) * (next[i] - i);
							//~ next[prev[i]] = next[i];
							//~ prev[next[i]] = prev[i];
						//~ }
						//~ inv[value].clear();
					//~ }
				//~ mask[tmp] = 0;
			//~ }
		
		
		/*for(pair<int,int> p : values) {
			int value = p.first;
			int i = p.second;
		
		//~ for(int value = H; value >= 0; --value) if(!inv[value].empty()) {
			//~ debug() << imie(value) imie(inv[value]);
			//~ for(int i : inv[value]) {
				debug() << imie(value) imie(i) imie(next[i]) imie(prev[i]);
				answer += (long long) value * (i - prev[i]) * (next[i] - i);
				next[prev[i]] = next[i];
				prev[next[i]] = prev[i];
			//~ }
			//~ inv[value].clear();
		}*/
		
		//~ for(int a = 0; a < W; ++a)
			//~ for(int b = a; b < W; ++b) {
				//~ int maly = right[a];
				//~ for(int i = a + 1; i <= b; ++i)
					//~ maly = min(maly, right[i]);
				//~ answer += maly;
			//~ }
	}
	
	if(my_id) {
		PutLL(0, answer);
		Send(0);
		return 0;
	}
	for(int i = 1; i < nodes; ++i) {
		Receive(i);
		answer += GetLL(i);
	}
	
	printf("%lld\n", answer);
}