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
#include "dzialka.h"
#include "message.h"
#include<iostream>
#include<algorithm>
#include<queue>
#include<list>
#include<stdio.h>
#include<vector>
#include<map>
#include<set>
using namespace std;
#define ull long long int
#define FOR(i,n) for(int i=0;i<n;++i)
#define FORD(i,n) for(int i=(n)-1;i>=0;--i)
#define dcout 0 && cout
int inline min(int a,int b){return a>b?b:a;}
int inline max(int a,int b){return a<b?b:a;}
class N{
public:
	ull sum;
	ull each;
	bool rem;
	N():sum(0LL),each(0LL),rem(0LL){}
	void reset(){
		sum=0LL;
		each=0LL;
		rem=0;
	}
};
int kcc=0;
class MyTree{
	public:
		ull s=0;
		N***h;
		MyTree(){
			h=new N**[20];
			h[0]=new N*[1<<20];
			FOR(i,1<<19)h[0][i]=new N();
			FOR(i,18)h[17-i]=*h+(1<<i)-1+i;
		}
		void cutAdd(int point){
			if(!point){h[17][0]->rem=1;s=0;return;}
			s+=point;
			s-=cutAdd(17,0,point, 0);
		}
		ull cutAdd(int level, int node, int point, int v){//++kcc;
			N*th=h[level][node];
			int node2=node<<1;
			int size=1<<level;
			int lev1 = level-1;
			int start = (size)*node;
			if(th->rem){
				th->sum=th->each=th->rem=0;//th->reset();
				if(level && point>start){
					N**rt=h[lev1]+node2;
					(*rt)->rem=1;
					(*++rt)->rem=1;
				}
			}
			th->sum+=v*size;
			if(point<=start){
				//remove
				 th->rem=1;
				 return th->sum;
			}
			th->each+=v;
			int end = (size)*(node+1);
			th->sum+= (min(end,point)-start);
			
			if(end<=point){
				++(th->each);
				return 0;
			}

			if(level==0)return 0;
	
			ull k = cutAdd(lev1, node2, point, th->each);
			k+=cutAdd(lev1, (node2)|1, point, th->each);
			th->each=0;
			th->sum-=k;
			return k;

		}
};
int main(){
	int v=GetFieldHeight();
	int h=GetFieldWidth();

	int n=NumberOfNodes();
	int me=MyNodeId();//if(me==0){MyTree*ii=new MyTree();FOR(i,1){ii->cutAdd(i*1000+200);}cout<<kcc<<endl;return 0;}return 0;

	int hc=h/(n);
	int vc=v/(n);

	int*startHc=new int[n];
	FOR(i,n)startHc[i]=hc*i;
	int*startVc=new int[n];
	FOR(i,n)startVc[i]=vc*i;
	int*endVc=new int[n];
	FOR(i,n)endVc[i]=(i==n-1)?v:startVc[i+1];
	int*endHc=new int[n];
	FOR(i,n)endHc[i]=(i==n-1)?h:startHc[i+1];

	int myChunkSize = endHc[me]-startHc[me];
	int*slupki=new int[myChunkSize];
	FOR(i,myChunkSize)slupki[i]=0;
	int*row=new int[h];
	FOR(i,n){
		if(i==me){
			FOR(k,myChunkSize)row[startHc[me]+k]=slupki[k];
		}else{
			FOR(k,myChunkSize)PutInt(i,slupki[k]);
			Send(i);
		}
		
		for(int j=startVc[i];j<endVc[i];++j){
			for(int k=startHc[me];k<endHc[me];++k){
				if(IsUsableCell(j,k)){
					++slupki[k - startHc[me]];
				}else{slupki[k - startHc[me]]=0;}
			}
		}
	}
	FOR(i,n){
		if(i==me)continue;
		Receive(i);
		for(int j=startHc[i];j<endHc[i];++j){
			row[j]=GetInt(i);

		}
	}

	MyTree*t=new MyTree();
	ull s=0;
	for(int j=startVc[me];j<endVc[me];++j){
		t->cutAdd(0);//continue;
		FOR(i,h){
			if(IsUsableCell(j,i)){
				//++row[i];
				//t->cut(row[i]);
				//t->add(0, row[i]);
				t->cutAdd(++row[i]);
				//cout<<t->s<<endl;
				s+=t->s;
			}else{
				row[i]=0;
				t->cutAdd(0);
			}
		}
	}
	if(me!=0){
		PutLL(0,s);
		Send(0);
	}else{
		FOR(i,n){
		if(!i)continue;
			Receive(i);
			s+=GetLL(i);
		}
	}
	if(me==0)cout<<s<<endl;
	//if(me==0)cout<<kcc<<endl;
	return 0;
}