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
#include <bits/stdc++.h>
#include "dzialka.h"
#include "message.h"

using namespace std;

typedef long long int LL;

const int N = 75007;
/*
int IsUsableCell(int a, int b){
	return (a + b)%2;
}

int GetFieldHeight(){
	return 10;
}

int GetFieldWidth(){
	return 10;
}

int MyNodeId(){
	return 0;
}

int Receive(int a){
	return a;
}

void Send(int a){
	a += a;
}

int GetInt(int a){
	return a;
}

LL GetLL(int a){
	return a;
}

void PutLL(int a, int b){	
	a += b;
}

void PutInt(int a, int b){
	a += b;
}
*/
int n, m;
int h[N];
int st[N];
bool was[N];
long long int res = 0LL;

void solve(){
	vector <pair <int, int> > cur;
	for(int i = 1; i <= m + 1; ++i){
		int last = i;
		while(cur.size()){
			if(cur.back().first < h[i])
				break;
			
			res += 1LL * (2 * i - last - cur.back().second + 1) * (last - cur.back().second) / 2LL * (cur.back().first - h[i]);
			last = cur.back().second;
			cur.pop_back();
		}
		
		cur.push_back({h[i], last});
	}
}

void przyjmij(int a, int b){
	int id = Receive(MyNodeId() - 1);
	for(int i = a; i <= b; ++i){
		st[i] = GetInt(id);
		if(!was[i])
			h[i] += st[i];
	}
	
	PutInt(MyNodeId() - 1, 1);
	Send(MyNodeId() - 1);
}

void wyslij(int a, int b){
	for(int i = a; i <= b; ++i)
		PutInt(MyNodeId() + 1, h[i]);
	Send(MyNodeId() + 1);
	
	int id = Receive(MyNodeId() + 1);
	GetInt(id);
	return;
}

int main(){
	n = GetFieldHeight(), m = GetFieldWidth();
	if(1LL * n * m <= LL(1e8)){
		if(MyNodeId() > 0)
			return 0;

		for(int i = 1; i <= n; ++i){
			for(int j = 1; j <= m; ++j)
				h[j] = IsUsableCell(i - 1, j - 1) ? h[j] + 1 : 0;
			solve();
		}
		
		printf("%lld\n", res);
		return 0;
	}
	
	int l = (n + 99) / 100;
	int s = MyNodeId() * l;
	int e = min(s + l - 1, n - 1);
	
	for(int i = s; i <= e; ++i){
		for(int j = 1; j <= m; ++j)
			if(IsUsableCell(i, j - 1) == 0){
				was[j] = true;
				h[j] = 0;
			}
			else
				++h[j];
	}
	
	if(MyNodeId() > 0){
		int d = m / 5;
		przyjmij(1, d);
		przyjmij(d + 1, 2 * d);
		przyjmij(2 * d + 1, 3 * d);
		przyjmij(3 * d + 1, 4 * d);
		przyjmij(4 * d + 1, m);
	}
	
	if(MyNodeId() < 99){
		int d = m / 5;
		wyslij(1, d);
		wyslij(d + 1, 2 * d);
		wyslij(2 * d + 1, 3 * d);
		wyslij(3 * d + 1, 4 * d);
		wyslij(4 * d + 1, m);
	}
	
	for(int i = 1; i <= m; ++i)
		h[i] = st[i];
	
	for(int i = s; i <= e; ++i){
		for(int j = 1; j <= m; ++j)
			h[j] = IsUsableCell(i, j - 1) ? h[j] + 1 : 0;
		solve();
	}
	
	if(MyNodeId() > 0){
		PutLL(0, res);
		Send(0);
	}
	else{
		for(int i = 1; i < 100; ++i){
			int id = Receive(i);
			res += GetLL(id);
		}
		
		printf("%lld\n", res);
	}
	
	return 0;
}