#include <bits/stdc++.h>
const int MX = 2e5 + 5;
int state[MX], par[MX][19], lev[MX];
// get last op on active path of x with lev <= maxLev
int get_par(int x, int max_lev) {
if (lev[x] <= max_lev) {
return x;
}
for (int i = 0; i < 19; i++) {
if (lev[par[x][i]] > max_lev) {
x = par[x][i];
}
}
return par[x][0];
}
int main() {
int n;
std::cin >> n;
for (int i = 1; i <= n; i++) {
std::cin >> state[i];
if (state[i] < 0) {
lev[i] = -state[i];
int z = get_par(i - 1, lev[i] - 1);
assert(z); // must be something to undo
par[i][0] = get_par(z - 1, lev[i] - 1);
// levels of ops in active path are strictly decreasing
assert(lev[i] > lev[par[i][0]]);
for (int j = 1; j < 19; j++) {
par[i][j] = par[par[i][j - 1]][j - 1]; // prep binary jumps
}
}
std::cout << state[get_par(i, 0)] << '\n';
// current active path is i, par[i], par[par[i]], ...
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |