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
// Krzysztof Małysa
#include <bits/stdc++.h>
using namespace std;

#define FOR(i,a,n) for (auto i = (a), i ## __ = (n); i <= i ## __; ++i)
#define FORD(i,a,n) for (auto i = (a), i ## __ = (n); i >= i ## __; --i)
#define REP(i,n) FOR(i, 0, n - 1)
#define ALL(x) x.begin(), x.end()
#define SZ(x) (int(x.size()))
#define EB emplace_back
#define ST first
#define ND second
#define tpv typedef vector<

typedef long long LL;
typedef pair<int, int> PII;
tpv int> VI;
tpv VI> VVI;
tpv PII> VPII;
tpv LL> VLL;

constexpr char nl = '\n';
#define endl nl

#define ris return *this
#define tem template<class T

tem, class B> inline void mini(T& a, B&& b) { if (b < a) a = b; }
tem, class B> inline void maxi(T& a, B&& b) { if (b > a) a = b; }
int ceil2(int x) { return x < 2 ? 1 : 1 << (sizeof(x) * 8 - __builtin_clz(x - 1)); }

tem> struct Dump { T a, b; };
tem> auto dump(T&& x) -> Dump<decltype(x.begin())> { return {ALL(x)}; }
struct Debug {
	~Debug() { cerr << endl; }
	tem> auto operator<<(T x) -> decltype(cerr << x, *this) { cerr << boolalpha << x; return *this; }
	tem> auto operator<<(T x) -> decltype(x.begin(), *this) {
		auto a = x.begin(), b = x.end();
		*this << "{";
		for (; a != b;)
			*this << *a++, *this << (a == b ? "" : " ");
		return *this << "}";
	}
	tem, class B> Debug& operator<<(pair<T, B> p) { ris << "(" << p.ST << ", " << p.ND << ")"; }
	tem> Debug& operator<<(Dump<T> d) {
		*this << "{\n";
		for (T a = d.a, c = a; a != d.b; ++a)
			*this << "  " << distance(c, a) << ": " << *a << '\n';
		ris << "}";
	}
};
struct Foo {tem>Foo& operator<<(T) {ris;}};

#ifdef DEBUG
# define deb Debug()
# undef DEBUG
# define DEBUG(x...) x
# define NODEBUG(x...)
#else
# define deb Foo()
# define DEBUG(x...)
# define NODEBUG(x...) x
#endif
#define imie(x...) #x ": " << (x) << " "
#define LOG(x...) deb << imie(x)
#define DOG(x...) deb << #x ": " << dump(x) << " "

#include "message.h"
#include "dzialka.h"

int MID, NO;
int H, W;

void brut() {
	if (MyNodeId())
		return;

	VI up_free(W), uf2(W);

	LL res = 0;
	REP (i, H) {
		LL current = 0;
		VPII S; // monotonic queue (height, number of columns)
		REP (j, W) {
			int ufij = up_free[j] = (IsUsableCell(i, j) ? 1 + uf2[j] : 0);
			current += ufij;
			int width = 1;
			while (S.size() and S.back().ST > ufij) {
				PII e = S.back();
				current -= e.ND * LL(e.ST - ufij);
				width += e.ND;
				S.pop_back();
			}

			S.EB(ufij, width);
			res += current;
		}
		up_free.swap(uf2);
	}

	cout << res << nl;
}

template<class Func>
void debug_series(Func&& foo) {
	DEBUG(
		if (MID)
			Receive(MID - 1), GetInt(MID - 1);
		else
			deb;

		foo();

		if (MID != NO - 1)
			PutInt(MID + 1, -1), Send(MID + 1);
		else
			PutInt(0, -1), Send(0);

		// Wait for the last one to finish
		if (MID == 0)
			Receive(NO - 1), GetInt(NO - 1);
	)
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	MID = MyNodeId();
	NO = NumberOfNodes();
	H = GetFieldHeight();
	W = GetFieldWidth();

	if (H < NO || W < NO || NO == 1)
		return brut(), 0;

	int h_beg = H * MID / NO;
	int h_end = H * (MID + 1) / NO;

	// Phase 1 (Compute up_free, locally)
	VI up_free(W), prev_upf(W, h_beg);
	FOR (i, h_beg, h_end - 1) {
		REP (j, W)
			up_free[j] = (IsUsableCell(i, j) ? 1 + prev_upf[j] : 0);
		up_free.swap(prev_upf);
	}

	// Phase 2 (Pass down the information)
	up_free.swap(prev_upf);
	if (MID == 0)
		fill(ALL(prev_upf), 0);

	REP (j, W) {
		int iterations = min(800, W - j);
		if (MID != 0) {
			Receive(MID - 1);
			FOR (k, j, j + iterations - 1) {
				prev_upf[k] = GetInt(MID - 1);
				mini(up_free[k], prev_upf[k] + h_end - h_beg);
			}
		}

		if (MID != NO - 1) {
			FOR (k, j, j + iterations - 1)
				PutInt(MID + 1, up_free[k]);
			Send(MID + 1);
		}

		j += iterations - 1;
	}

	// Phase 3 (Compute result in each instance)
	LL res = 0;
	FOR (i, h_beg, h_end - 1) {
		LL current = 0;
		VPII S; // monotonic queue (height, number of columns)
		REP (j, W) {
			int ufij = up_free[j] = (IsUsableCell(i, j) ? 1 + prev_upf[j] : 0);
			current += ufij;
			int width = 1;
			while (S.size() and S.back().ST > ufij) {
				PII e = S.back();
				current -= e.ND * LL(e.ST - ufij);
				width += e.ND;
				S.pop_back();
			}

			S.EB(ufij, width);
			res += current;
		}
		up_free.swap(prev_upf);
	}

	// Phase 4 (collect results)
	if (MID != NO - 1) {
		PutLL(NO - 1, res);
		Send(NO - 1);
		return 0;
	}

	FORD (i, NO - 2, 0) {
		Receive(i);
		res += GetLL(i);
	}

	cout << res << nl;

	return 0;
}