이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int nmax = 5e5;
int dif;
int n;
int v[nmax + 5];
vector<int> l[nmax + 5];
int st[nmax + 5], dr[nmax + 5];
int pst[nmax + 5], pdr[nmax + 5];
struct arbore_lazy
{
pair<int,int> ai[4 * nmax + 5];
int lazy[4 * nmax + 5];
private:
pair<int,int> Merge(pair<int,int> a, pair<int,int> b)
{
pair<int,int> rez;
rez.first = min(a.first,b.first);
rez.second = max(a.second,b.second);
return rez;
}
void propag(int nod)
{
if(lazy[nod]==0)
{
return;
}
lazy[nod*2] += lazy[nod];
lazy[nod*2+1] += lazy[nod];
ai[nod*2].first += lazy[nod], ai[nod*2].second += lazy[nod];
ai[nod*2+1].first += lazy[nod], ai[nod*2+1].second += lazy[nod];
lazy[nod] = 0;
}
public:
void update(int ua, int ub, int val, int nod, int a, int b)
{
if(ua<=a && ub>=b)
{
ai[nod].first += val;
ai[nod].second += val;
lazy[nod] += val;
return;
}
propag(nod);
int mij = (a + b) >> 1;
if(ua<=mij)
{
update(ua,ub,val,nod*2,a,mij);
}
if(ub>mij)
{
update(ua,ub,val,nod*2+1,mij+1,b);
}
ai[nod] = Merge(ai[nod*2],ai[nod*2+1]);
}
pair<int,int> query(int qa, int qb, int nod, int a, int b)
{
if(qa<=a && qb>=b)
{
return ai[nod];
}
propag(nod);
int mij = (a + b) >> 1;
if(qa<=mij && qb>mij)
{
return Merge(query(qa,qb,nod*2,a,mij),query(qa,qb,nod*2+1,mij+1,b));
}
if(qa<=mij)
{
return query(qa,qb,nod*2,a,mij);
}
return query(qa,qb,nod*2+1,mij+1,b);
}
};
arbore_lazy asp;
struct arbore_de_intervale
{
int ai[12 * nmax + 5];
void update(int poz, int val, int nod, int a, int b, bool as = false)
{
if(a==b)
{
if(as)
{
ai[nod] = val;
}
ai[nod] = min(ai[nod],val);
return;
}
int mij = (a + b) >> 1;
if(poz<=mij)
{
update(poz,val,nod*2,a,mij,as);
}
else
{
update(poz,val,nod*2+1,mij+1,b,as);
}
ai[nod] = min(ai[nod*2],ai[nod*2+1]);
}
int query(int qa, int qb, int nod, int a, int b)
{
if(qa > qb)
{
return nmax;
}
if(qa<=a && qb>=b)
{
return ai[nod];
}
int mij = (a + b) >> 1;
if(qa<=mij && qb>mij)
{
return min(query(qa,qb,nod*2,a,mij),query(qa,qb,nod*2+1,mij+1,b));
}
if(qa<=mij)
{
return query(qa,qb,nod*2,a,mij);
}
return query(qa,qb,nod*2+1,mij+1,b);
}
};
arbore_de_intervale aicnt;
vector<int> a;
int get_val(int val)
{
int st = 1;
int dr = a.size();
int poz = dr+1;
while(st<=dr)
{
int mij = (st + dr) >> 1;
if(a[mij-1]>=val)
{
poz = mij;
dr = mij - 1;
}
else
{
st = mij + 1;
}
}
return poz;
}
int sequence(int N, vector<int> A)
{
n = N;
for(int i=1; i<=n; i++)
{
v[i] = A[i - 1];
l[v[i]].push_back(i);
}
for(int i=1; i<=n; i++)
{
asp.update(i,n,+1,1,1,n);
}
int rez = 0;
for(int i=1;i<=2*n;i++)
{
aicnt.ai[i] = nmax;
}
for(int val=1; val<=n; val++)
{
int nr = l[val].size();
if(!nr)
{
continue;
}
for(auto it : l[val])
{
asp.update(it,n,-1,1,1,n);
}
if(l[val].front() != 1)
{
pair<int,int> sp = asp.query(1,l[val].front()-1,1,1,n);
st[0] = sp.first;
dr[0] = sp.second;
st[0] = min(st[0], 0);
dr[0] = max(dr[0], 0);
}
else
{
st[0] = 0;
dr[0] = 0;
}
for(int i=0; i<nr; i++)
{
int u = n;
if(i + 1 < nr)
{
u = l[val][i + 1] - 1;
}
pair<int,int> sp = asp.query(l[val][i],u,1,1,n);
st[i + 1] = sp.first;
dr[i + 1] = sp.second;
}
auto cmpst = [](int a, int b)
{
return (a + st[a] <= b + st[b]);
};
auto cmpdr = [](int a, int b)
{
return (a + dr[a] <= b + dr[b]);
};
for(int i=0;i<=nr;i++)
{
pst[i] = i, pdr[i] = i;
}
sort(pst,pst+nr+1,cmpst);
sort(pdr,pdr+nr+1,cmpdr);
int poz = 0;
a.clear();
for(int i=0;i<=nr;i++)
{
a.push_back(st[i] - i);
a.push_back(dr[i] - i);
}
sort(a.begin(),a.end());
for(int p=0;p<=nr;p++)
{
int j = pdr[p];
while(poz <= nr && pst[poz] + st[pst[poz]] <= dr[j] + j)
{
int i = pst[poz];
aicnt.update(get_val(dr[i]-i),i,1,1,2*n);
++poz;
}
rez = max(rez, j - aicnt.query(get_val(st[j]-j),2*n,1,1,2*n));
}
for(int i=0;i<=nr;i++)
{
aicnt.update(get_val(dr[i]-i),nmax,1,1,2*n,1);
}
for(auto it : l[val])
{
asp.update(it,n,-1,1,1,n);
}
}
return rez;
}
#ifdef home
int main()
{
freopen("nr.in","r",stdin);
freopen("nr.out","w",stdout);
int nn;
cin>>nn;
vector<int> vv(nn);
for(int i=1; i<=nn; i++)
{
cin>>vv[i - 1];
}
cout<<sequence(nn,vv)<<'\n';
return 0;
}
#endif // home
# | 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... |