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 "xylophone.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF (int) (2 * 1e9)
#define MOD (1000 * 1000 * 1000 + 7)
#define maxn 5111
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
//static int A[5000];
int ret[maxn];
bool used[maxn];
void solve(int N){
	int l = 2, d = N, mid, max_ind = N;
	while(l <= d){
		int mid = (l + d) / 2;
		int val = query(1, mid);
		if(val == N - 1){
			max_ind = mid;
			d = mid - 1;
		}
		else
			l = mid + 1; 
	}
	ret[max_ind] = N;
	used[N] = 1;
	int cur = query(max_ind, max_ind + 1);
	ret[max_ind + 1] = N - cur;
	used[N - cur] = 1;
	cur = query(max_ind - 1, max_ind);
	ret[max_ind - 1] = N - cur;
	used[N - cur] = 1;
	for(int i = max_ind + 2; i <= N; i++){
		int diff = query(i - 1, i);
		int opt1 = ret[i - 1] + diff;
		int opt2 = ret[i - 1] - diff;
		if(opt1 < 1 || opt1 > N || used[opt1]){
			ret[i] = opt2;
			used[ret[i]] = 1;
			continue;
		}
		if(opt2 < 1 || opt2 > N || used[opt2]){
			ret[i] = opt1;
			used[ret[i]] = 1;
			continue;
		}
		int updown = query(i - 2, i);
		if(max(ret[i - 2], max(ret[i - 1], opt1)) - min(ret[i - 2], min(ret[i - 1], opt1)) == updown){
			ret[i] = opt1;
		}
		else
			ret[i] = opt2;
		used[ret[i]] = 1;
	}
	for(int i = max_ind - 2; i >= 1; i--){
		int diff = query(i, i + 1);
		int opt1 = ret[i + 1] + diff;
		int opt2 = ret[i + 1] - diff;
		if(opt1 < 1 || opt1 > N || used[opt1]){
			ret[i] = opt2;
			used[ret[i]] = 1;
			continue;
		}
		if(opt2 < 1 || opt2 > N || used[opt2]){
			ret[i] = opt1;
			used[ret[i]] = 1;
			continue;
		}
		int updown = query(i, i + 2);
		if(max(ret[i + 2], max(ret[i + 1], opt1)) - min(ret[i + 2], min(ret[i + 1], opt1)) == updown){
			ret[i] = opt1;
		}
		else
			ret[i] = opt2;
		used[ret[i]] = 1;
	}
	for(int i = 1; i <= N; i++)
		answer(i, ret[i]);
}
Compilation message (stderr)
xylophone.cpp: In function 'void solve(int)':
xylophone.cpp:25:20: warning: unused variable 'mid' [-Wunused-variable]
  int l = 2, d = N, mid, max_ind = N;
                    ^~~| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |