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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include <message.h>
#include "krazki.h"

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
#include <bitset>
#include <sstream>
//#include <bits/stdc++.h>
#include <ext/numeric>
using namespace std ;
using namespace __gnu_cxx ;
typedef long long LL ;
typedef pair<int,int> PII ;
typedef vector<int> VI ;
const int INF = 1e9+100 ;
const LL INFLL = (LL)INF*INF ;
#define REP(i,n) for(int i=0;i<(n);++i)
#define ALL(c) c.begin(),c.end()
#define FOREACH(i,c) for(auto i=(c).begin();i!=(c).end();++i)
#define CLEAR(t) memset(t,0,sizeof(t))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second

template<class T1,class T2> ostream& operator<<(ostream &s, const pair<T1,T2> &x) { return s << "(" << x.FI << "," << x.SE << ")" ;}
template<class T>           ostream& operator<<(ostream &s, const vector<T>   &t) { FOREACH(it, t) s << *it << " " ; return s ; }
template<class T>           ostream& operator<<(ostream &s, const set<T>      &t) { FOREACH(it, t) s << *it << " " ; return s ; }
template<class T1,class T2> ostream& operator<<(ostream &s, const map<T1, T2> &t) { FOREACH(it, t) s << *it << " " ; return s ; }

template<class T1, class T2> T1 conv(const T2 &x) {
	stringstream sss ; sss << x ; T1 y ; sss >> y ; return y ;
}

template<class T> void _debug(bool printName, const char* s, const T &x) { if(printName) cerr << s << "=" ; cerr << x << endl ; }
void _debug(bool, const char* s, const char*) {
	for(;*s;s++) if(*s!='"') { cerr << *s ; } cerr << endl ;
}
template<class T, class... R> void _debug(bool printName, const char* s, const T &x, R... y) {
	bool o=0, str=(*s=='"') ;
	for(; o || *s!=',' ; s++) if(*s=='"') o=!o ; else if(printName||str) cerr<<*s ;
	for(s++;*s && *s==' '; s++) ;
	if(!str) { if(printName) cerr << "=" ; cerr << x ; if(*s!='"' && printName) cerr << "," ; } cerr << " " ;
	_debug(printName, s, y...) ;
}

template<class T>             void _coord(const T &x)         { cerr << "[" << x << "]" ;             }
template<class T, class... R> void _coord(const T &x, R... y) { cerr << "[" << x << "]" ; _coord(y...) ; }

template<class T, class I>             void _val(T &t, const I &i)         { cerr << t[i] ; }
template<class T, class I, class... J> void _val(T &t, const I &i, J... j) { _val(t[i], j...) ; }

#define CERR(...)     if(DFLAG) _debug(0, #__VA_ARGS__, __VA_ARGS__)
#define DEBUG(...)    if(DFLAG) _debug(1, #__VA_ARGS__, __VA_ARGS__)	
#define DARRAY(t,...) if(DFLAG) { cerr << #t ; _coord(__VA_ARGS__) ; cerr << " = " ; _val(t,__VA_ARGS__) ; cerr << endl ; }
#define DFLAG 0

////////////////////////////////////////////////////////////////////////////////

// zamyka niepotrzebne workery i zwraca ile pozostalo
int closeTooMany(int dataSize, bool powerOf2=false) {
	int nodes = min(dataSize, NumberOfNodes()) ;
	if(powerOf2) {
		int n = 1 ;
		while(n*2 <= nodes) n*=2 ;
		nodes = n ;
	}
	if(MyNodeId() >= nodes) exit(0) ;
	return nodes ;
}

////////////////////////////////////////////////////////////////////////////////

// odczytuje ciag elementow, poprawiajac go funkcja 'better'
void getSequence(vector<LL> &seq, LL (*get)(LL), LL n, int maxNodeId,
				 							LL startElem, const LL& (*better)(const LL&, const LL&)) {
	if(MyNodeId()>=maxNodeId) return ;
	
	LL start = 1+n*(MyNodeId()  )/maxNodeId ;
	LL end   = 1+n*(MyNodeId()+1)/maxNodeId ;
	assert(start < end) ;
	
	LL elem = startElem ;					// odczytywanie lokalnego kawalka
	for(int i=start ; i<end ; i++) {
		elem = better(elem, get(i)) ;
		seq.PB(elem) ;
	}
	
	LL bestElem = startElem ;
	if(MyNodeId()>0) {						// poprawianie na bazie calosci
		Receive(MyNodeId()-1) ;
		bestElem = GetLL(MyNodeId()-1) ;
	}
	if(MyNodeId()+1 < maxNodeId) {
		PutLL(MyNodeId()+1, better(bestElem, seq.back())) ;
		Send(MyNodeId()+1) ;
	}
	FOREACH(it, seq)
		*it = better(*it, bestElem) ;
}

//////////////////////////

struct interval {
	int start, end ;
	LL firstElem ;
	interval(int ss=0, int ee=0, LL ff=0) :
		start(ss), end(ee), firstElem(ff) {} ;
} ;
ostream &operator<<(ostream &s, const interval &i) {
	return s << "([" << i.start << "," << i.end << "]," << i.firstElem << ")" ;
}
void PutInterval(int target, interval i) {
	PutInt(target, i.start) ;
	PutInt(target, i.end) ;
	PutLL (target, i.firstElem) ;
}
interval GetInterval(int source) {
	interval i ;
	i.start = GetInt(source) ;
	i.end   = GetInt(source) ;
	i.firstElem = GetLL(source) ;
	return i ;
}
/////////////////////////////

