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
#include <bits/stdc++.h>
#include "cielib.h"
using namespace std;
#define REP(i,n) for(int _n=(n), i=0;i<_n;++i)
#define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)
#define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i)
#define TRACE(x) cerr << "TRACE(" #x ")" << endl;
#define DEBUG(x) cerr << #x << " = " << (x) << endl;
typedef long long LL; typedef unsigned long long ULL;
const int INF = 1000000000; const LL INFLL = LL(INF) * LL(INF);
template<class T> inline int size(const T&c) { return c.size(); }

class Solver {
 public:
  void solve();
 private:
  int D;
  int R;
  vector<int> current;
  vector<int> currentCopy;

  struct Exact {
    int start;
    int dir;
  };
  vector<Exact> exact;

  void initMiddle();
  bool improveBy1();
  void findAllExact();
  void solve1D();

  int compareSingle(int d, int x);

  int compute1DMax();
  int compare1D(int x);
  void goto1DNoQuery(int x);
  bool goto1D(int x);

  bool query();
  void done();
};

void Solver::solve() {
  D = podajD();
  R = podajR();

  exact.assign(D, Exact{0,0});

  initMiddle();

  if (improveBy1()) {
    findAllExact();
    solve1D();
  }
  done();
}

void Solver::initMiddle() {
  current.assign(D, R/2);
  query();
}

bool Solver::improveBy1() {
  REP(d, D) {
    const int minus = compareSingle(d, current[d]-1);
    const int plus = compareSingle(d, current[d]+1);
    if (minus > 0 && plus > 0) {
      // we're at distance 0
      return false;
    } else if (minus > 0) {
      exact[d] = Exact{current[d]+1, 1};
    } else if (plus > 0) {
      exact[d] = Exact{current[d]-1, -1};
    }
  }

  REP(d, D) {
    if (exact[d].dir) {
      current[d] = exact[d].start;
    }
  }
  const bool improved = query();
  assert(improved);

  return true;
}

void Solver::findAllExact() {
  REP(d, D) {
    if (exact[d].dir) continue;
    int alpha = 0;
    int beta = 0;
    int start = 0;
    int dir = 0;
    if (compareSingle(d, 0) > 0) {
      start = 0;
      dir = 1;
      beta = current[d];
    } else {
      start = R;
      dir = -1;
      beta = R-current[d];
    }
    while(beta-alpha>1) {
      const int gamma = (alpha+beta)/2;
      if(compareSingle(d, start + dir * gamma) > 0) {
        alpha = gamma;
      } else {
        beta = gamma;
      }
    }
    exact[d] = Exact{start + dir * beta, dir};
  }
}

void Solver::solve1D() {
  int m = compute1DMax();
  goto1D(0);
  if (goto1D(m)) {
    REP(d,D) {
      const int newdir = -exact[d].dir;
      exact[d] = Exact{current[d], newdir};
    }
  } else {
    goto1D(0);
  }
  // dist(m) >= dist(0)
  // Find last x such that dist(x) <= dist(0)
  int alpha = 0;
  int beta = m+1;
  while (beta - alpha > 1) {
    const int gamma = (alpha+beta)/2;
    if (compare1D(gamma) <= 0) {
      alpha = gamma;
    } else {
      beta = gamma;
    }
  }
  assert(alpha%2==0);
  goto1DNoQuery(alpha/2);
}

int Solver::compareSingle(const int d, const int x) {
  const int old = current[d];
  current[d] = x;
  const int res1 = query();
  current[d] = old;
  const int res2 = query();
  return res2 - res1;
}

int Solver::compute1DMax() {
  int m = R;
  REP(d,D) {
    const int v =
      exact[d].dir > 0 ?
      R - exact[d].start :
      exact[d].start;
    m = min(m, v);
  }
  return m;
}

inline int Solver::compare1D(const int x) {
  const int res1 = goto1D(x);
  const int res2 = goto1D(0);
  return res2 - res1;
}

inline bool Solver::goto1D(const int x) {
  goto1DNoQuery(x);
  return query();
}

void Solver::goto1DNoQuery(const int x) {
  REP(d,D) {
    current[d] = exact[d].start + exact[d].dir * x;
  }
}

bool Solver::query() {
  currentCopy = current;
  return czyCieplo(currentCopy.data());
}

void Solver::done() {
  currentCopy = current;
  znalazlem(currentCopy.data());
}

int main() {
  Solver solver;
  solver.solve();
}