제출 #1122944

#제출 시각아이디문제언어결과실행 시간메모리
1122944stefanneaguBall Machine (BOI13_ballmachine)C++20
0.56 / 100
157 ms34196 KiB
#include <bits/stdc++.h> using namespace std; const int nmax = 1e5 + 10; set<int> s; vector<int> in[nmax], adj[nmax]; bool f[nmax]; int mps[nmax], to[nmax], back[nmax]; int up[nmax][17], d[nmax]; void dfs1(int i) { mps[i] = i; for(auto it : in[i]) { dfs1(it); mps[i] = min(mps[i], mps[it]); } } int timp = 1; bool cmp(int a, int b) { return mps[a] < mps[b]; } void dfs2(int i) { sort(in[i].begin(), in[i].end(), cmp); for(auto it : in[i]) { dfs2(it); } to[i] = timp; back[timp] = i; timp++; } void dfs(int i) { for(int j = 1; (1 << j) <= d[i]; j++) { up[i][j] = up[up[i][j - 1]][j - 1]; } for(auto it : adj[i]) { d[it] = d[i] + 1; dfs(it); } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n, q, root = -1; cin >> n >> q; for(int i = 1; i <= n; i++) { s.insert(i); f[i] = 1; cin >> up[i][0]; if(up[i][0] == 0) { root = i; continue; } in[up[i][0]].push_back(i); } dfs1(root); dfs2(root); for(int i = 1; i <= n; i++) { for(auto it : in[i]) { adj[to[i]].push_back(to[it]); } } dfs(n); int ult = 0, ans = 0; while(q--) { int t, a; cin >> t >> a; if(t == 1) { ult = 0; while(a--) { if(s.empty()) { break; } ult = *s.begin(); f[ult] = 0; s.erase(ult); } cout << back[ult] << '\n'; } else { a = to[a]; ans = 0; for(int i = 16; i >= 0; i--) { if((1 << i) <= d[a] && f[up[a][i]] == 0) { a = up[a][i]; ans += (1 << i); } } cout << ans << '\n'; s.insert(a); f[a] = 1; } } 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...