이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "sequence.h"
#include <bits/stdc++.h>
using namespace std;
#define pf printf
#define pb push_back
#define maxn 500005
struct node{
int s,e,m,mn,mx,lz;node *l,*r;
node(int _s,int _e){
s=_s,e=_e,m=(s+e)>>1,mn=0,mx=0,lz=0;
if(s!=e)l=new node(s,m),r=new node(m+1,e);
}
void ppo(){
mn+=lz,mx+=lz;
if(lz&&s!=e){
l->lz+=lz;
r->lz+=lz;
}
lz=0;
}
void up(int x,int y,int nv){
if(s==x&&e==y){lz+=nv;return;}
if(y<=m)l->up(x,y,nv);
else if(x>m)r->up(x,y,nv);
else l->up(x,m,nv),r->up(m+1,y,nv);
l->ppo(),r->ppo();
mn=min(l->mn,r->mn);
mx=max(l->mx,r->mx);
}
int qmx(int x,int y){
if(s==x&&e==y)return mx+lz;
ppo();
if(y<=m)return l->qmx(x,y);
if(x>m)return r->qmx(x,y);
return max(l->qmx(x,m),r->qmx(m+1,y));
}
int qmn(int x,int y){
if(s==x&&e==y)return mn+lz;
ppo();
if(y<=m)return l->qmn(x,y);
if(x>m)return r->qmn(x,y);
return min(l->qmn(x,m),r->qmn(m+1,y));
}
}*prt,*srt;
int pmn[maxn],pmx[maxn],smn[maxn],smx[maxn],tot[maxn][2];
vector<int> v[maxn];
//a range is valid if sum(>=x)-sum(<x)>=0 --> find min: == x value 1
//and sum(>x)-sum(<=x)<=0 --> find max: == x value -1
inline bool check(int i,int l,int r){
return (tot[i][0]-pmn[l]-smn[r]>=0)&&(tot[i][1]-pmx[l]-smx[r]<=0);
}
int sequence(int N,vector<int> A){
prt=new node(0,N+1);
srt=new node(0,N+1);
for(int i=0;i<N;++i){
v[A[i]].pb(i+1);
prt->up(i+1,N+1,1);
srt->up(0,i+1,1);
}
for(int i=1;i<=N;++i){
for(int x:v[i]){
pmn[x]=prt->qmn(0,x-1);
smn[x]=srt->qmn(x+1,N+1);
}
tot[i][0]=prt->qmx(N,N);
for(int x:v[i]){
prt->up(x,N+1,-2);
srt->up(0,x,-2);
}
tot[i][1]=prt->qmx(N,N);
for(int x:v[i]){
pmx[x]=prt->qmx(0,x-1);
smx[x]=srt->qmx(x+1,N+1);
}
}
int ans=0;
for(int i=1;i<=N;++i){
if(v[i].empty())continue;
int l=0,r=0;
while(r<v[i].size()){
assert(l<=r);
if(check(i,v[i][l],v[i][r])){
ans=max(ans,r-l+1);
++r;
}
else ++l;
}
}
return ans;
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'int sequence(int, std::vector<int>)':
sequence.cpp:87:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | while(r<v[i].size()){
| ~^~~~~~~~~~~~
# | 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... |