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>
using namespace std;
struct info{
int sum,mnl,mnr,mxl,mxr;
info(int v){
mnl=mnr=min(0,sum=v),mxl=mxr=max(0,v);
}
info(){
sum=mnl=mnr=mxl=mxr=0;
}
info(info a,info b){
sum=a.sum+b.sum;
mnl=min(a.mnl,b.mnl+a.sum);
mnr=min(b.mnr,a.mnr+b.sum);
mxl=max(a.mxl,b.mxl+a.sum);
mxr=max(b.mxr,a.mxr+b.sum);
}
}T[1<<20];
void update(int i,int l,int r,int p,int x){
if(l==r)
return void(T[i]=info(x));
if(p>l+r>>1)
update(i*2+1,l+r+2>>1,r,p,x);
else update(i*2,l,l+r>>1,p,x);
T[i]=info(T[i*2],T[i*2+1]);
}
info query(int i,int l,int r,int ll,int rr){
if(ll<=l&&r<=rr)
return T[i];
if(l>rr||ll>r)
return T[0];
return info(query(i*2,l,l+r>>1,ll,rr),
query(i*2+1,l+r+2>>1,r,ll,rr));
}
int n=0;
bool ok(int l,int r,int cnt){
int w=query(1,1,n,l,r).sum-cnt;
if(w<0)
return w+cnt+query(1,1,n,1,l-1).mxr+query(1,1,n,r+1,n).mxl>=0;
return w-cnt+query(1,1,n,0,l-1).mnr+query(1,1,n,r+1,n).mnl<=0;
}
int sequence(int N, std::vector<int> A) {
vector<int> p[N+1];
n=N;
for(int i=1;i<=N;i++)
update(1,1,n,i,1),
p[A[i-1]].push_back(i);
int ans=0;
for(int i=1;i<=N;i++){
int r=0;
for(int l=0;l<p[i].size();l++)
while(r<p[i].size()&&ok(p[i][l],p[i][r],r-l+1))
ans=max(ans,++r-l);
for(auto j:p[i])
update(1,1,n,j,-1);
}
return ans;
}
Compilation message (stderr)
sequence.cpp: In function 'void update(int, int, int, int, int)':
sequence.cpp:23:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
23 | if(p>l+r>>1)
| ~^~
sequence.cpp:24:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
24 | update(i*2+1,l+r+2>>1,r,p,x);
| ~~~^~
sequence.cpp:25:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
25 | else update(i*2,l,l+r>>1,p,x);
| ~^~
sequence.cpp: In function 'info query(int, int, int, int, int)':
sequence.cpp:33:30: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
33 | return info(query(i*2,l,l+r>>1,ll,rr),
| ~^~
sequence.cpp:34:28: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
34 | query(i*2+1,l+r+2>>1,r,ll,rr));
| ~~~^~
sequence.cpp: In function 'int sequence(int, std::vector<int>)':
sequence.cpp:52:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for(int l=0;l<p[i].size();l++)
| ~^~~~~~~~~~~~
sequence.cpp:53:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | while(r<p[i].size()&&ok(p[i][l],p[i][r],r-l+1))
| ~^~~~~~~~~~~~
# | 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... |