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
/* Copyright 2016 AcrossTheSky */
#include <iostream>
#include <cstdio>
#include <utility>
#include <cassert>
#include <map>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <sstream>
#include <fstream>
#include <string>
#include <cmath>
#include <algorithm>
#define REP(i, a, b) for (int i = (a); i <= (b); ++i)
#define PER(i, a, b) for (int i = (a); i >= (b); --i)
#define RVC(i, c) fot (int i = 0; i < (c).size(); ++i)
#define RED(k, u) for (int k = head[(u)]; k; k = edge[k].next)
#define lowbit(x) ((x) & (-(x)))
#define CL(x, v) memset(x, v, sizeof x)
#define MP std::make_pair
#define PB push_back
#define FR first
#define SC second
#define rank rankk
#define next nextt
#define link linkk
#define index indexx
#define abs(x) ((x) > 0 ? (x) : (-(x)))
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;

template<class T> inline
bool getmin(T *a, const T &b) {
    if (b < *a) {
        *a = b;
        return true;
    }
    return false;
}

template<class T> inline
bool getmax(T *a, const T &b) {
    if (b > *a) {
        *a = b;
        return true;
    }
    return false;
}

template<class T> inline
void read(T *a) {
    char c;
    while (isspace(c = getchar())) {}
    bool flag = 0;
    if (c == '-') flag = 1, *a = 0;
    else
        *a = c - 48;
    while (isdigit(c = getchar())) *a = *a * 10 + c - 48;
    if (flag) *a = -*a;
}
const int mo = 1000000007;
template<class T>
T pow(T a, T b, int c = mo) {
    T res = 1;
    for (T i = 1; i <= b; i <<= 1, a = 1LL * a * a % c) if (b & i) res = 1LL * res * a % c;
    return res;
}
/*======================= TEMPLATE =======================*/
#include "krazki.h"
#include "message.h"

const int N = 3000000;

int l, r, cnt, num, n, m, cur;
LL pmin;
LL a[N];

int main() {
    n = PipeHeight();
    num = NumberOfNodes();
    int len = (n + num - 1) / num;
    int D = NumberOfDiscs();
    cur = MyNodeId();
    l = len * cur + 1;
    r = min(n, (cur + 1) * len);
    REP(i, l, r) a[++cnt] = HoleDiameter(i);
    REP(i, 2, cnt) getmin(&a[i], a[i - 1]);
    if (cur) {
        Receive(cur - 1);
        pmin = GetLL(cur - 1);
    } else pmin = 1e18;
    if (cur < num - 1) {
        PutLL(cur + 1, min(pmin, a[cnt]));
        Send(cur + 1);
    }
    
    if (cur == num - 1) m = NumberOfDiscs();
    else {
        Receive(cur + 1);
        m = GetInt(cur + 1);
    }
    if (!m) {
        if (cur)
            PutInt(cur - 1, 0), Send(cur - 1);
        return 0;
    }
    int i = cnt;
    while (i) {
        LL x = DiscDiameter(D - m + 1);
        if (cur && x > pmin) {
            PutInt(cur - 1, m); Send(cur - 1);
            return 0;
        }
        while (i && a[i] < x) --i;
        if (!i) break;
        if (m == 1) {
            cout << l + i - 1 << endl;
            if (cur) PutInt(cur - 1, 0), Send(cur - 1);
            return 0;
        }
        --i; --m;
    }
    if (!cur) cout << 0 << endl;
    else 
        PutInt(cur - 1, m), Send(cur - 1);
    return 0;
}