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
#pragma warning(disable:4786)
#pragma warning(disable:4996)
#include <ctime>
#include<list>
#include <numeric>
#include<bitset>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<functional>
#include<string>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<utility>
#include<fstream>
#include<sstream>
#include<cmath>
#include<stack>
#include<assert.h>
#include<unordered_map>

#include "dzialka.h"
#include "message.h"
using namespace std;

#define MEM(a, b) memset(a, (b), sizeof(a))
#define CLR(a) memset(a, 0, sizeof(a))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ABS(X) ( (X) > 0 ? (X) : ( -(X) ) )
#define S(X) ( (X) * (X) )
#define SZ(V) (int )V.size()
#define FORN(i, n) for(int i = 0; i < n; i++)
#define FORAB(i, a, b) for(int i = a; i <= b; i++)
#define ALL(V) V.begin(), V.end()
#define IN(A, B, C)  ((B) <= (A) && (A) <= (C))
#define AIN(A, B, C) assert(IN(A, B, C))

//typedef int LL;
typedef long long int LL;
//typedef __int128 LL;
//typedef __int64 LL;

typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<long double, long double> PDD;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII > VP;
typedef vector<double> VD;
typedef vector<LL> VL;
typedef long double ld;

int num_nodes, my_id;
int h, w;

LL nc2(LL n) {
	return (n * (n + 1)) / 2;
}

void solve_single() {
	vector<vector<int>> V(h, VI(w, 0));
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			V[i][j] = IsUsableCell(i, j);
		}
	}

	stack<PII> S;
	LL ans = 0;
	for (int j = 0; j < w; j++) {
		for (int i = 0; i < h; i++) {
			if (j && V[i][j]) {
				V[i][j] += V[i][j - 1];
			}
			int cur = V[i][j];
			int pos = i;
			int last = i;
			while (S.size()) {
				if (S.top().first < cur) break;
				pos = S.top().second;
				ans += (nc2(i - pos) - nc2(pos - last))  * (S.top().first - cur);
				last = pos;
				S.pop();
			}
			S.push({ cur, pos });
		}
		int last = h;
		while (S.size()) {
			int pos = S.top().second;
			ans += (nc2(h - pos) - nc2(pos - last)) * S.top().first;
			last = pos;
			S.pop();
		}
	}
	printf("%lld\n", ans);
}

stack<PII> S[75002];
int main() {
	my_id = MyNodeId();
	num_nodes = NumberOfNodes();
	h = GetFieldHeight();
	w = GetFieldWidth();

	if (h <= 1000 || w <= 1000 || num_nodes == 1) {
		if (my_id) return 0;
		solve_single();
		return 0;
	}
	
	int seg_len = (w + num_nodes - 1) / num_nodes;
	int start = seg_len * my_id;
	int end = seg_len * (my_id + 1) - 1;
	end = MIN(end, w - 1);

	int div = 100;
	VI V(h, 0);
	LL ans = 0;
	for (int d = 0; d < div; d++) {
		int row_len = (h + div - 1) / div;
		int start_row = row_len * d;
		int end_row = row_len * (d + 1) - 1;
		end_row = MIN(end_row, h - 1);

		// init
		if (my_id) Receive(my_id - 1);
		for (int r = start_row; r <= end_row; r++) {
			if (my_id == 0) V[r] = 0;
			else {
				V[r] = GetInt(my_id - 1);
			}
			//printf("%d %d %d\n", my_id, r, V[r]);
		}

		// process
		for (int c = start; c <= end; c++) {
			for (int r = start_row; r <= end_row; r++) {
				int now = IsUsableCell(r, c);
				if (now == 0) V[r] = 0;
				else V[r]++;

				int cur = V[r];
				int pos = r;
				int last = r;
				while (S[c].size()) {
					if (S[c].top().first < cur) break;
					pos = S[c].top().second;
					ans += (nc2(r - pos) - nc2(pos - last))  * (S[c].top().first - cur);
					last = pos;
					S[c].pop();
				}
				S[c].push({ cur, pos });
			}
		}

		for (int r = start_row; r <= end_row; r++) {
			//printf(">>> %d %d %d, %d %d\n", my_id, start, end, r, V[r]);
		}
		// Send out
		if (my_id != num_nodes - 1) {
			for (int r = start_row; r <= end_row; r++) {
				PutInt(my_id + 1, V[r]);
			}
			Send(my_id + 1);
		}
	}

	for (int c = start; c <= end; c++) {
		int last = h;
		while (S[c].size()) {
			int pos = S[c].top().second;
			ans += (nc2(h - pos) - nc2(pos - last)) * S[c].top().first;
			last = pos;
			S[c].pop();
		}
	}
	PutLL(0, ans);
	Send(0);

	if (my_id) return 0;

	LL total = 0;
	for (int i = 0; i < num_nodes; i++) {
		Receive(i);
		total += GetLL(i);
	}
	printf("%lld\n", total);
	return 0;
}