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
#include <cstdlib>
#include <bits/stdc++.h>
#include "krazki.h"
//#include <time.h>
#include "message.h"
using namespace std;

#define fru(j,n) for(int j=0; j<(n); ++j)
#define tr(it,v) for(auto it=(v).begin(); it!=(v).end(); ++it)
//#define tr(it,v) for(typeof((v).begin()) it=(v).begin(); it!=(v).end(); ++it)
#define x first
#define y second
#define pb push_back
#define ALL(G) (G).begin(),(G).end()

#if 0
#define DEB printf
#else
#define DEB(...)
#endif

typedef long long ll;
typedef long long LL;
typedef double D;
typedef pair<int,int> pii;
typedef vector<int> vi;

const int inft = 1000000009;
const int mod = 1000000007;
const int INST=80,RO=2;
const int MAXN = 200*1000*1000/INST+10;

const int BB=10;
int N,me,h,d,part;


/*void wai(){
	struct timespec tim;tim.tv_sec=0;tim.tv_nsec=50;
//	CT++;
	nanosleep(&tim,NULL);
};
int PipeHeight(){wai();return _HE;}
int NumberOfDiscs(){wai();return _DI;}

long long HoleDiameter(long long a){wai();assert(a>=1 && a<= _HE); return 1LL* _DI*_DI;}
long long DiscDiameter(long long a){wai();assert(a>=1 && a<= _DI); return 1LL* _DI*_DI;}
*/
void termin(int a){
	if(a==-1){//just broadcast
		int v;
		for(int i=0;i<BB;i++)if((v=BB*me+i)<N){
			PutInt(v,-1);
			Send(v);
		}
		exit(0);
	}
	assert(me==0);
	printf("%d\n",a);
	int v;
	for(int i=1;i<BB;i++)if((v=BB*me+i)<N){
		PutInt(v,-1);
		Send(v);
	}
	exit(0);
}

ll holes[MAXN];
ll discs[RO][MAXN];//decreasing
int att;
pair<ll,ll> range[RO];
ll getDiam(int pos){
	fru(i,RO)if(i!=att)if(pos>=range[i].x && pos<=range[i].y){
		return discs[att][pos-range[att].x]=discs[i][pos-range[i].x];
	}
	return discs[att][pos-range[att].x]=DiscDiameter(pos);
}

bool check(int value){
	att++;
	if(att==RO)att=0;
	//send to children
	int v;
	for(int i=0;i<BB;i++)if((v=BB*me+i)<N && v!=0){
		PutInt(v,value);
		Send(v);
	}
	bool good=1;
	//do local computations [beg,endi)
	int beg=max(value,me*part),endi=min(me*part+part,h);
	range[att].x=d-(endi-1-value);
	range[att].y=d-(beg-value);
	for(int i=beg;i<endi && good;i++){
		ll p1=holes[i-me*part];
		ll p2=i-value+1<=d?getDiam(d-(i-value)):-1;
		if(i-value+1 <=d && p1<p2)good=0;
	}
	//collect children data
	for(int i=0;i<BB;i++)if((v=BB*me+i)<N && v!=0){
		Receive(v);
		good&=GetInt(v);
	}
	return good;
}
int main() {
	fru(i,RO)range[i]=make_pair(-1,-1);
	att=0;
	N=NumberOfNodes();
	me=MyNodeId();
	h=PipeHeight();
	d=NumberOfDiscs();
	part=(h+N-1)/N;
	int beg=me*part,endi=min(me*part+part,h);
	for(int i=beg;i<endi;i++)holes[i-beg]=HoleDiameter(i+1);
	//fix holes
	for(int i=1;i<endi-beg;i++)holes[i]=min(holes[i],holes[i-1]);
	//wait for previous
	ll w=1LL<<62;
	if(me!=0){
		Receive(me-1);
		w=GetLL(me-1);
	}
	if(me+1<N){
		PutLL(me+1,min(w,endi-beg-1>=0?holes[endi-beg-1]:0LL));
		Send(me+1);
	}

	for(int i=0;i<endi-beg;i++)holes[i]=min(holes[i],w);
	for(int i=0;i<endi-beg;i++)DEB("%lld ",holes[i]);DEB("\n");
	//
	if (MyNodeId() == 0) {
		if(!check(0) || d>h)termin(0);
		int p=0,k=h-d+1;
		while(k-p>1){
			int m=(p+k)/2;
			if(check(m))p=m;
			else k=m;
		}
		termin(p+1);
		return 0;
	}else{
		while(1){
			Receive(me/BB);
			int u=GetInt(me/BB);
			if(u==-1)termin(-1);
			PutInt(me/BB,check(u));
			Send(me/BB);
		}
	}
}