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 std::vector;
using std::array;
constexpr int LOG = 19;
int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int N;
    std::cin >> N;
    vector<int> ans(N);
    array<vector<int>, LOG> par, level;
    for (int j = 0; j < LOG; ++j) {
        par[j].resize(N);
        level[j].resize(N);
    }
    for (int i = 0; i < N; ++i) {
        int op;
        std::cin >> op;
        if (op > 0) {
            ans[i] = op;
            level[0][i] = 0;
            par[0][i] = i;
        } else {
            level[0][i] = -op;
            int to = i - 1;
            for (int j = LOG - 1; j >= 0; --j) {
                if (level[j][to] >= level[0][i]) {
                    to = par[j][to];
                }
            }
            to -= 1;
            par[0][i] = to;
            ans[i] = to == -1 ? 0 : ans[to];
        }
        for (int j = 0; j < LOG - 1; ++j) {
            if (par[j][i] == -1) {
                par[j + 1][i] = -1;
                level[j + 1][i] = 0;
            } else {
                par[j + 1][i] = par[j][par[j][i]];
                level[j + 1][i] = std::min(level[j][i], level[j][par[j][i]]);
            }
        }
        std::cout << ans[i] << '\n';
    }
    return 0;
}
| # | 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... |