Submission #959478

#TimeUsernameProblemLanguageResultExecution timeMemory
959478vjudge1Ball Machine (BOI13_ballmachine)C++17
100 / 100
394 ms35388 KiB
#include <bits/stdc++.h> #define all(x) x.begin(), x.end() #define fi(i, a, b) for(int i = a; i <= b; i++) #define fid(i, a, b) for(int i = a; i >= b; i--) #define maxn (int) 2e5 + 7 using namespace std; int n, q, N; int cl[maxn], vt[maxn], cha[maxn][18]; int Min[maxn], a[maxn]; vector<int> edges[maxn]; set<int> S; void pre_dfs(int u, int par) { for(int v : edges[u]) if(v != par) { pre_dfs(v, u); Min[u] = min(Min[u], Min[v]); } } int In[maxn], Out[maxn], Time; void dfs(int u, int par) { for(int v : edges[u]) if(v != par) { cha[v][0] = u; fi(i, 1, 17) cha[v][i] = cha[cha[v][i - 1]][i - 1]; dfs(v, u); } a[++N] = u, vt[u] = N; } int Add(int k, int ans = 0) { while(k--) { ans = a[*S.begin()]; cl[a[*S.begin()]] = 1, S.erase(S.begin()); } return ans; } int Del(int u, int ans = 0) { fid(i, 17, 0) if(cl[cha[u][i]]) u = cha[u][i], ans += (1 << i); S.insert(vt[u]), cl[u] = 0; return ans; } int main() { cin >> n >> q; int rt = 0; fi(i, 1, n) { Min[i] = i, S.insert(i); int x; cin >> x; if(x == 0) rt = i; else edges[x].push_back(i); } pre_dfs(rt, 0); fi(i, 1, n) sort(all(edges[i]), [](int x, int y) { return Min[x] < Min[y]; } ); dfs(rt, 0); fi(i, 1, q) { int type, x; cin >> type >> x; if(type == 1) cout << Add(x) << '\n'; else cout << Del(x) << '\n'; } 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...