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<bits/stdc++.h>
#define MAXN 500007
using namespace std;
int n,a[MAXN],ans,c[MAXN];
int pref[MAXN],mins[MAXN],maxs[MAXN];
vector<int> poss[MAXN];
const int bucket_sz=300;
struct node{
int sum;
int minpref,maxpref;
inline friend node operator + (node fr,node sc){
return {fr.sum+sc.sum,min(fr.minpref,fr.sum+sc.minpref),max(fr.maxpref,fr.sum+sc.maxpref)};
}
}curr;
struct ST{
node tree[4*MAXN];
void update(int v,int l,int r,int pos,int val){
if(l==r){
tree[v].sum=val;
tree[v].minpref=min(val,0);
tree[v].maxpref=max(val,0);
}else{
int tt=(l+r)/2;
if(pos<=tt)update(2*v,l,tt,pos,val);
else update(2*v+1,tt+1,r,pos,val);
tree[v]=tree[2*v]+tree[2*v+1];
}
}
node getinfo(int v,int l,int r,int ll,int rr){
if(ll>rr)return {0,0,0};
if(l==ll and r==rr){
return tree[v];
}else{
int tt=(l+r)/2;
return getinfo(2*v,l,tt,ll,min(tt,rr)) + getinfo(2*v+1,tt+1,r,max(tt+1,ll),rr);
}
}
}seg;
struct ST2D{
struct node{
int l,r,val;
};
struct ST1D{
vector<node> tree;
void init(){
tree.clear();
tree.push_back({0,0,10*n});
tree.push_back({0,0,10*n});
}
void addnode(){
tree.push_back({0,0,10*n});
}
void check(int v){
if(tree[v].l==0){
addnode(); tree[v].l=tree.size()-1;
}
if(tree[v].r==0){
addnode(); tree[v].r=tree.size()-1;
}
}
void update(int v,int l,int r,int pos,int val){
if(l==r){
tree[v].val=val;
}else{
int tt=(l+r)/2;
check(v);
if(pos<=tt)update(tree[v].l,l,tt,pos,val);
else update(tree[v].r,tt+1,r,pos,val);
tree[v].val=min(tree[tree[v].l].val,tree[tree[v].r].val);
}
}
int getmin(int v,int l,int r,int ll,int rr){
if(v==0 or ll>rr)return 10*n;
if(l==ll and r==rr)return tree[v].val;
int tt=(l+r)/2;
return min( getmin(tree[v].l,l,tt,ll,min(tt,rr)) , getmin(tree[v].r,tt+1,r,max(tt+1,ll),rr) );
}
};
ST1D tree[8*MAXN];
void init(){
for(int i=1;i<=8*n;i++)tree[i].init();
}
void update(int v,int l,int r,int posx,int posy,int val){
if(l==r){
tree[v].update(1,0,2*n,posy,val);
}else{
int tt=(l+r)/2;
if(posx<=tt)update(2*v,l,tt,posx,posy,val);
else update(2*v+1,tt+1,r,posx,posy,val);
tree[v].update(1,0,2*n,posy,val);
}
}
int getmin(int v,int l,int r,int lx,int rx,int ly,int ry){
if(lx>rx)return 10*n;
if(l==lx and r==rx)return tree[v].getmin(1,0,2*n,ly,ry);
int tt=(l+r)/2;
return min( getmin(2*v,l,tt,lx,min(tt,rx),ly,ry) , getmin(2*v+1,tt+1,r,max(tt+1,lx),rx,ly,ry) );
}
}seg2;
int solve(int val){
int res=0;
for(int ii=0;ii<poss[val].size();ii++){
for(int ff=ii;ff<poss[val].size();ff++){
int i=poss[val][ii];
int f=poss[val][ff];
int sum=seg.getinfo(1,1,n,i+1,f-1).sum;
curr=seg.getinfo(1,1,n,f,n);
int minpref=curr.minpref,maxpref=curr.maxpref;
curr=seg.getinfo(1,1,n,1,i);
int minsuff=curr.sum-curr.maxpref,maxsuff=curr.sum-curr.minpref;
if((ff-ii+1)>=sum+minpref+minsuff and (ff-ii+1)>=-sum-maxpref-maxsuff)res=max(res,(ff-ii+1));
}
}
return res;
}
struct bro{
int x,y,b;
}s[MAXN];
int bigsolve(int val){
seg2.init();
int A=0,B=0,C=0,res=0;
//seg2.update(1,0,2*n,n,n,0);
s[0]={n,n,0};
for(int i=1;i<=n;i++){
if(a[i]>val)A++;
if(a[i]<val)C++;
if(a[i]==val)B++;
int z=n;
for(int f=0;f<=i-1;f++){
if(s[f].x<=B-(A-C)+n and s[f].y<=B+(A-C)+n)z=min(z,s[f].b);
}
res=max(res,B-z);
s[i]={B-(A-C)+n,B+(A-C)+n,B};
assert(B-(A-C)+n>=0 and B-(A-C)+n<=2*n);
assert(B+(A-C)+n>=0 and B+(A-C)+n<=2*n);
//seg2.update(1,0,2*n,B-(A-C)+n,B+(A-C)+n,B);
//res=max(res,B-seg2.getmin(1,0,2*n,0,B-(A-C)+n,0,B+(A-C)+n));
}
return res;
}
int sequence(int N, std::vector<int> A){
n=N;
for(int i=1;i<=n;i++){
a[i]=A[i-1];
poss[a[i]].push_back(i);
seg.update(1,1,n,i,1);
}
for(int i=1;i<=n;i++){
for(int f:poss[i-1])seg.update(1,1,n,f,-1);
for(int f:poss[i])seg.update(1,1,n,f,0);
if(poss[i].size()<=0)ans=max(ans,solve(i));
else ans=max(ans,bigsolve(i));
}
return ans;
}
/*int main(){
//cout<<sequence(9, {1, 1, 2, 3, 4, 3, 2, 1, 1})<<"\n";
//cout<<sequence(14, {2, 6, 2, 5, 3, 4, 2, 1, 4, 3, 5, 6, 3, 2})<<"\n";
return 0;
}*/
Compilation message (stderr)
sequence.cpp: In function 'int solve(int)':
sequence.cpp:132:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
132 | for(int ii=0;ii<poss[val].size();ii++){
| ~~^~~~~~~~~~~~~~~~~
sequence.cpp:133:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
133 | for(int ff=ii;ff<poss[val].size();ff++){
| ~~^~~~~~~~~~~~~~~~~
# | 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... |