이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "sequence.h"
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
struct SegTreeLazy_min
{
int aint[1100000],lazy[1100000];
void build(int nod, int st, int dr, int paritate)
{
lazy[nod]=0;
if(st==dr)
{
if(st%2==paritate) aint[nod]=0;
else aint[nod]=INF;
return;
}
aint[nod] = min(aint[nod*2], aint[nod*2+1]);
}
void propagate(int nod)
{
lazy[nod*2]+=lazy[nod];
lazy[nod*2+1]+=lazy[nod];
aint[nod*2]+=lazy[nod];
aint[nod*2+1]+=lazy[nod];
lazy[nod]=0;
}
void upd(int nod, int st, int dr, int le, int ri, int newv)
{
if(le>ri)
return;
if(le==st && dr==ri)
{
lazy[nod]+=newv;
aint[nod]+=newv;
return;
}
propagate(nod);
int mij=(st+dr)/2;
upd(nod*2,st,mij,le,min(mij,ri),newv);
upd(nod*2+1,mij+1,dr,max(mij+1,le),ri,newv);
aint[nod] = min(aint[nod*2], aint[nod*2+1]);
}
int qry(int nod, int st, int dr, int le, int ri)
{
if(le>ri)
return INF;
if(le==st && dr==ri)
return aint[nod];
propagate(nod);
int mij=(st+dr)/2;
return min(qry(nod*2,st,mij,le,min(mij,ri)),
qry(nod*2+1,mij+1,dr,max(mij+1,le),ri));
}
};
struct SegTreeLazy_max
{
int aint[1100000],lazy[1100000];
void build(int nod, int st, int dr, int paritate)
{
lazy[nod]=0;
if(st==dr)
{
if(st%2==paritate) aint[nod]=0;
else aint[nod]=-INF;
return;
}
aint[nod] = max(aint[nod*2], aint[nod*2+1]);
}
void propagate(int nod)
{
lazy[nod*2]+=lazy[nod];
lazy[nod*2+1]+=lazy[nod];
aint[nod*2]+=lazy[nod];
aint[nod*2+1]+=lazy[nod];
lazy[nod]=0;
}
void upd(int nod, int st, int dr, int le, int ri, int newv)
{
if(le>ri)
return;
if(le==st && dr==ri)
{
lazy[nod]+=newv;
aint[nod]+=newv;
return;
}
propagate(nod);
int mij=(st+dr)/2;
upd(nod*2,st,mij,le,min(mij,ri),newv);
upd(nod*2+1,mij+1,dr,max(mij+1,le),ri,newv);
aint[nod] = max(aint[nod*2], aint[nod*2+1]);
}
int qry(int nod, int st, int dr, int le, int ri)
{
if(le>ri)
return -INF;
if(le==st && dr==ri)
return aint[nod];
propagate(nod);
int mij=(st+dr)/2;
return max(qry(nod*2,st,mij,le,min(mij,ri)),
qry(nod*2+1,mij+1,dr,max(mij+1,le),ri));
}
};
SegTreeLazy_min aint_min[2];
SegTreeLazy_max aint_max[2];
bool verif(int le1, int ri1, int le2, int ri2, int cnt0, int N)
{
pair<int,int> x[2],y[2];
for(int i=0;i<2;i++)
{
x[i] = {aint_min[i].qry(1,0,N,le1,ri1), aint_max[i].qry(1,0,N,le1,ri1)};
y[i] = {aint_min[i].qry(1,0,N,le2,ri2), aint_max[i].qry(1,0,N,le2,ri2)};
x[i].first/=2;
x[i].second/=2;
y[i].first/=2;
y[i].second/=2;
}
for(int p=0;p<2;p++)
{
for(int q=0;q<2;q++)
{
pair<int,int> a = x[p], b = y[q];
if(a.first > b.first)
swap(a,b);
if(p==q)
{
if(a.second + cnt0 - 1 >= b.first)
return 1;
}
else
{
if(a.second + cnt0 >= b.first)
return 1;
}
}
}
return 0;
}
vector<int> pozs[500005];
int sequence(int N, std::vector<int> A)
{
aint_min[0].build(1,0,N,0);
aint_min[1].build(1,0,N,1);
aint_max[0].build(1,0,N,0);
aint_max[1].build(1,0,N,1);
for(int i=1;i<=N;i++)
{
aint_min[0].upd(1,0,N,i,N,+2);
aint_min[1].upd(1,0,N,i,N,+2);
aint_max[0].upd(1,0,N,i,N,+2);
aint_max[1].upd(1,0,N,i,N,+2);
pozs[A[i-1]].push_back(i);
}
int mxm=1;
for(int val=1;val<=N;val++)
{
for(auto x:pozs[val])
{
aint_min[0].upd(1,0,N,x,N,-2);
aint_min[1].upd(1,0,N,x,N,-2);
aint_max[0].upd(1,0,N,x,N,-2);
aint_max[1].upd(1,0,N,x,N,-2);
}
for(int i=0;i<pozs[val].size();i++)
{
for(int j=i;j<pozs[val].size();j++)
{
for(int k=0;k<pozs[val].size();k++)
{
int x = pozs[val][k];
if(k<i || k>j)
{
aint_min[0].upd(1,0,N,x,N,-1);
aint_min[1].upd(1,0,N,x,N,-1);
aint_max[0].upd(1,0,N,x,N,+1);
aint_max[1].upd(1,0,N,x,N,+1);
}
}
if(verif(0,pozs[val][i]-1,pozs[val][j],N,j-i+1,N))
{
mxm = max(mxm, j-i+1);
}
for(int k=0;k<pozs[val].size();k++)
{
int x = pozs[val][k];
if(k<i || k>j)
{
aint_min[0].upd(1,0,N,x,N,+1);
aint_min[1].upd(1,0,N,x,N,+1);
aint_max[0].upd(1,0,N,x,N,-1);
aint_max[1].upd(1,0,N,x,N,-1);
}
}
}
}
for(auto x:pozs[val])
{
aint_min[0].upd(1,0,N,x,N,-2);
aint_min[1].upd(1,0,N,x,N,-2);
aint_max[0].upd(1,0,N,x,N,-2);
aint_max[1].upd(1,0,N,x,N,-2);
}
}
return mxm;
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'int sequence(int, std::vector<int>)':
sequence.cpp:167:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
167 | for(int i=0;i<pozs[val].size();i++)
| ~^~~~~~~~~~~~~~~~~
sequence.cpp:169:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
169 | for(int j=i;j<pozs[val].size();j++)
| ~^~~~~~~~~~~~~~~~~
sequence.cpp:171:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
171 | for(int k=0;k<pozs[val].size();k++)
| ~^~~~~~~~~~~~~~~~~
sequence.cpp:188:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
188 | for(int k=0;k<pozs[val].size();k++)
| ~^~~~~~~~~~~~~~~~~
# | 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... |