int main()
{
	//ios_base::sync_with_stdio(0) ;
	const LL n = PipeHeight(), m = NumberOfDiscs() ;
	const LL allNodes = closeTooMany(max(n,m)) ;
	const LL pipeNodes = min(n, allNodes) ;		// tyle workerow bedzie przetwarzalo rure
	const LL discNodes = min(m, allNodes) ;		// tyle workerow bedzie przetwarzalo dyski
	
	///////////////////////////////////////////////
	
	vector<LL> holes ; //= { INFLL } ;
	getSequence(holes, HoleDiameter, n, pipeNodes, INFLL, min<LL>) ;

	if(MyNodeId()<pipeNodes) {						// wysylam wszystkim swoj opis:
		DEBUG(holes) ;
		REP(i, allNodes) {							
			PutLL(i, holes.back()) ;				// min element
			PutLL(i, holes[0]) ;					// maks element
			Send(i) ;
		}
	}
	vector<pair<LL, LL> > pipesDesc ;				// opis rur
	REP(i, pipeNodes) {
		Receive(i) ;
		LL min = GetLL(i) ;
		LL max = GetLL(i) ;
		pipesDesc.PB(MP(min, max)) ;
	}
	
	if(MyNodeId()==0) {
		DEBUG(pipesDesc) ;
	}
	
	///////////////////////////////////////////////
	
	vector<LL> discs ;
	getSequence(discs, DiscDiameter, m, discNodes, 0, max<LL>) ;
	
	if(MyNodeId()<discNodes) {
		DEBUG(discs) ;
		pipesDesc[0].SE = INFLL ; // powiekszam na wszelki wypadek
		
		vector<pair<interval, int> > intervals ;
		LL start = 1+m*(MyNodeId()  )/discNodes ;
		
		int j=(int)pipesDesc.size()-1 ;
		for(int i=0 ; i<discs.size() ; i++) {
			while(pipesDesc[j].SE < discs[i]) {
				j-- ;
				assert(j>=0) ;
			}
			int i2=i ;
			while(i2+1<discs.size() && discs[i2+1] <= pipesDesc[j].SE)
				i2++ ;
			
			intervals.PB(MP(interval(start+i,start+i2, discs[i]), j)) ;
			i=i2 ;
		}
		
		PutInt(0, intervals.size()) ;
		FOREACH(it, intervals) {
			PutInterval(0, it->FI) ;
			PutInt     (0, it->SE) ;
		}
		Send(0) ;
	}
	
	if(MyNodeId()==0) {
		vector<pair<interval, int> > intervals ;
		REP(i, discNodes) {									// scalam przedzialy
			Receive(i) ;
			int l = GetInt(i) ;
			while(l--) {
				interval inter = GetInterval(i) ;
				int idx        = GetInt(i) ;
				assert(intervals.empty() || intervals.back().FI.end+1 == inter.start) ;
				
				if(!intervals.empty() && intervals.back().SE == idx)
					intervals.back().FI.end = inter.end ;	// rozszerzam
				else
					intervals.PB(MP(inter, idx)) ;
			}
		}
		DEBUG(intervals) ;
		
		vector<bool> wasSend(pipeNodes) ;					// wysylam ktory kawalek maja czytac
		FOREACH(it, intervals) {
			PutInt(it->SE, 1) ;
			PutInterval(it->SE, it->FI) ;
			Send(it->SE) ;
			wasSend[it->SE] = true ;
		}
		REP(i, pipeNodes)
			if(!wasSend[i]) {
				PutInt(i, 0) ;
				Send(i) ;
			}
	}
	
	/////////////////////////////////////////////////////
	
	if(MyNodeId()<pipeNodes) {
		int unused = holes.size(), lastUsed = holes.size(), tooMany=0 ;
		Receive(0) ;
		if(GetInt(0)) {
			interval inter = GetInterval(0) ;
			
			LL disc = inter.firstElem ;
			int i = (int)holes.size()-1 ;
			for(int j=inter.start ; j<=inter.end ; j++) {
				disc = max(disc, DiscDiameter(j)) ;
				while(i>=0 && holes[i] < disc)
					i-- ;
				if(i>=0) {	// umieszczam dysk w i-tym otworze
					unused-- ;
					lastUsed=i ;
					i-- ;
				}
				else tooMany++ ;
			}
			assert(lastUsed<=unused) ;
		}
		
		if(MyNodeId()+1 < pipeNodes) {
			Receive(MyNodeId()+1) ;
			int infoFromPrev = GetInt(MyNodeId()+1) ;
			if(infoFromPrev>=0) {	// nic nie wystaje ; pozycja pierwszego elementu
				if(lastUsed == holes.size())
					lastUsed += infoFromPrev ;
			}
			else {
				int tooManyFromPrev = -infoFromPrev ; // >0
				if(tooManyFromPrev > unused)
					tooMany += tooManyFromPrev-unused ;
				else {
					lastUsed -= max(0, tooManyFromPrev-(unused-lastUsed)) ;
				}
			}
		}
		
		if(MyNodeId()>0) {
			if(tooMany>0)
				PutInt(MyNodeId()-1, -tooMany) ;
			else
				PutInt(MyNodeId()-1, lastUsed) ;
			Send(MyNodeId()-1) ;
		}
		else {
			if(tooMany)
				cout << 0 << endl ;
			else
				cout << lastUsed+1 << endl ;
		}
	}
	
}