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
#include<bits/stdc++.h>
#define ALL(X)        X.begin(),X.end()
#define FOR(I,A,B)    for(int (I) = (A); (I) <= (B); (I)++)
#define FORW(I,A,B)   for(int (I) = (A); (I) < (B);  (I)++)
#define FORD(I,A,B)   for(int (I) = (A); (I) >= (B); (I)--)
#define CLEAR(X)      memset(X,0,sizeof(X))
#define SIZE(X)       int(X.size())
#define CONTAINS(A,X) (A.find(X) != A.end())
#define PB            push_back
#define MP            make_pair
#define X             first
#define Y             second
using namespace std;
template<typename T, typename U> ostream& operator << (ostream& os, const pair<T, U> &_p) { return os << "(" << _p.X << "," << _p.Y << ")"; }
template<typename T> ostream& operator << (ostream &os, const vector<T>& _V) { bool f = true; os << "["; for(auto v: _V) { os << (f ? "" : ",") << v; f = false; } return os << "]"; }
template<typename T> ostream& operator << (ostream &os, const set<T>& _S) { bool f = true; os << "("; for(auto s: _S) { os << (f ? "" : ",") << s; f = false; } return os << ")"; }
template<typename T, typename U> ostream& operator << (ostream &os, const map<T, U>& _M) { return os << set<pair<T, U>>(ALL(_M)); }
typedef signed long long slong;
typedef long double ldouble;
const slong INF = 1000000100;
const ldouble EPS = 1e-9;
const slong inf = INF * INF;

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

slong n, m, proc, id;

#define MASTER 0
const int MAXSEG = 3504242;
slong _pipe[MAXSEG];

typedef pair<slong,slong> pll;

#define cerr if(0)cout

void slave(slong a, slong b, slong c, slong d) // segment [a, b) of pipe and [c, d) of discs
{
    cerr << "proc:" << id << " pipe:" << MP(a,b) << " disc:" << MP(c,d) << endl;
    //cout << id << " : " << a << " " << b << endl;

    slong minD = inf; // min pipe diam in segment
    slong* pipe = _pipe - a + 100000; // [a,b] range for pipe
    FORW(i,a,b)
    {
        pipe[i] = HoleDiameter(i);
        minD = min(minD, pipe[i]);
        pipe[i] = min(pipe[i], minD);
    }
    if(b > n)
    {
        minD = 0;
    }

    slong maxD = 0; // max disc diam in segment
    FORW(i,c,d)
    {
        maxD = max(maxD, DiscDiameter(i));
    }

    // Send minD and maxD to all
    FORW(i,0,proc)
    {
        PutLL(i, minD);
        PutLL(i, maxD);
        Send(i);
    }

    vector<slong> seg_diam(proc), max_pref(proc);
    // Receive minDs, seg_diam will be nonincreasing
    FORW(i,0,proc)
    {
        Receive(i);
        seg_diam[i] = GetLL(i);
        max_pref[i] = GetLL(i);
        if(i)
        {
            seg_diam[i] = min(seg_diam[i], seg_diam[i-1]);
            max_pref[i] = max(max_pref[i], max_pref[i-1]);
        }
    }

    cerr << "proc:" << id << " seg_diam:" << seg_diam << endl;

    // Node i will take disc segment [st[i],en[i]) from us
    vector<int> st(proc,c), en(proc,0);
    int target = proc-1, cur_st = c;
    slong cur_diam = 0;
    if(id > 0)
    {
        cur_diam = max_pref[id-1];
    }

    FORW(i,c,d)
    {
        slong disc = DiscDiameter(i);
        cur_diam = max(cur_diam, disc);
        disc = max(disc, cur_diam);
        int target_changed = 0;
        while(target > 0 && seg_diam[target-1] < disc)
        {
            target--;
            target_changed = 1;
            st[target] = i;
        }
        en[target] = i+1;
    }

    // Send computed disc segments
    FORW(i,0,proc)
    {
        PutLL(i, st[i]);
        PutLL(i, en[i]);
        Send(i);
    }

    slong cnt = 0;
    vector<pll> disc_seg(proc);
    // Receive disc segments
    FORW(i,0,proc)
    {
        Receive(i);
        disc_seg[i].X = GetLL(i);
        disc_seg[i].Y = GetLL(i);
        cnt += max(0LL, disc_seg[i].Y - disc_seg[i].X);
    }

    cerr << "proc:" << id << " disc_seg:" << disc_seg << endl;

    // Calculate max and min depth for the received discs
    int max_depth = 0, min_depth = 0;
    int cur_depth = b - 1;
    cur_diam = 0; // value shouldn't matter, not used if a >= b
    if(a < b)
    {
        cur_diam = pipe[cur_depth];
    }

    int over = 0; // discs outside of the pipe segment
    int processed = 0;
    FORW(j,0,proc)
    {
        if(over) continue;
        FORW(i,disc_seg[j].X,disc_seg[j].Y)
        {
            slong disc = DiscDiameter(i);
            while(cur_depth > a && disc > cur_diam)
            {
                cur_depth--;
                cur_diam = pipe[cur_depth];
            }
            // rest of the disc will be outside of the segment
            if(cur_depth < a || disc > cur_diam)
            {
                over = cnt - processed;
                break;
            }
            // disc i lands on depth cur_depth
            if(!max_depth)
            {
                max_depth = cur_depth;
            }
            min_depth = cur_depth;
            processed++;

            cur_depth--;
            if(cur_depth >= a)
            {
                cur_diam = pipe[cur_depth];
            }
        }
    }

    if(over == cnt)
    {
        max_depth = a - 1;
        min_depth = a - cnt;
        over = 0;
    }

    //printf("id:%lld wyszlo max_depth:%lld min_depth:%lld i over:%d\n", id,max_depth,min_depth,over);
    //min_depth -= over;
    if(over)
    {
        min_depth = a - over;
    }
    min_depth = max(0, min_depth);

    // Send (max_depth,min_depth,cnt) to master
    PutLL(MASTER, max_depth);
    PutLL(MASTER, min_depth);
    PutLL(MASTER, cnt);
    Send(MASTER);
}

void master()
{
    // Start from deepest segments and move up
    slong cur_depth = inf;
    FORD(i,proc-1,0)
    {
        Receive(i);
        slong max_depth = GetLL(i);
        slong min_depth = GetLL(i);
        slong cnt = GetLL(i);
        if(cnt == 0) continue;
        // If cur_depth < min_depth + cnt discs will go up
        // (without holes, as pipe is wider upwards)
        slong over = max(0LL, (min_depth+cnt) - cur_depth);
        cur_depth = min_depth - over;
    }
    cur_depth = max(0LL,cur_depth);
    cout << cur_depth << endl;
    //cout << "OUT[" << cur_depth << "]" << endl;
}

int main()
{
    n = PipeHeight();
    m = NumberOfDiscs();
    proc = NumberOfNodes();
    id = MyNodeId();
    slong N = (1<<n);

    slong psegm = (n + proc - 1) / proc; // pipe segment length
    slong dsegm = (m + proc - 1) / proc; // disc segment length
    slave(min(psegm*id,n)+1, min(psegm*(id+1), n)+1,
          min(dsegm*id,m)+1, min(dsegm*(id+1), m)+1);
    if(id == 0) master();
    return 0;
}