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 "bits/stdc++.h"
#define f first
#define s second
#define ll long long
#define pb push_back
#define pii pair<ll,ll>
using namespace std;
const int N=2e5+5;
unordered_map<int,vector<int>>dp;
ll t[4*N],lz[4*N];
void push(int i,int l,int r){
if(lz[i]){
t[i]=lz[i];
if(l<r){
lz[2*i]=lz[i];
lz[2*i+1]=lz[i];
}
}lz[i]=0;
}
void upd(int i,int l,int r,int tl,int tr,int v){
push(i,l,r);
if(r<tl||l>tr)return;
if(r<=tr&&l>=tl){
lz[i]=v;push(i,l,r);return;
}int m=(l+r)>>1;
upd(2*i,l,m,tl,tr,v);
upd(2*i+1,m+1,r,tl,tr,v);
t[i]=max(t[2*i],t[2*i+1]);
}
int qr(int i,int l,int r,int idx){
push(i,l,r);
if(r<idx||l>idx)return 0;
if(l==r)return t[i];
int m=(l+r)>>1;
return max(qr(2*i,l,m,idx),qr(2*i+1,m+1,r,idx));
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int n;cin>>n;
int a[n+1];
for(int i=1;i<=n;i++)cin>>a[i];
dp[a[1]].pb(1);upd(1,1,n,1,1,a[1]);
for(int i=2;i<=n;i++){
if(dp.find(a[i])!=dp.end()){
while(!dp[a[i]].empty()&&qr(1,1,n,dp[a[i]].back())!=a[i])dp[a[i]].pop_back();
if(!dp[a[i]].empty()){
upd(1,1,n,dp[a[i]].back(),i-1,a[i]);
}
}upd(1,1,n,i,i,a[i]);
dp[a[i]].pb(i);
}
for(int i=1;i<=n;i++)cout<<qr(1,1,n,i)<<"\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |