제출 #969163

#제출 시각아이디문제언어결과실행 시간메모리
969163Tuanlinh123서열 (APIO23_sequence)C++17
100 / 100
526 ms55940 KiB
#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) { ll ans=0; 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); for (ll z=sz(p[i])-1; z>=0; z--) { ll j=p[i][z]; SegTree::update(j, n, 2); while (z+ans<sz(p[i])) { ll r=p[i][z+ans], l1, r1, l2, r2; tie(l1, r1)=SegTree::query(0, j-1); tie(l2, r2)=SegTree::query(r, n); if (max(l1, l2-2*(ans+1))-min(r1, r2)<=0) ans++; else break; } } for (ll j:p[i]) SegTree::update(j, n, -2); } return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...