Submission #1058853

#TimeUsernameProblemLanguageResultExecution timeMemory
1058853PekibanBall Machine (BOI13_ballmachine)C++17
100 / 100
163 ms28436 KiB
#include <bits/stdc++.h> using namespace std; #define pb push_back #define lwb lower_bound const int N = 1e5+5, LOG = 17; vector<int> g[N]; int up[LOG][N], D[N], mn[N], ps[N]; vector<int> a = {-1}; void dfs1(int s, int e = -1) { mn[s] = s; for (auto u : g[s]) { if (u == e) continue; D[u] = D[s] + 1; up[0][u] = s; dfs1(u, s); mn[s] = min(mn[s], mn[u]); } } bool cmp(int x, int y) { return mn[x] < mn[y]; } void dfs2(int s, int e = -1) { sort(g[s].begin(), g[s].end(), cmp); for (auto u : g[s]) { if (u == e) continue; dfs2(u, s); } a.pb(s); } int main() { ios::sync_with_stdio(0); cin.tie(0); int n, q, R; cin >> n >> q; for (int i = 1; i <= n; ++i) { int p; cin >> p; if (i == 0) { R = i; continue; } g[p].pb(i); g[i].pb(p); } dfs1(R); dfs2(R); for (int i = 1; i < LOG; ++i) { for (int j = 1; j <= n; ++j) { up[i][j] = up[i-1][up[i-1][j]]; } } for (int i = 1; i <= n; ++i) ps[a[i]] = i; ps[0] = 1e9; set<int> r, av; for (int i = 1; i <= n; ++i) av.insert(i); map<int, int> L; L[0] = 1; while (q--) { int t; cin >> t; if (t == 1) { int k, ans; cin >> k; while (k--) { int x = *av.begin(); av.erase(x); if (r.lwb(x) == r.end() || x+1 < L[*r.lwb(x)]) { L[x] = L[x-1]; if (x > 1) L.erase(x-1); if (x > 1) r.erase(x-1); r.insert(x); } else { L[*r.lwb(x)] = L[x-1]; if (x > 1) L.erase(x-1); if (x > 1) r.erase(x-1); } ans = x; } cout << a[ans] << '\n'; } else { int x; cin >> x; int X = x, ri = *r.lwb(ps[x]); for (int i = LOG-1; i >= 0; --i) { if (ps[up[i][x]] <= ri) x = up[i][x]; } L[ps[x] - 1] = L[ri]; r.insert(ps[x] - 1); if (ps[x] - 1 < L[ps[x] - 1]) { L.erase(ps[x] - 1); r.erase(ps[x] - 1); } L[ri] = ps[x] + 1; if (L[ri] > ri) { L.erase(ri); r.erase(ri); } av.insert(ps[x]); cout << D[X] - D[x] << '\n'; } } }

Compilation message (stderr)

ballmachine.cpp: In function 'int main()':
ballmachine.cpp:80:26: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
   80 |             cout << a[ans] << '\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...