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

#include "krazki.h"
#include "message.h"

using namespace std;

vector<pair <long long, int> > pipe;
vector<long long> kraz;

int main(){
    int hei=PipeHeight();
	int NOD=NumberOfDiscs();
	int non=NumberOfNodes();	//if(NumberOfNodes()!=100)printf("%d\n", 1/0);
	int pnon=sqrt(non);
	int jamjest=MyNodeId();
	if(jamjest<pnon*pnon){
	int hp=min(((1LL*jamjest/pnon)*1LL*hei+pnon-1)/pnon +1, 1LL*hei+1), hk=min(((1LL*jamjest/pnon+1)*1LL*hei+pnon-1)/pnon +1 , 1LL*hei+1);
	int dp=min(((1LL*jamjest%pnon)*1LL*NOD+pnon-1)/pnon +1, 1LL*NOD+1), dk=min(((1LL*jamjest%pnon+1)*1LL*NOD+pnon-1)/pnon +1 , 1LL*NOD+1);
	pipe.push_back(make_pair(0, hei+1));
	for(int i=hk-1;i>=hp;--i){
		long long newhole=HoleDiameter(i);
		while(pipe[pipe.size()-1].first>=newhole)pipe.pop_back();
		pipe.push_back(make_pair(newhole, i));
	}
	for(int i=dp;i<dk;++i){
		kraz.push_back(DiscDiameter(i));
	}
	//sort(pipe.begin(), pipe.end());
	int ah=hei+1;
	for(int i=0;i<kraz.size();++i){
		int a=0;int b=pipe.size();
		while(a+1<b){
			int s=a+b;
			s/=2;
			if(kraz[i]>pipe[s].first)a=s;
			else b=s;
		}
		if(pipe.size()!=0)ah=min(ah-1, pipe[a].second-1);
	}
	int hod=1000000000;
	if(jamjest%pnon!=0){
		Receive(jamjest-1);
		hod=GetInt(jamjest-1);
	}
	int cand=hod-kraz.size();
	ah=min(ah, cand);
	if(jamjest%pnon==pnon-1 && jamjest!=pnon*pnon-1){
		Receive(jamjest+pnon);
		hod=GetInt(jamjest+pnon);
		ah=min(ah, hod);
	}
	if(jamjest%pnon!=pnon-1){
		PutInt(jamjest+1,ah);
		Send(jamjest+1);
	}
	else if(jamjest!=pnon-1){
		PutInt(jamjest-pnon,ah);
		Send(jamjest-pnon);
	}
	else{
		if(ah<0)ah=0;
		printf("%d\n", ah);
	}
	}
	return 0;
}