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

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

#define REP(a,b) for(int a=0; a<(b); ++a)
#define FWD(a,b,c) for(int a=(b); a<(c); ++a)
#define FWDS(a,b,c,d) for(int a=(b); a<(c); a+=d)
#define BCK(a,b,c) for(int a=(b); a>(c); --a)
#define ALL(a) (a).begin(), (a).end()
#define SIZE(a) ((int)(a).size())
#define VAR(x) #x ": " << x << " "
#define popcount __builtin_popcount
#define popcountll __builtin_popcountll
#define gcd __gcd
#define x first
#define y second
#define st first
#define nd second
#define pb push_back

using namespace std;

template<typename T> ostream& operator<<(ostream &out, const vector<T> &v){ out << "{"; for(const T &a : v) out << a << ", "; out << "}"; return out; }
template<typename S, typename T> ostream& operator<<(ostream &out, const pair<S,T> &p){ out << "(" << p.st << ", " << p.nd << ")"; return out; }

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, int> PLI;
typedef pair<LL, LL> PLL;
typedef long double K;
typedef vector<int> VI;

const int dx[] = {0,0,-1,1}; //1,1,-1,1};
const int dy[] = {-1,1,0,0}; //1,-1,1,-1};

using namespace std;

const LL INF = 1000LL * 1000 * 1000 * 1000 * 1000 * 1000 + 10;

int get_part(int i, int is, int k){
  return (int)((long long)i*k/is) + 1;
}

int u, us;

queue<PLL> Q0;
queue<int> Q1, Q2, Q3;

void ProcessPrevEx(int from){ LL a = GetLL(from); LL b = GetLL(from); Q0.push(make_pair(a, b)); }
void ProcessDiscBound(int from){ Q1.push(GetInt(from)); }
void ProcessPipeBound(int from){ Q2.push(GetInt(from)); }
void ProcessRes(int from){ Q3.push(GetInt(from)); }

void ReceiveAny(){
  int from = Receive(-1);
  char t = GetChar(from);
  switch(t){
    case 0: ProcessPrevEx(from); break;
    case 1: ProcessDiscBound(from); break;
    case 2: ProcessPipeBound(from); break;
    case 3: ProcessRes(from); break;
  }
}

template<typename T>
T ReceiveTo(queue<T> &Q){
  while(Q.empty()) ReceiveAny();
  T msg = Q.front();
  Q.pop();
  return msg;
}

PLL ReceivePrevEx(){ return ReceiveTo(Q0); }
int ReceiveDiscBound(){ return ReceiveTo(Q1); }
int ReceivePipeBound(){ return ReceiveTo(Q2); }
int ReceiveRes(){ return ReceiveTo(Q3); }

void SendPrevEx(int to, LL a, LL b){
  PutChar(to, 0);
  PutLL(to, a);
  PutLL(to, b);
  Send(to);
}

void SendInt(int to, char type, int v){
  PutChar(to, type);
  PutInt(to, v);
  Send(to);
}

void SendDiscBound(int to, int v){ SendInt(to, 1, v); }
void SendPipeBound(int to, int v){ SendInt(to, 2, v); }
void SendRes(int to, int v){ SendInt(to, 3, v); }

int n, m;

vector<LL> G, PG;

vector<LL> D;
vector<LL> P;
vector<int> IP;

int main() {
  u = MyNodeId();
  us = NumberOfNodes();

  n = PipeHeight();
  m = NumberOfDiscs();

  mt19937_64 gen(1234);
  uniform_int_distribution<int> RP(1,n);
  uniform_int_distribution<int> RD(1,m);

  FWD(i,0,(us-1)*us*2){
    PG.push_back(HoleDiameter(RP(gen))+1);
    PG.push_back(DiscDiameter(RD(gen)));
  }

  sort(PG.begin(), PG.end());
  G.push_back(0);
  FWD(i,1,us) G.push_back(PG[4*i*(us-1)]);
  G.push_back(INF);

  int plo, phi, dlo, dhi;
  plo = get_part(u, us, n);
  phi = get_part(u+1, us, n);
  dlo = get_part(u, us, m);
  dhi = get_part(u+1, us, m);

  P.resize(phi-plo+3);

  LL mip = INF;
  FWD(i,0,phi-plo){
    LL p = HoleDiameter(i+plo)+1;
    P[i] = p;
    mip = min(mip, p);
  }

  D.resize(dhi-dlo+3);

  LL mad = 0;
  FWD(i,0,dhi-dlo){
    LL d = DiscDiameter(i+dlo);
    D[i] = d;
    mad = max(mad, d);
  }

  LL pmip = INF, pmad = 0;
  if(u > 0) tie(pmip, pmad) = ReceivePrevEx();
  if(u < us-1) SendPrevEx(u+1, min(pmip, mip), max(pmad, mad));

  int pg = us-1;
  while(G[pg] > pmip) --pg;

  FWD(i,0,phi-plo){
    pmip = min(pmip, P[i]);
    while(G[pg] > pmip){
      SendPipeBound(pg, plo+i);
      --pg;
      SendPipeBound(pg, plo+i);
    }
  }
  if(u == us-1){
    while(pg){
      SendPipeBound(pg, phi);
      --pg;
      SendPipeBound(pg, phi);
    }
  }

  int dg = 0;
  while(G[dg+1] <= pmad) ++dg;

  FWD(i,0,dhi-dlo){
    pmad = max(pmad, D[i]);
    while(G[dg+1] <= pmad){
      SendDiscBound(dg, dlo+i);
      ++dg;
      SendDiscBound(dg, dlo+i);
    }
  }
  if(u == us-1){
    while(dg+1 != us){
      SendDiscBound(dg, dhi);
      ++dg;
      SendDiscBound(dg, dhi);
    }
  }

  plo = 1; phi = n+1;
  if(u != 0) phi = ReceivePipeBound();
  if(u != us-1) plo = ReceivePipeBound();
  if(plo > phi) swap(plo, phi);

  dlo = 1; dhi = m+1;
  if(u != 0) dlo = ReceiveDiscBound();
  if(u != us-1) dhi = ReceiveDiscBound();
  if(dlo > dhi) swap(dlo, dhi);

  //printf("%d: val: [%lld, %lld) disc: [%d, %d) pipe: [%d, %d)\n", u, G[u], G[u+1], dlo, dhi, plo, phi);

  P.resize(phi-plo+3);
  IP.resize(phi-plo+3);

  int pc = 0;
  LL p;
  FWD(i,plo,phi){
    p = HoleDiameter(i)+1;
    if(p < G[u+1] && (!pc || P[pc-1] > p)){
      IP[pc] = i;
      P[pc] = p;
      ++pc;
    }
  }
  IP[pc] = phi;

  int w = n+1;
  LL d, md = G[u]-1;
  FWD(i,dlo,dhi){
    d = DiscDiameter(i);
    if(d > md){
      md = d;
      while(pc && P[pc-1] <= d){
        --pc;
      }
      w = min(w, IP[pc] - (m - i + 1));
    }
  }

  w = max(w, 0);

  if(u == 0){
    FWD(from,1,us) w = min(w, ReceiveRes());
    printf("%d\n", w);
  }else{
    SendRes(0, w);
  }

  return 0;
}