#include "xylophone.h"
#include <bits/stdc++.h>
#define ll long long
#define endl "\n"
using namespace std;
map<pair<ll, ll>, ll> mp;
ll ask(ll i, ll j)
{
return mp.count({i, j}) ? mp[{i, j}] : mp[{i, j}] = query(i, j);
}
void solve(int n) {
ll a[n + 1];
ll l = 1, r = n;
while (l <= r)
{
ll mid = (l + r) >> 1;
if (ask(mid, n) == n - 1)
l = mid + 1;
else
r = mid - 1;
}
l--;
a[l] = 1;
if (l + 1 <= n)
a[l + 1] = ask(l, l + 1) + 1;
set<ll> s;
s.insert(1);
s.insert(a[l + 1]);
for (ll i = l + 2; i <= n; i++)
{
ll x = ask(i - 1, i);
if (a[i - 1] - x < 1 or s.count(a[i - 1] - x))
{
a[i] = a[i - 1] + x;
continue;
}
if (a[i - 1] + x > n or s.count(a[i - 1] + x))
{
a[i] = a[i - 1] - x;
continue;
}
ll y = ask(i - 2, i);
for (ll z : {a[i - 1] - x, a[i - 1] + x})
if (max({a[i - 2], a[i - 1], z}) - min({a[i - 2], a[i - 1], z}) == y)
a[i] = z;
s.insert(a[i]);
}
if (l - 1 > 0)
a[l - 1] = ask(l - 1, l) + 1;
s.insert(a[l - 1]);
for (ll i = l - 2; i >= 1; i--)
{
ll x = ask(i, i + 1);
if (a[i + 1] - x < 1 or s.count(a[i + 1] - x))
{
a[i] = a[i + 1] + x;
continue;
}
if (a[i + 1] + x > n or s.count(a[i + 1] + x))
{
a[i] = a[i + 1] - x;
continue;
}
ll y = ask(i, i + 2);
for (ll z : {a[i + 1] - x, a[i + 1] + x})
if (max({a[i + 2], a[i + 1], z}) - min({a[i + 2], a[i + 1], z}) == y)
a[i] = z;
}
// for (ll i = 1; i <= n; i++)
// cout << a[i] << " \n"[i == n];
for (ll i = 1; i <= n; i++)
answer(i, a[i]);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |