Submission #964265

#TimeUsernameProblemLanguageResultExecution timeMemory
964265PringBall Machine (BOI13_ballmachine)C++17
16.11 / 100
1092 ms32756 KiB
#include <bits/stdc++.h>
using namespace std;

#ifdef MIKU
string dbmc = "\033[1;38;2;57;197;187m", dbrs = "\033[0m";
#define debug(x...) cout << dbmc << "[" << #x << "]: ", dout(x)
void dout() { cout << dbrs << endl; }
template <typename T, typename ...U>
void dout(T t, U ...u) { cout << t << (sizeof...(u) ? ", " : ""); dout(u...); }
#else
#define debug(...) 39
#endif

#define fs first
#define sc second
#define mp make_pair
#define FOR(i, j, k) for (int i = j, Z = k; i < Z; i++)
using ll = long long;
typedef pair<int, int> pii;

const int MXN = 100005, LG = 18;
int n, q, rt;
vector<int> edge[MXN];
vector<int> chd[MXN];
int d[MXN], dfn[MXN], scr[MXN], C;
int p[LG][MXN];
bitset<MXN> b;
set<int> S;

void DFS(int id, int par, int dep) {
    p[0][id] = par;
    d[id] = dep;
    for (auto &i : edge[id]) {
        if (i == par) continue;
        chd[id].push_back(i);
    }
    sort(chd[id].begin(), chd[id].end());
    for (auto &i : chd[id]) {
        DFS(i, id, dep + 1);
    }
    scr[C] = id;
    dfn[id] = C++;
}

int calc1(int x) {
    int ans = 0;
    while (x--) {
        int id = *S.begin();
        S.erase(S.begin());
        b[scr[id]] = true;
        ans = scr[id];
    }
    return ans;
}

int calc2(int x) {
    int x0 = x;
    for (int w = LG - 1; w >= 0; w--) {
        if (!b[p[w][x]]) continue;
        x = p[w][x];
    }
    b[x] = false;
    S.insert(dfn[x]);
    return d[x0] - d[x];
}

void miku() {
    int x, y;
    cin >> n >> q;
    FOR(i, 1, n + 1) {
        cin >> x;
        if (x == 0) rt = i;
        else {
            edge[i].push_back(x);
            edge[x].push_back(i);
        }
    }
    DFS(rt, 0, 0);
    FOR(w, 1, LG) {
        FOR(i, 1, n + 1) p[w][i] = p[w - 1][p[w - 1][i]];
    }
    FOR(i, 0, n) S.insert(i);
    while (q--) {
        cin >> x >> y;
        if (x == 1) cout << calc1(y) << '\n';
        else cout << calc2(y) << '\n';
    }
}

int32_t main() {
    cin.tie(0) -> sync_with_stdio(false);
    cin.exceptions(cin.failbit);
    miku();
    return 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...