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;
const int INF=1e6;
struct node{
int mn, mx;
node(int mn=0, int mx=0) : mn(mn), mx(mx) {}
};
int n;
vector<vector<int>> pos;
vector<int> lazy;
vector<node> tr;
void change(int pos, int add) {
tr[pos].mn+=add;
tr[pos].mx+=add;
lazy[pos]+=add;
}
node pull(node L, node R) {
int mn, mx;
mn=min(L.mn, R.mn);
mx=max(L.mx, R.mx);
return node(mn, mx);
}
void push(int pos) {
change(2*pos+1, lazy[pos]);
change(2*pos+2, lazy[pos]);
lazy[pos]=0;
}
void update(int ll, int rr=n-1, int add=-1, int pos=0, int l=0, int r=n-1) {
if(ll>rr) return;
if(ll==l && rr==r) {change(pos, add); return;}
push(pos); int mid=l+r>>1;
update(ll, min(rr, mid), add, 2*pos+1, l, mid);
update(max(mid+1, ll), rr, add, 2*pos+2, mid+1, r);
tr[pos]=pull(tr[2*pos+1], tr[2*pos+2]);
}
node get(int ll, int rr, int pos=0, int l=0, int r=n-1) {
if(ll>rr) return node(INF, -INF);
if(ll==l && rr==r) return tr[pos];
push(pos); int mid=l+r>>1;
return pull(get(ll, min(rr, mid), 2*pos+1, l, mid), get(max(mid+1, ll), rr, 2*pos+2, mid+1, r));
}
node build(int pos=0, int l=0, int r=n-1) {
lazy[pos]=0;
if(l==r) return tr[pos]=node(l+1, r+1);
int mid=l+r>>1;
return tr[pos]=pull(build(2*pos+1, l, mid), build(2*pos+2, mid+1, r));
}
int sequence(int N, vector<int> a) {
n=N;
pos.resize(n+1);
tr.resize(4*n);
lazy.resize(4*n);
for(int i=0; i<n; i++) pos[a[i]].push_back(i);
int ans=1;
build();
for(int val=1; val<=n; val++) {
int idx=0;
for(int p : pos[val]) update(p);
for(int i=0; i<pos[val].size(); i++) {
idx=max(idx, i);
while(idx<pos[val].size()) {
int mn=get(pos[val][idx], n-1).mn;
if(pos[val][i]) mn-=max(0, get(0, pos[val][i]-1).mx);
int mx=get(pos[val][idx], n-1).mx;
if(pos[val][i]) mx-=min(0, get(0, pos[val][i]-1).mn);
int len=idx-i+1;
if(len<mn || -len>mx) break;
else idx++;
}
ans=max(ans, idx-i);
}
for(int p : pos[val]) update(p);
}
return ans;
}
Compilation message (stderr)
sequence.cpp: In function 'void update(int, int, int, int, int, int)':
sequence.cpp:32:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
32 | push(pos); int mid=l+r>>1;
| ~^~
sequence.cpp: In function 'node get(int, int, int, int, int)':
sequence.cpp:40:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
40 | push(pos); int mid=l+r>>1;
| ~^~
sequence.cpp: In function 'node build(int, int, int)':
sequence.cpp:46:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
46 | int mid=l+r>>1;
| ~^~
sequence.cpp: In function 'int sequence(int, std::vector<int>)':
sequence.cpp:60:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for(int i=0; i<pos[val].size(); i++) {
| ~^~~~~~~~~~~~~~~~
sequence.cpp:62:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | while(idx<pos[val].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... |