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>
using namespace std;
typedef long long ll;
typedef pair<int, int> pll;
#define fastio ios::sync_with_stdio(false), cin.tie(0)
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define pb push_back
#define eb emplace_back
#define f first
#define s second
const int maxn = 2e5 + 5;
int n;
vector<int> val;
int getid(int x){
return lower_bound(val.begin(), val.end(), x) - val.begin() + 1;
}
int tag[maxn * 4];
void push(int v){
if(tag[v]){
tag[v * 2] = tag[v];
tag[v * 2 + 1] = tag[v];
tag[v] = 0;
}
}
void apply(int l, int r, int val, int v = 1, int L = 0, int R = n - 1){
if(l > R || r < L) return;
if(l <= L && r >= R){
tag[v] = val;
return;
}
push(v);
int m = (L + R) / 2;
apply(l, r, val, v * 2, L, m);
apply(l, r, val, v * 2 + 1, m + 1, R);
}
int qry(int pos, int v = 1, int l = 0, int r = n - 1){
if(l == r) return tag[v];
push(v);
int m = (l + r) / 2;
if(pos <= m) return qry(pos, v * 2, l, m);
else return qry(pos, v * 2 + 1, m + 1, r);
}
int main(void){
fastio;
cin>>n;
vector<int> a(n);
for(int i = 0; i < n; i++) cin>>a[i], val.pb(a[i]);
sort(val.begin(), val.end());
val.erase(unique(val.begin(), val.end()), val.end());
for(int i = 0; i < n; i++) a[i] = getid(a[i]);
vector<stack<int>> lst(n + 1);
for(int i = 0; i < n; i++){
while(!lst[a[i]].empty() && qry(lst[a[i]].top()) != a[i]) lst[a[i]].pop();
if(!lst[a[i]].empty()) apply(lst[a[i]].top(), i, a[i]);
else apply(i, i, a[i]);
lst[a[i]].push(i);
}
for(int i = 0; i < n; i++) cout<<val[qry(i) - 1]<<"\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... |