제출 #1124104

#제출 시각아이디문제언어결과실행 시간메모리
1124104adaawfBall Machine (BOI13_ballmachine)C++20
21.83 / 100
162 ms24300 KiB
#include <iostream> #include <set> #include <vector> using namespace std; set<int> s[100005]; int l[100005][18], da[100005], f[100005], h; void dfs(int x, int fl) { if (fl == 1) { h = x; } f[x] = x; if (s[x].empty()) return; for (int w : s[x]) { dfs(w, (w == *s[x].begin())); if (w == *s[x].begin()) f[x] = f[w]; } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, q; cin >> n >> q; for (int i = 1; i <= n; i++) { int x; cin >> x; l[i][0] = x; s[x].insert(i); } dfs(0, 1); for (int i = 1; i <= 17; i++) { for (int j = 1; j <= n; j++) { l[j][i] = l[l[j][i - 1]][i - 1]; } } for (int jj = 0; jj < q; jj++) { int w, x; cin >> w >> x; if (w == 1) { int res = 0; for (int i = 1; i <= x; i++) { res = h; da[h] = 1; s[l[h][0]].erase(h); if (!s[l[h][0]].empty()) { int k = *s[l[h][0]].begin(); h = f[k]; } else h = l[h][0]; } cout << res << '\n'; } else { int res = 0; for (int i = 17; i >= 0; i--) { if (da[l[x][i]] == 1) { x = l[x][i]; res += (1 << i); } } s[l[x][0]].insert(x); h = x; da[h] = 0; cout << res << '\n'; } } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...