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
215
216
217
#include<bits/stdc++.h>
#include"message.h"
#include"dzialka.h"
#define VAR(i,n) __typeof(n) i = (n)
#define loop(i,j,s) for(int i=j;i<s;i++)
#define loopback(i,j,s) for(int i=j;i>=s;i--)
#define foreach(i,c) for(VAR(i,(c).begin());i!=(c).end();i++)
#define pln( x ) cout << x << "\n"
#define ps( x ) cout << x << " "
#define entr cout << "\n"
#define pcnt(i) __builtin_popcount(i)
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define SIZE(c) (c).size()
#define ALL(c) (c).begin(), (c).end()
using namespace std;
typedef vector<int> VI;
const int INFTY=20000000;
const int MAX=3100;
const int MOD=10000000;

void coutTab(int* tab,int n){
	loop(i,0,n){
		cout<<tab[i]<<" ";
	}
	cout<<"\n";
}
//------------------------------------------
bool F[MAX][MAX];
int obsC[MAX][MAX]; //obstacles in every instance
int obsCb[MAX][MAX]; //obstacles back in every instance
int obstN[101][MAX]; //obstacles sended to 0
int obstNb[101][MAX]; //obstacles back sended to 0
int m,s,e;
const int NoN=NumberOfNodes();
const int FH=GetFieldHeight();
const int FW=GetFieldWidth();
int getN(int i,int j){
	int s=(i)*FH/NoN;
	int e=((j+1))*FH/NoN;
	return e-s;
}
//Obstacles counting
void countObst(){
	loop(i,s,e){
		loop(j,0,m){
			F[i-s][j]=IsUsableCell(i,j);
			if(i==s){
				if(!F[i-s][j]) obsC[i-s][j]=0;
				else obsC[i-s][j]=-INFTY;
			}else{
				if(!F[i-s][j]) obsC[i-s][j]=i-s;
				else obsC[i-s][j]=obsC[i-s-1][j];
			}
		}
	}
	loopback(i,e-1,s){
		loop(j,0,m){
			//ps(i);ps(j);ps(s);pln(e);
			if(i==e-1){
				if(!F[i-s][j]) obsCb[i-s][j]=i-s;
				else obsCb[i-s][j]=INFTY;
			}else{
				if(!F[i-s][j]) obsCb[i-s][j]=i-s;
				else obsCb[i-s][j]=obsCb[i-s+1][j];
			}
		}
	}
	/*loop(i,s,e){
		loop(j,0,m){
			ps(obsC[i-s][j]);
		}
		entr;
	}
	entr;*/
}
int readObs(int i,int j){
	/*int v=0;
	int ic=i;
	while(i>=0&&obstN[i][j]==-INFTY){
		v+=n;
		i--;
	}*/
	if(getN(i,i)==0) return 0;
	else if(obstN[i][j]==-INFTY) return getN(i,i);
	return getN(i,i)-obstN[i][j]-1;
}
int readObsB(int i,int j){
	int v=0;
	int ic=i;
	while(i<NoN&&obstNb[i][j]==INFTY){
		v+=getN(i,i);
		i++;
	}
	if(i==NoN) return (FH-getN(0,ic-1));
	return v+obstNb[i][j];
}
//Min tree
int tree[2*MAX],tree2[2*MAX];
int D;
void countD(int m){
	D=1;
	while(D<=m) D*=2;
}
void createTree(int tree[2*MAX]){
	loopback(i,D-1,1){
		tree[i]=min(tree[2*i],tree[2*i+1]);
	}
}
int read(int tree[2*MAX],int L,int R){
	L+=D;
	R+=D;
	int v=INFTY;
	while(L<=R){
		if(L%2==1) v=min(v,tree[L]);
		if(R%2==0) v=min(v,tree[R]);
		L=(L+1)/2;
		R=(R-1)/2;
	}
	//v=min(v,tree[L]);
	return v;
}
int main(){
	ios_base::sync_with_stdio(0);
	m=FW;
	s=(MyNodeId())*FH/NoN;
	e=((MyNodeId()+1))*FH/NoN;
	loop(i,s,e+2){
		loop(j,0,m){
			obsC[i-s][j]=-INFTY;
			obsCb[i-s][j]=INFTY;
		}
	}
	countObst();
	ll c=0;
	countD(m);
	int N=getN(MyNodeId(),MyNodeId());
	//pln(N);
	loop(r,s,e){
		loop(i,0,m){
			if(obsC[r-s][i]!=-INFTY) tree[i+D]=r-s-obsC[r-s][i];
			else tree[i+D]=INFTY;
		}
		createTree(tree);
		loop(i,0,m){
			loop(j,i,m){
				int m=INFTY;
				/*loop(k,i,j+1){
					if(obsC[r-s][k]!=-INFTY) m=min(m,r-s-obsC[r-s][k]);
				}*/
				m=read(tree,i,j);
				//ps(r);ps(i);ps(j);pln(m);
				if(m==INFTY) c+=(r-s+1);
				else c+=(m);
					
			}
		}
	}
	if(MyNodeId()!=0){
		PutLL(0,c);
		loop(i,0,m) PutInt(0,obsC[getN(MyNodeId(),MyNodeId())-1][i]);
		loop(i,0,m) PutInt(0,obsCb[0][i]);
		Send(0);
	}else{
		loop(i,0,m){
			obstN[0][i]=obsC[getN(0,0)-1][i];
			obstNb[0][i]=obsCb[0][i];
		}
		loop(i,1,NoN){
			int nod=Receive(-1);
			c+=GetLL(nod);
			loop(i,0,m) obstN[nod][i]=GetInt(nod);
			loop(i,0,m) obstNb[nod][i]=GetInt(nod);
		}
	}
	//pln(c);
	if(MyNodeId()==0){
		/*loop(i,0,NoN){
			loop(j,0,m){
				ps(obstNb[i][j]);
			}
			entr;
		}*/
		loop(r,0,NoN-1){
			loop(j,0,m){
				tree[j+D]=readObs(r,j);
				//ps(rddObs[j][0]);
			}
			//entr;
			loop(j,0,m){
				tree2[j+D]=readObsB(r+1,j);
				//ps(rddObs[j][1]);
			}
			createTree(tree);
			createTree(tree2);
			//entr;entr;
			loop(i,0,m){
				loop(j,i,m){
					int m=INFTY,m2=INFTY;
					/*loop(k,i,j+1){
						m=min(rddObs[k][0],m);
						m2=min(rddObs[k][1],m2);
					}*/
					m=read(tree,i,j);
					m2=read(tree2,i,j);
					c+=(m*m2);
					//ps(i);ps(j);ps(m);pln(m2);
				}
			}
		}
		pln(c);
	}
	
}