#include <bits/stdc++.h>
const int MAX_N = 3e5 + 1;
const int MAX_D = 19; // ceil(log2(3*(10^5)))
int state[MAX_N], par[MAX_N][MAX_D], lev[MAX_N];
// 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 = MAX_D - 1; i >= 0; 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 < MAX_D; 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... |