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 "sequence.h"
#include<bits/stdc++.h>
#define ll int
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ld long double
#define sz(a) ((ll)(a).size())
using namespace std;
const ll maxn=500005, inf=1e9;
vector <ll> p[maxn];
ll a[maxn], minp[maxn], maxp[maxn], mins[maxn], maxs[maxn];
namespace SegTree
{
ll n, Max[maxn*4], Min[maxn*4], lz[maxn*4];
void build(ll i, ll Start, ll End)
{
if (Start==End) {Min[i]=Max[i]=Start; return;}
ll mid=(Start+End)/2;
build(i*2, Start, mid), build(i*2+1, mid+1, End);
Min[i]=min(Min[i*2], Min[i*2+1]);
Max[i]=max(Max[i*2], Max[i*2+1]);
}
void init(ll sz){n=sz, build(1, 0, n);}
void Move(ll i)
{
ll t=lz[i]; lz[i]=0;
Min[i*2]+=t, Max[i*2]+=t, lz[i*2]+=t;
Min[i*2+1]+=t, Max[i*2+1]+=t, lz[i*2+1]+=t;
}
void update(ll i, ll Start, ll End, ll l, ll r, ll x)
{
if (Start>r || End<l) return;
if (Start>=l && End<=r) {Max[i]+=x, Min[i]+=x, lz[i]+=x; return;}
ll mid=(Start+End)/2; if (lz[i]) Move(i);
if (mid>=l) update(i*2, Start, mid, l, r, x);
if (mid+1<=r) update(i*2+1, mid+1, End, l, r, x);
Min[i]=min(Min[i*2], Min[i*2+1]);
Max[i]=max(Max[i*2], Max[i*2+1]);
}
void update(ll l, ll r, ll x) {update(1, 0, n, l, r, x);}
pll query(ll i, ll Start, ll End, ll l, ll r)
{
if (Start>r || End<l) return {1e9, 0};
if (Start>=l && End<=r) return {Min[i], Max[i]};
ll mid=(Start+End)/2; if (lz[i]) Move(i);
if (mid<l) return query(i*2+1, mid+1, End, l, r);
if (mid+1>r) return query(i*2, Start, mid, l, r);
pll q1=query(i*2, Start, mid, l, r), q2=query(i*2+1, mid+1, End, l, r);
return {min(q1.fi, q2.fi), max(q1.se, q2.se)};
}
pll query(ll l, ll r){return query(1, 0, n, l, r);}
}
int sequence(int n, vector <int> A)
{
SegTree::init(n);
for (ll i=1; i<=n; i++) a[i]=A[i-1], p[a[i]].pb(i);
for (ll i=1; i<=n; i++)
{
for (ll j:p[i])
{
SegTree::update(j, n, -2);
tie(minp[j], maxp[j])=SegTree::query(0, j-1);
tie(mins[j], maxs[j])=SegTree::query(j, n);
}
}
auto check=[&](ll mid)
{
for (ll i=1; i<=n; i++)
{
for (ll j=0; j+mid<=sz(p[i]); j++)
{
ll l=p[i][j], r=p[i][j+mid-1];
ll l1=minp[l], r1=maxp[l], l2=mins[r], r2=maxs[r];
if (max(l1, l2)-min(r1, r2)<=mid) return true;
}
}
return false;
};
ll lo=1, hi=n;
while (hi>lo)
{
ll mid=(lo+hi+1)/2;
if (check(mid)) lo=mid;
else hi=mid-1;
}
return lo;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |