#include"sequence.h"
#include<vector>
#include<iostream>
using namespace std;
const int TREE_SIZE=1<<19;
int n;
vector<int>arr[500000];
int tree_lazy[2*TREE_SIZE];
int tree_min[2*TREE_SIZE];
int tree_max[2*TREE_SIZE];
void push_lazy(int node){
if(tree_lazy[node]!=0){
tree_min[node]+=tree_lazy[node];
tree_max[node]+=tree_lazy[node];
if(node<TREE_SIZE){
tree_lazy[2*node]+=tree_lazy[node];
tree_lazy[2*node+1]+=tree_lazy[node];
}
tree_lazy[node]=0;
}
}
int get_min(int node,int rl,int rr,int l,int r){
push_lazy(node);
if(r<=rl||rr<=l)
return 1e8;
if(l<=rl&&rr<=r)
return tree_min[node];
int mid=(rl+rr)/2;
return min(get_min(2*node,rl,mid,l,r),get_min(2*node+1,mid,rr,l,r));
}
int get_max(int node,int rl,int rr,int l,int r){
push_lazy(node);
if(r<=rl||rr<=l)
return -1e8;
if(l<=rl&&rr<=r)
return tree_max[node];
int mid=(rl+rr)/2;
return max(get_max(2*node,rl,mid,l,r),get_max(2*node+1,mid,rr,l,r));
}
int get_min(int l,int r){
return get_min(1,0,TREE_SIZE,l,r);
}
int get_max(int l,int r){
return get_max(1,0,TREE_SIZE,l,r);
}
void add(int node,int rl,int rr,int l,int r,int v){
push_lazy(node);
if(r<=rl||rr<=l)
return;
if(l<=rl&&rr<=r){
tree_lazy[node]+=v;
push_lazy(node);
return;
}
int mid=(rl+rr)/2;
add(2*node,rl,mid,l,r,v);
add(2*node+1,mid,rr,l,r,v);
tree_min[node]=min(tree_min[2*node],tree_min[2*node+1]);
tree_max[node]=max(tree_max[2*node],tree_max[2*node+1]);
}
void add(int l,int r,int v){
add(1,0,TREE_SIZE,l,r,v);
}
int sequence(int N,vector<int>A){
n=N;
for(int i=0;i<n;i++)
arr[A[i]-1].push_back(i);
for(int i=0;i<=n;i++)
add(i,i+1,-i);
int res=1;
for(int v=0;v<n;v++){
for(int idx:arr[v])
add(idx+1,n+1,1);
vector<int>mn1,mn2,mx1,mx2;
int idx=0;
int prev_mn1=-1e8;
int prev_mn2=-1e8;
for(int i:arr[v]){
mn1.push_back(max(prev_mn1,get_min(0,i+1)+idx));
mn2.push_back(max(prev_mn2,get_min(i,n+1)-idx));
prev_mn1=mn1.back();
prev_mn2=mn2.back();
mx1.push_back(get_max(0,i+1)-idx+1);
mx2.push_back(get_max(i,n+1)+idx+1);
idx++;
}
for(int r=0;r<(int)arr[v].size();r++){
int l1=0,l2=0;
while(l1<r&&mx1[l1]<mn2[r])
l1++;
while(l2<r&&mx2[r]<mn1[l2])
l2++;
int l=max(l1,l2);
res=max(res,r-l+1);
}
for(int idx:arr[v])
add(idx+1,n+1,1);
}
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
15960 KB |
Output is correct |
2 |
Correct |
6 ms |
15964 KB |
Output is correct |
3 |
Correct |
5 ms |
15964 KB |
Output is correct |
4 |
Correct |
5 ms |
15964 KB |
Output is correct |
5 |
Correct |
4 ms |
15960 KB |
Output is correct |
6 |
Incorrect |
4 ms |
15960 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
15960 KB |
Output is correct |
2 |
Correct |
6 ms |
15964 KB |
Output is correct |
3 |
Correct |
5 ms |
15964 KB |
Output is correct |
4 |
Correct |
5 ms |
15964 KB |
Output is correct |
5 |
Correct |
4 ms |
15960 KB |
Output is correct |
6 |
Incorrect |
4 ms |
15960 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
15960 KB |
Output is correct |
2 |
Correct |
1312 ms |
40508 KB |
Output is correct |
3 |
Correct |
1311 ms |
39564 KB |
Output is correct |
4 |
Execution timed out |
2023 ms |
32444 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15964 KB |
Output is correct |
2 |
Execution timed out |
2029 ms |
40444 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1386 ms |
46408 KB |
Output is correct |
2 |
Correct |
1378 ms |
45524 KB |
Output is correct |
3 |
Correct |
1405 ms |
45152 KB |
Output is correct |
4 |
Correct |
1346 ms |
44944 KB |
Output is correct |
5 |
Correct |
1444 ms |
41604 KB |
Output is correct |
6 |
Correct |
1359 ms |
41876 KB |
Output is correct |
7 |
Correct |
1278 ms |
40276 KB |
Output is correct |
8 |
Correct |
1270 ms |
39948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
15960 KB |
Output is correct |
2 |
Correct |
6 ms |
15964 KB |
Output is correct |
3 |
Correct |
5 ms |
15964 KB |
Output is correct |
4 |
Correct |
5 ms |
15964 KB |
Output is correct |
5 |
Correct |
4 ms |
15960 KB |
Output is correct |
6 |
Incorrect |
4 ms |
15960 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
15960 KB |
Output is correct |
2 |
Correct |
6 ms |
15964 KB |
Output is correct |
3 |
Correct |
5 ms |
15964 KB |
Output is correct |
4 |
Correct |
5 ms |
15964 KB |
Output is correct |
5 |
Correct |
4 ms |
15960 KB |
Output is correct |
6 |
Incorrect |
4 ms |
15960 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |