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 "gap.h"
#include<bits/stdc++.h>
using namespace std;
long long findGap(int T, int N)
{
long long l, r, ans = 0;
MinMax(0, 1000000000000000000, &l, &r);
if (T == 1)
{
vector<long long> a(N);
a[0] = l, a[N - 1] = r;
for (int i = 1; i <= N - i - 1; i++)
MinMax(a[i - 1] + 1, a[N - i] - 1, &a[i], &a[N - i - 1]);
long long mx = 0;
for (int i = 0; i + 1 < N; i++)
mx = max(mx, a[i + 1] - a[i]);
return mx;
}
else
{
queue<pair<long long, long long>> q;
q.push({l, r});
while (!q.empty())
{
l = q.front().first, r = q.front().second, q.pop();
if (r - l <= ans)
continue;
if (r - l <= 1)
ans = max(ans, r - l);
else if (r - l == 2)
{
long long tl, tr;
MinMax(l, l + 1, &tl, &tr);
ans = max(ans, 2LL - (tr == l + 1));
}
else if (r - l == 3)
{
long long tl, tr;
MinMax(l + 1, r - 1, &tl, &tr);
ans = max(ans, 3LL - (tl == l + 1) - (tr == r - 1));
}
else
{
long long m = (l + r) / 2, ll, lr, rl, rr;
MinMax(l + 1, m, &ll, &lr), MinMax(m + 1, r - 1, &rl, &rr);
if (ll == -1 && rl == -1)
ans = max(ans, r - l);
else if (ll == -1)
ans = max({ans, rl - l, r - rr}), q.push({rl, rr});
else if (rl == -1)
ans = max({ans, ll - l, r - lr}), q.push({ll, lr});
else
ans = max({ans, ll - l, rl - lr, r - rr}), q.push({ll, lr}), q.push({rl, rr});
}
}
}
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |