Submission #1108346

#TimeUsernameProblemLanguageResultExecution timeMemory
1108346Kirill22Ball Machine (BOI13_ballmachine)C++17
100 / 100
337 ms28848 KiB
#include "bits/stdc++.h" using namespace std; const int N = (int) 1e5 + 22; vector<int> g[N]; int n, q, dp[N], rt, uk = 0, ord[N], up[N][20]; void dfs(int v) { dp[v] = v; for (int j = 1; j < 20; j++) { if (up[v][j - 1] == -1) { up[v][j] = -1; continue; } up[v][j] = up[up[v][j - 1]][j - 1]; } for (auto u : g[v]) { up[u][0] = v; dfs(u); dp[v] = min(dp[v], dp[u]); } std::sort(g[v].begin(), g[v].end(), [&] (int x, int y) { return dp[x] < dp[y]; }); } void dfs2(int v) { for (auto u : g[v]) { dfs2(u); } ord[v] = uk++; } void solve() { cin >> n >> q; for (int i = 0; i < n; i++) { int p; cin >> p; p--; if (p == -1) { rt = i; } else { g[p].push_back(i); } } up[rt][0] = -1; dfs(rt); dfs2(rt); set<pair<int, int>> life; for (int i = 0; i < n; i++) { life.insert({ord[i], i}); } while (q--) { int t, x; cin >> t >> x; if (t == 1) { int ans = 0; while (x--) { int v = life.begin()->second; ans = v; life.erase(life.begin()); } cout << ans + 1 << '\n'; } else { x--; int v = x, dist = 0; for (int j = 19; j >= 0; j--) { int u = up[v][j]; if (u == -1) { continue; } if (life.find({ord[u], u}) == life.end()) { dist += (1 << j); v = u; } } life.insert({ord[v], v}); cout << dist << '\n'; } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; // cin >> t; while (t--) { solve(); } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...