Submission #1291344

#TimeUsernameProblemLanguageResultExecution timeMemory
1291344qrnBall Machine (BOI13_ballmachine)C++20
20.95 / 100
354 ms38740 KiB
// one expects wise words here, but what can one do when they cannot find words to express their thoughts?
// just staring at the messy code below, arent you? well, im supposed to improve, not jibber jabber here.
// perhaps one does not have a choice when he does not enjoy anything, so he just yaps to himself in vsc like a loser.
// ome47

#include "bits/stdc++.h"
using namespace std;
#define intt long long

const intt mxN = 2e5 + 5;
const intt LG = 20;
const intt inf = 1e18;

vector<vector<intt>> graph, up;
vector<intt> par(mxN), in(mxN), out(mxN), isfull(mxN), depth(mxN);
set<intt> avil;
intt n, q, timer, root;

void dfs(intt node, intt parent) {
    if(graph[node].size() == 1 && node != root) {
        avil.insert(node);
    }

    in[node] = timer++;
    up[0][node] = parent;
    for(intt i = 1; i <= LG; i++) {
        up[i][node] = up[i-1][up[i-1][node]];
    }

    for(auto u : graph[node]) {
        if(u != parent) {
            depth[u] = depth[node] + 1;
            dfs(u, node);
        }
    }
    out[node] = timer++;
}

intt bl(intt node, intt k) {
    for(intt i = LG - 1; i >= 0; i--) {
        if((1 << i) & k) {
            node = up[i][node];
        }
    }
    return node;
}

void _() {
    cin >> n >> q;
    graph.assign(n + 1, vector<intt> {});
    up.assign(LG + 1, vector<intt>(n + 1, 0ll));
    in.assign(n + 1, 0ll); out.assign(n + 1, 0ll);

    for(intt i = 1; i <= n; i++) {
        intt node; cin >> node;
        if(node == 0) {
            root = i;
            par[root] = 0;
        } else {
            graph[node].push_back(i);
            graph[i].push_back(node);
            par[i] = node;
        }
    }

    dfs(root, par[root]);
    while(q--) {
        intt type, x, ans = 0;
        cin >> type >> x;
        if(type == 1) {
            for(intt i = 0; i < x; i++) {
                if(avil.empty()) break;
                intt leaf = *avil.begin();
                isfull[leaf] = 1;
                avil.erase(avil.begin());
                ans = leaf;
                if(leaf != root)
                avil.insert(par[leaf]);
            }
        } else {
            if(isfull[root]) {
                isfull[root] = 0;
                avil.insert(root);
                cout << depth[x] << endl;
                continue;
            }

            intt l = 1, r = depth[x], anc = x;
            while(l <= r) {
                intt mid = (l + r) / 2;
                intt cand = bl(x, mid);
                if(isfull[cand]) {
                    anc = cand;
                    l = mid + 1;
                } else {
                    r = mid - 1;
                }
            }
            if(!avil.empty() && avil.find(par[anc]) != avil.end()) {
                avil.erase(par[anc]);
            }
            avil.insert(anc);
            isfull[anc] = 0;
            ans = depth[x] - depth[anc];
        }
        cout << ans << endl;
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    
    intt t = 1;
    // cin >> t;
    while(t--){
        _();
    }
}
// ⠀⠀⠀⠀⠀⠀⠀⢀⣤⣦⣶⣤⠀⠀⠀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
// ⣤⢀⣀⡀⣠⣄⣠⣶⣿⣿⣿⣿⣿⣿⣿⣷⣾⡦⠀⠀⠀⠀⠀⠀⠀⠀⠀
// ⠻⡳⢛⠛⠒⠚⢛⣿⣿⠟⠋⠉⠛⠛⢿⣿⣿⣿⠇⠀⠀⠀⠀⠀⠀⠀⠀
// ⢛⡛⢿⣿⠟⠻⠛⣿⣇⣶⣖⣤⣀⣄⡰⣹⣿⡟⠁⠀⠀⠀⠀⠀⠀⠀⠀
// ⢿⠿⠿⠿⠿⠿⠿⢭⣐⠠⢼⣬⠽⢈⢁⡿⠛⠂⠀⠀⠀⠀⠀⠀⠀⠀⠀
// ⣛⣱⣤⣤⣤⣀⣄⣄⣗⢌⠋⠉⢙⣴⣧⢤⣤⣤⣤⣦⡀⠀⠀⠀⠀⢺⡿
// ⣿⣿⣿⠟⠛⠋⠀⡬⠼⢠⠍⠂⠀⠨⡇⠀⡏⠉⠛⢿⣧⠀⠀⠀⠀⢸⣀
// ⣟⠄⠀⠀⠀⠀⠀⢡⠀⠀⠀⠀⠀⡠⠁⢀⠁⠀⠀⠀⠘⣗⢀⠄⢶⣼⣾
// ⠀⠀⠀⢠⣦⠇⠀⠀⠁⠒⠒⠒⠈⠀⠀⠀⣞⢶⡀⠀⠀⠹⣧⣠⡞⣸⣏
// ⠀⠀⢀⡞⠋⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⣿⣿⣦⡀⠀⠈⠫⣕⣻⡇
// ⠀⠀⣾⣧⠈⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠠⣿⣿⣿⣷⣦⡠⡤⠀⣿⡇
// ⠠⢿⣿⣿⠂⠀⠀⠀⠀⠀⠀⠀⠀⣀⣀⠀⠴⣿⣿⣿⣿⠟⠋⢀⣤⣿⣷
// ⣀⣀⣀⣙⣃⣠⣤⣔⣤⣢⣵⣒⣒⣢⣄⣤⣄⣛⣋⣉⣀⣀⣤⣿⣿⣿⣿
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...