Submission #755424

#TimeUsernameProblemLanguageResultExecution timeMemory
755424The_SamuraiBall Machine (BOI13_ballmachine)C++17
7.54 / 100
1096 ms26956 KiB
#include <bits/stdc++.h> using namespace std; vector<vector<int>> g; vector<int> par, way, pos; vector<bool> ball; void dfs(int u) { for (int v: g[u]) { dfs(v); } way.emplace_back(u); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, q; cin >> n >> q; g.assign(n + 1, {}); par.assign(n + 1, 0); ball.assign(n + 1, false); pos.assign(n + 1, 0); for (int i = 1; i <= n; i++) { cin >> par[i]; g[par[i]].emplace_back(i); } way.clear(); dfs(0); // dfs way created set<pair<int, int>> emp; for (int i = 0; i < n; i++) { emp.insert({i, way[i]}); pos[way[i]] = i; } while (q--) { int t, x; cin >> t >> x; if (t == 1) { int i = 0, last = 0; vector<pair<int, int>> del; for (auto it = emp.begin(); i < x; i++, it++) { last = (*it).second; ball[(*it).second] = true; del.emplace_back(*it); } for (auto it: del) { emp.erase(it); } cout << last << '\n'; } else { assert(ball[x]); int cnt = 0; while (ball[par[x]]) { x = par[x]; cnt++; } ball[x] = false; emp.insert({pos[x], x}); cout << cnt << '\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...