Submission #872042

#TimeUsernameProblemLanguageResultExecution timeMemory
872042vjudge1Ball Machine (BOI13_ballmachine)C++17
100 / 100
161 ms30156 KiB
#include <bits/stdc++.h> using namespace std; const int N = 100'000 + 7, L = 20 + 7; vector<int> ord, adj[N]; int n, q, r, mn[N], idx[N], sp[L][N]; bool mark[N]; set<int> s; void read_input() { cin >> n >> q; for (int i = 1; i <= n; i++) { int p; cin >> p; if (p == 0) { r = i; } else { adj[i].push_back(p); adj[p].push_back(i); } } } void dfs1(int v, int p) { mn[v] = v; for (int u : adj[v]) if (u != p) { dfs1(u, v); mn[v] = min(mn[v], mn[u]); } } void dfs2(int v, int p) { sort(adj[v].begin(), adj[v].end(), [](int v1, int v2) -> bool { return mn[v1] < mn[v2]; }); for (int u : adj[v]) if (u != p) dfs2(u, v); ord.push_back(v); } void dfs3(int v, int p) { sp[0][v] = p; for (int i = 1; i < L; i++) { if (sp[i - 1][sp[i - 1][v]] == 0) break; else sp[i][v] = sp[i - 1][sp[i - 1][v]]; } for (int u : adj[v]) if (u != p) dfs3(u, v); } void pre_process() { dfs1(r, 0); ord.push_back(-1); dfs2(r, 0); dfs3(r, 0); for (int i = 1; i <= n; i++) idx[ord[i]] = i; for (int i = 1; i <= n; i++) s.insert(i); } void add(int x) { for (int i = 1; i <= x; i++) { int t = *s.begin(); s.erase(t); mark[t] = true; if (i == x) cout << ord[t] << '\n'; } } void remove(int v) { int res = 0, u = v; for (int i = L - 1; i >= 0; i--) if (mark[idx[sp[i][u]]]) { u = sp[i][u]; res |= (1 << i); } mark[idx[u]] = false; s.insert(idx[u]); cout << res << '\n'; } void solve() { while (q--) { int t, x; cin >> t >> x; if (t == 1) add(x); else remove(x); } } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); read_input(); pre_process(); 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...