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
#include "dzialka.h"
#include "message.h"
#include <bits/stdc++.h>
using namespace std;
#define PB push_back
#define MP make_pair
#define LL long long
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define FORD(i,a,b) for(int i = (a); i >= (b); i--)
#define RE(i,n) FOR(i,1,n)
#define REP(i,n) FOR(i,0,(int)(n)-1)
#define R(i,n) REP(i,n)
#define VI vector<int>
#define PII pair<int,int>
#define LD long double
#define FI first
#define SE second
#define st FI
#define nd SE
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define M_PI 3.14159265358979323846
template<class C> void mini(C& _a4, C _b4) { _a4 = min(_a4, _b4); }
template<class C> void maxi(C& _a4, C _b4) { _a4 = max(_a4, _b4); }

template<class TH> void _dbg(const char *sdbg, TH h) { cerr << sdbg << '=' << h << endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
	while (*sdbg != ',')cerr << *sdbg++; cerr << '=' << h << ','; _dbg(sdbg + 1, a...);
}

template<class T> ostream& operator<<(ostream& os, vector<T> V) {
	os << "["; for (auto& vv : V) os << vv << ","; os << "]";
	return os;
}

template<class T> ostream& operator<<(ostream& os, set<T> V) {
	os << "["; for (auto& vv : V) os << vv << ","; os << "]";
	return os;
}

template<class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
	return os << "(" << P.st << "," << P.nd << ")";
}


//#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
//#else
//#define debug(...) (__VA_ARGS__)
//#endif
inline int top(int a, int b) {
	return (a + b - 1) / b;
}
int nodes = NumberOfNodes();
int myId = MyNodeId();
int totalHeight = GetFieldHeight();
int totalWidth = GetFieldWidth();
int widthInterval = top(totalWidth, nodes);
const int masterNode = 0;
PII myInterval = {
	myId * widthInterval,
	(myId + 1) * widthInterval
};

LL solve(VI rvector) {
	if (!totalWidth)return 0;
	LL res = 0;
	FORD(j, myInterval.second - 1, myInterval.first) {
		vector<pair<LL,LL>> stos;
		LL sum = 0;
		REP(i, totalHeight) {
			if (IsUsableCell(i, j)) {
				rvector[i]++;
			}
			else {
				rvector[i] = 0;
			}
			LL co = 1;
			while (!stos.empty() && stos.back().first >= rvector[i]) {
				co += stos.back().second;
				sum -= stos.back().first * stos.back().second;
				stos.pop_back();
			}
			stos.push_back({ rvector[i], co });
			sum += rvector[i] * co;
			res += sum;
		}
	}
	return res;
}

inline void transpose_scalar_block(float *A, float *B, const int lda, const int ldb, const int block_size) {
#pragma omp parallel for
	for (int i = 0; i<block_size; i++) {
		for (int j = 0; j<block_size; j++) {
			B[j*ldb + i] = A[i*lda + j];
		}
	}
}

inline void transpose_block(float *A, float *B, const int n, const int m, const int lda, const int ldb, const int block_size) {
#pragma omp parallel for
	for (int i = 0; i<n; i += block_size) {
		for (int j = 0; j<m; j += block_size) {
			transpose_scalar_block(&A[i*lda + j], &B[j*ldb + i], lda, ldb, block_size);
		}
	}
}

const int chunk = 15000;
void sendVector(VI dupcia, int dest) {
	int sz = SZ(dupcia);
	
	for (int i = 0; i < sz; i += chunk) {
		for (int j = i; j < i + chunk && j < sz; j++) {
			PutInt(dest, dupcia[j]);
		}
		Send(dest);
	}
}

VI receiveVector(int src, int sz) {
	VI res;
	for (int i = 0; i < sz; i += chunk) {
		//debug("Receive", src);
		Receive(src);
		for (int j = i; j < i + chunk && j < sz; j++) {
			res.push_back(GetInt(src));
		}
	}
	return res;
}

int32_t main(int32_t argc, char ** argv) {
	ios_base::sync_with_stdio(0);
	mini(myInterval.first, totalWidth);
	mini(myInterval.second, totalWidth);

	int myWidth = myInterval.second - myInterval.first;
	VI toSend(totalHeight);
	if (myWidth) {
		REP(i, totalHeight) {
			REP(j, myWidth) {
				if (!IsUsableCell(i, myInterval.first + j)) {
					break;
				}
				toSend[i]++;
			}
		}
	}	
	VI rVector(totalHeight);
	if (myId != nodes - 1) {
		rVector = receiveVector(myId + 1, totalHeight);
		if (myWidth) {
			REP(i, totalHeight) {
				if (toSend[i] == myWidth) {
					toSend[i] += rVector[i];
				}
			}
		}
	}
	if (myId != 0) {
		sendVector(toSend, myId - 1);
	}
	LL myScore = solve(rVector);
	PutLL(masterNode, myScore);
	Send(masterNode);
	if (myId == masterNode) {
		LL score = 0;
		REP(i, nodes) {
			Receive(i);
			score += GetLL(i);
		}
		cout << score << "\n";
	}
	return 0;
}