Submission #1057255

#TimeUsernameProblemLanguageResultExecution timeMemory
1057255alexddSequence (APIO23_sequence)C++17
20 / 100
728 ms52816 KiB
#include "sequence.h" #include <bits/stdc++.h> using namespace std; const int INF = 1e9; pair<int,int> combine(pair<int,int> x, pair<int,int> y) { return {min(x.first,y.first),max(x.second,y.second)}; } pair<int,int> aint[1100000][2]; int lazy[1100000]; void build(int nod, int st, int dr) { if(st==dr) { if(st%2==0) { aint[nod][0]={0,0}; aint[nod][1]={INF,-INF}; } else { aint[nod][1]={0,0}; aint[nod][0]={INF,-INF}; } return; } } void propagate(int nod) { if(!lazy[nod]) return; lazy[nod*2]+=lazy[nod]; lazy[nod*2+1]+=lazy[nod]; aint[nod*2][0].first+=lazy[nod]; aint[nod*2][0].second+=lazy[nod]; aint[nod*2+1][0].first+=lazy[nod]; aint[nod*2+1][0].second+=lazy[nod]; aint[nod*2][1].first+=lazy[nod]; aint[nod*2][1].second+=lazy[nod]; aint[nod*2+1][1].first+=lazy[nod]; aint[nod*2+1][1].second+=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][0].first+=newv; aint[nod][0].second+=newv; aint[nod][1].first+=newv; aint[nod][1].second+=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][0] = combine(aint[nod*2][0], aint[nod*2+1][0]); aint[nod][1] = combine(aint[nod*2][1], aint[nod*2+1][1]); } pair<int,int> qry(int nod, int st, int dr, int le, int ri, int c) { if(le>ri) return {INF,-INF}; if(le==st && dr==ri) return aint[nod][c]; propagate(nod); int mij=(st+dr)/2; return combine(qry(nod*2,st,mij,le,min(mij,ri),c), qry(nod*2+1,mij+1,dr,max(mij+1,le),ri,c)); } bool verif(int le, int ri, int cnt0, int N) { pair<int,int> x[2] = {qry(1,0,N,0,le-1,0), qry(1,0,N,0,le-1,1)}; pair<int,int> y[2] = {qry(1,0,N,ri,N,0), qry(1,0,N,ri,N,1)}; 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) { build(1,0,N); for(int i=1;i<=N;i++) { upd(1,0,N,i,N,+1); pozs[A[i-1]].push_back(i); } int mxm=1; for(int val=1;val<=N;val++) { for(auto x:pozs[val]) upd(1,0,N,x,N,-1); int st=0; for(int i=0;i<pozs[val].size();i++) { while(!verif(pozs[val][st],pozs[val][i],i-st+1,N)) st++; mxm = max(mxm, i-st+1); } for(auto x:pozs[val]) upd(1,0,N,x,N,-1); } return mxm; }

Compilation message (stderr)

sequence.cpp: In function 'int sequence(int, std::vector<int>)':
sequence.cpp:117:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  117 |         for(int i=0;i<pozs[val].size();i++)
      |                     ~^~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...