Submission #1309455

#TimeUsernameProblemLanguageResultExecution timeMemory
1309455aaaaaaaaBall Machine (BOI13_ballmachine)C++20
100 / 100
113 ms30768 KiB
#include <bits/stdc++.h> using namespace std; const int mxN = 1e5 + 10; const int mxB = 21; vector<int> adj[mxN]; int mn[mxN], ball[mxN], dp[mxN][mxB], depth[mxN]; vector<int> ord; void dfs(int u = 1){ mn[u] = u; depth[u] = depth[dp[u][0]] + 1; for(int j = 1; j < mxB; ++j){ dp[u][j] = dp[dp[u][j - 1]][j - 1]; } for(auto it : adj[u]){ dp[it][0] = u; dfs(it); mn[u] = min(mn[u], mn[it]); } } void dfs2(int u = 1){ vector<pair<int, int>> val; for(auto it : adj[u]){ val.push_back({mn[it], it}); } sort(val.begin(), val.end()); for(auto it : val){ dfs2(it.second); } ord.push_back(u); } int up_most(int u){ for(int i = mxB - 1; i >= 0; --i){ if(ball[dp[u][i]]){ u = dp[u][i]; } } return u; } signed main() { ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr); int n, q; cin >> n >> q; int root = 1; for(int i = 1, par; i <= n; ++i){ cin >> par; adj[par].push_back(i); if(par == 0) root = i; } dfs(root); dfs2(root); set<pair<int, int>> empty; vector<int> pos(n + 5, 0); for(int i = 0; i < (int) ord.size(); ++i){ empty.insert({i, ord[i]}); pos[ord[i]] = i; } auto remove = [&](int x) -> void { int top = up_most(x); empty.insert({pos[top], top}); ball[top] = 0; cout << depth[x] - depth[top] << "\n"; }; while(q--){ int t, k; cin >> t >> k; if(t == 1){ int last = 0; while(k--){ auto tp = *empty.begin(); empty.erase(tp); last = tp.second; ball[tp.second] = 1; } cout << last << "\n"; }else{ remove(k); } } 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...