Submission #919880

#TimeUsernameProblemLanguageResultExecution timeMemory
919880JakobZorzSequence (APIO23_sequence)C++17
63 / 100
2037 ms43880 KiB
#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_mx1=-1e8; for(int i:arr[v]){ mn1.push_back(min(prev_mn1,get_min(0,i+1)+idx)); prev_mn1=mn1.back(); mn2.push_back(get_min(i,n+1)-idx); mx1.push_back(max(prev_mx1,get_max(0,i+1)-idx+1)); prev_mx1=mx1.back(); 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; }
#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...