Submission #964273

#TimeUsernameProblemLanguageResultExecution timeMemory
964273PringBall Machine (BOI13_ballmachine)C++17
16.11 / 100
150 ms20812 KiB
#include <bits/stdc++.h> using namespace std; #ifdef MIKU string dbmc = "\033[1;38;2;57;197;187m", dbrs = "\033[0m"; #define debug(x...) cout << dbmc << "[" << #x << "]: ", dout(x) void dout() { cout << dbrs << endl; } template <typename T, typename ...U> void dout(T t, U ...u) { cout << t << (sizeof...(u) ? ", " : ""); dout(u...); } #else #define debug(...) 39 #endif #define fs first #define sc second #define mp make_pair #define FOR(i, j, k) for (int i = j, Z = k; i < Z; i++) using ll = long long; typedef pair<int, int> pii; const int MXN = 100005, LG = 18; int n, q, rt; vector<int> chd[MXN]; int d[MXN], dfn[MXN], scr[MXN], C; int p[LG][MXN]; bitset<MXN> b; // set<int> S; priority_queue<int, vector<int>, greater<int>> pq; void DFS(int id, int par, int dep) { p[0][id] = par; d[id] = dep; for (auto &i : chd[id]) { DFS(i, id, dep + 1); } scr[C] = id; dfn[id] = C++; } int calc1(int x) { int ans = 0; while (x--) { assert(pq.size()); // int id = *S.begin(); // S.erase(S.begin()); int id = pq.top(); pq.pop(); b[scr[id]] = true; ans = scr[id]; } return ans; } int calc2(int x) { int x0 = x; for (int w = LG - 1; w >= 0; w--) { if (!b[p[w][x]]) continue; x = p[w][x]; } b[x] = false; // S.insert(dfn[x]); pq.push(dfn[x]); return d[x0] - d[x]; } void miku() { int x, y; cin >> n >> q; FOR(i, 1, n + 1) { cin >> x; if (x == 0) rt = i; else chd[x].push_back(i); } DFS(rt, 0, 0); // FOR(i, 1, n + 1) cout << dfn[i] << ' '; // cout << endl; FOR(w, 1, LG) { FOR(i, 1, n + 1) p[w][i] = p[w - 1][p[w - 1][i]]; } FOR(i, 0, n) pq.push(i); while (q--) { cin >> x >> y; if (x == 1) cout << calc1(y) << '\n'; else cout << calc2(y) << '\n'; } } int32_t main() { cin.tie(0) -> sync_with_stdio(false); cin.exceptions(cin.failbit); miku(); 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...