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 int long long
using namespace std;
struct SegTree {
int h = 0;
vector<int> flag;
vector<int> resarr;
SegTree(int n) {
while ((1 << h) < n)
h += 1;
flag.resize((1 << (h + 1)), -1);
resarr.resize(n);
}
void upd(int v, int vl, int vr, int l, int r, int val) {
if (vl >= r || vr <= l)
return;
if (l <= vl && vr <= r) {
flag[v] = val;
return;
}
int vm = (vl + vr) / 2;
upd(2 * v, vl, vm, l, r, val);
upd(2 * v + 1, vm, vr, l, r, val);
}
void upd(int l, int r, int val) {
upd(1, 0, (1 << h), l, r, val);
}
void calc(int v, int vl, int vr, int upval = -1) {
if (flag[v] != -1 && upval == -1)
upval = flag[v];
if (vl + 1 == vr) {
if (upval != -1)
resarr[v - (1 << h)] = upval;
return;
}
int vm = (vl + vr) / 2;
calc(2 * v, vl, vm, upval);
calc(2 * v + 1, vm, vr, upval);
}
void calc() {
calc(1, 0, (1 << h));
}
};
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n;
cin >> n;
vector<int> a(n);
vector<int> curst;
multiset<int> has;
SegTree st(n);
for (int i = 0; i < n; ++i) {
int ai;
cin >> ai;
a[i] = ai;
if (!has.count(ai)) {
has.insert(ai);
curst.push_back(i);
st.upd(i, i + 1, ai);
continue;
}
while (a[curst.back()] != ai) {
has.extract(a[curst.back()]);
curst.pop_back();
}
st.upd(curst.back(), i + 1, ai);
}
st.calc();
for (auto el : st.resarr)
cout << el << '\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... |