Submission #447657

#TimeUsernameProblemLanguageResultExecution timeMemory
447657prvocisloBall Machine (BOI13_ballmachine)C++17
100 / 100
228 ms26668 KiB
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5, logn = 17; int l[logn][maxn], mini[maxn], inv[maxn], n, q; vector<pair<int, int> > g[maxn]; vector<int> ord; void dfs1(int u, int p = -1) { for (int i = 1; i < logn; i++) if (l[i-1][u] != -1) l[i][u] = l[i-1][l[i-1][u]]; for (pair<int, int> &v : g[u]) if (v.first != p) { l[0][v.first] = u; dfs1(v.first, u); v.second = mini[v.first]; mini[u] = min(mini[u], mini[v.first]); } } void dfs2(int u, int p = -1) { for (pair<int, int> &v : g[u]) if (v.first != p) dfs2(v.first, u); inv[u] = ord.size(), ord.push_back(u); } bool cmp(pair<int, int> a, pair<int, int> b) { return a.second < b.second; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> q; iota(mini, mini+n, 0); int root = -1; for (int i = 0, p; i < n; i++) { cin >> p; p--; if (p == -1) root = i; else g[p].push_back({i, -1}); } for (int i = 0; i < logn; i++) for (int j = 0; j < n; j++) l[i][j] = -1; dfs1(root); for (int i = 0; i < n; i++) sort(g[i].begin(), g[i].end(), cmp); dfs2(root); set<int> s; for (int i = 0; i < n; i++) s.insert(i); vector<bool> b(n, false); while (q--) { int typ; cin >> typ; if (typ == 1) { int k; cin >> k; int ans = -1; for (int i = 0; i < k; i++) { int vr = ord[*s.begin()]; ans = vr; b[vr] = true; s.erase(s.begin()); } cout << ans+1 << "\n"; } else { int x; cin >> x; x--; int y = x, ans = 0; for (int i = logn-1; i >= 0; i--) if (l[i][y] != -1 && b[l[i][y]]) y = l[i][y], ans += (1 << i); b[y] = false; s.insert(inv[y]); cout << ans << "\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...