# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
654075 | thiago_bastos | Xylophone (JOI18_xylophone) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
void solve(int N) {
int l = 1, r = N + 1;
while(l < r) {
int m = (l + r) / 2;
if(query(m, N) != N - 1) r = m;
else l = m + 1;
}
int k = r - 1;
vector<int> P(N + 1, -1);
vector<bool> vis(N + 1, false);
P[k] = 1;
P[k + 1] = 1 + query(k, k + 1);
vis[1] = vis[P[k + 1]] = true;
auto f = [&](int l, int r, int i) {
int d = query(min({l,r,i}), max({l,r,i}));
if(d == abs(P[l] - P[r]))
P[i] = P[l] > P[r] ? P[l] - query(min(l,i), max(l,i)) : P[l] + query(min(l,i), max(l,i));
else {
int lo = min(P[l], P[r]) + d, hi = max(P[l], P[r]) - d;
if(lo <= N && hi >= 1 && !vis[lo] && !vis[hi]) {
int d2 = query(min(l,i), max(l,i));
P[i] = P[l] > P[r] ? (P[l] + (d == d2 ? -d : d2)) : (P[l] + (d == d2 ? d : -d2));
} else
P[i] = lo > N || vis[lo] ? hi : lo;
}
vis[P[i]] = true;
};
for(int i = k + 2; i <= N; ++i) f(i - 1, i - 2, i);
for(int i = k - 1; i; --i) f(i + 1, i + 2, i);
for(int i = 1; i <= N; ++i) answer(i, P[i]);
}