Submission #525522

#TimeUsernameProblemLanguageResultExecution timeMemory
525522crap_the_coderBall Machine (BOI13_ballmachine)C++17
20.95 / 100
1101 ms37500 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define endl '\n'

const int MOD = 1e9 + 7;
const int INF = LLONG_MAX >> 1;

const int LOG = 25;

signed main() {
    ios::sync_with_stdio(false); cin.tie(NULL);

    int n, q; cin >> n >> q;
    vector<int> tree[n+1];
    int par[n+1][LOG], cd[n+1]{};

    for (int i = 1; i <= n; i++) {
        int c; cin >> c; cd[c]++;
        tree[c].push_back(i);
        tree[i].push_back(c);
    }

    set<int> lf;
    for (int i = 1; i <= n; i++)
        if (cd[i] == 0)
            lf.insert(i);

    function<void(int, int)> dfs = [&](int u, int p) {
        par[u][0] = p;
        for (int i = 1; i < LOG; i++)
            par[u][i] = par[par[u][i-1]][i-1];

        for (auto v : tree[u])
            if (v != p)
                dfs(v, u);

    }; dfs(0, 0);

    bool marked[n+1]{};

    // Queries
    while (q--) {
        int t, k; cin >> t >> k;

        if (t == 1) {
            int ans = 0;
            while (k--) {
                marked[ans = *lf.begin()] = true;

                lf.erase(lf.begin());
                if (--cd[par[ans][0]] == 0)
                    lf.insert(par[ans][0]);
            }

            cout << ans << endl;

        } else {
            int r = 0;
            for (int j = LOG-1; j >= 0; j--)
                if (marked[par[k][j]])
                    k = par[k][j], r += (1 << j);

            cout << r << endl;

            marked[k] = false; cd[par[k][0]]++;
            lf.insert(k); lf.erase(par[k][0]);
        }
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...