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
#include "dzialka.h"
#include "message.h"

#include <vector>
#include <cstdio>
#include <algorithm>

using namespace std;

#define WHICH -1
#if WHICH == 1
	static char IsInitialized = 0;
	static int FieldH, FieldW;
	vector<vector<char>> Board;

	static void Initialize() {
		FILE* f = fopen("./in", "r");
		fscanf(f, "%d%d", &FieldH, &FieldW);
		Board.resize(FieldH);
		for (int i = 0; i < FieldH; ++i) {
			Board[i].resize(FieldW);
			fscanf(f, "%s", Board[i].data());
		}
		fclose(f);
		IsInitialized = true;
	}
	int GetFieldHeight() {
		if (!IsInitialized) { Initialize(); }
		return FieldH;
	}
	int GetFieldWidth() {
		if (!IsInitialized) { Initialize(); }
		return FieldW;
	}
	int IsUsableCell(int Row, int Col) {
		if (!IsInitialized) { Initialize(); }
		return Board[Row][Col] == '1';
	}
#endif
#if WHICH == 2
	int GetFieldHeight() { return 6; }
	int GetFieldWidth() { return 6; }
	int IsUsableCell(int Row, int Col) {
		return 
			(Row != 0 || Col != 1) &&
			(Row != 1 || Col != 5) &&
			(Row != 4 || Col != 0) &&
			(Row != 5 || Col != 4);
	}
	
#endif
#if WHICH == 3
	int GetFieldHeight() { return 9; }
	int GetFieldWidth() { return 9; }
	int IsUsableCell(int Row, int Col) {
		return Row != Col;
	}
#endif
#if WHICH == 4
	int GetFieldHeight() { return 40000; }
	int GetFieldWidth() { return 40000; }
	int IsUsableCell(int Row, int Col) {
		return Row != Col;
	}
#endif
#if WHICH == 5
	int GetFieldHeight() { return 75000; }
	int GetFieldWidth() { return 75000; }
	int IsUsableCell(int Row, int Col) {
		return 1;
	}
#endif

typedef long long ll;
int nodes;

#define INF 1000000000

int divs[109];
int my_max[100009];
int get_max[100009];
int pref[100009];
pair<ll, int> pps[100009];

#define MAX_PER 15500
void send_vec(int who, int* arr, int sz){
	int szx = min(sz, MAX_PER);
	for(int i=0; i<szx; i++){
		PutInt(who, arr[i]);
	}
	Send(who);
	if(sz - szx)
		send_vec(who, arr + szx, sz - szx);
}

void recv_vec(int who, int* arr, int sz){
	int szx = min(sz, MAX_PER);
	Receive(who);
	for(int i=0; i<szx; i++){
		arr[i] = GetInt(who);
	}
	if(sz - szx)
		recv_vec(who, arr + szx, sz - szx);
}

void parallel(){
	int h = GetFieldHeight();
	int w = GetFieldWidth();
	int me = MyNodeId();

	for(int i=0; i<=nodes; i++){
		divs[i] = w*i/nodes;
	}

	int left = divs[me];
	int right = divs[me+1];
	//vector<vector<bool>> board(h);
	for(int i=0; i<h; i++){
		my_max[i] = -1;
		//board[i].resize(right-left+1);
		for(int j=left; j<right; j++){
			//board[i][j-left] = IsUsableCell(i, j);
			if(!IsUsableCell(i, j)){
				my_max[i] = j;
			}
		}
	}

	if(me != 0){
		recv_vec(me - 1, get_max, h);
	}
	else{
		for(int i=0; i<h; i++){
			get_max[i] = -1;
		}
	}

	for(int i=0; i<h; i++){
		my_max[i] = max(my_max[i], get_max[i]);
	}

	if(me != nodes - 1){
		send_vec(me+1, my_max, h);
	}

	for(int i=0; i<h; i++){
		pref[i] = left - get_max[i] - 1;
	}

	ll sum = 0;
	for(int j=left; j<right; j++){
		for(int i=0; i<h; i++){
			if(IsUsableCell(i, j)){
				pref[i]++;
			}
			else{
				pref[i] = 0;
			}
		}
		// { value, index }
		ll psum = 0;
		int ppsind = 0;
		pps[ppsind++] = {-1, -1};
		for(int i=0; i<h; i++){
			pair<int, int> pp(pref[i], i);
			while(pps[ppsind-1].first >= pp.first){
				psum -= pps[ppsind-1].first * (pps[ppsind-1].second - pps[ppsind-2].second);
				ppsind--;
			}
			pps[ppsind++] = (pp);
			psum += pps[ppsind-1].first * (pps[ppsind-1].second - pps[ppsind-2].second);

			/*
			printf("%02lld ", psum);
			for(auto k: pps)
				printf("%lld@%d ", k.first, k.second);
			printf("\n");
			*/

			sum += psum;
		}
		//printf("\n");
	}

	PutLL(0, sum);
	Send(0);

	if(me == 0){
		ll ss = 0;
		for(int i=0; i<nodes; i++){
			Receive(i);
			ss += GetLL(i);
		}
		printf("%lld\n", ss);
	}
}

int main() {
	nodes = NumberOfNodes();
	int w = GetFieldWidth();
	nodes = min(nodes, w);
	if(MyNodeId() >= nodes){
		return 0;
	}
	//serial();
	parallel();
	/*
	if(MyNodeId() == 0){
		printf("\n");
		ground();
		printf("^ ground\n");
	}
	*/
}