Submission #872123

#TimeUsernameProblemLanguageResultExecution timeMemory
872123sleepntsheepBall Machine (BOI13_ballmachine)C++17
100 / 100
440 ms19040 KiB
#include <iostream> #include <cassert> #include <queue> #include <bitset> #include <cstring> #include <vector> #include <algorithm> #include <deque> #include <set> #include <utility> #include <functional> #include <array> using i64 = long long; using u64 = unsigned long long; using f64 = double; using f80 = long double; using namespace std; #define ALL(x) x.begin(), x.end() #define ShinLena cin.tie(nullptr)->sync_with_stdio(false); #define N 100000 int root, n, q, L[N], J[N], P[N], tout[N], rv[N], timer, t[N<<1], low[N]; signed char lz[N<<1]; vector<int> g[N]; inline void push(int v, int l, int r) { if (-1 == lz[v]) return; t[v] = lz[v] * (r-l+1); if (l != r) { int m = (l+r)/2, vl=v+1, vr=v+(m-l+1)*2; lz[vl] = lz[vr] = lz[v]; } lz[v] = -1; } int qry(int x, int y, int v = 0, int l = 0, int r = n - 1) { push(v, l, r); if (y < l || r < x) return 0; if (x <= l && r <= y) return t[v]; int m = (l+r)/2, vl =v + 1, vr=v+(m-l+1)*2; return qry(x, y, vl, l, m) + qry(x, y, vr, m+1, r); } void update(int x, int y, int k, int v = 0, int l = 0, int r = n - 1) { push(v, l, r); if (y < l || r < x) return; if (x <= l && r <= y) {lz[v] = k; push(v, l, r); return; } int m = (l+r)/2, vl =v + 1, vr=v+(m-l+1)*2; update(x, y, k, vl, l, m), update(x, y, k, vr, m+1, r); t[v] = t[vl] + t[vr]; } inline int highest(int u) { for (; u != P[u]; ) { if (qry(tout[J[u]], tout[J[u]])) u = J[u]; else if (qry(tout[P[u]], tout[P[u]])) u = P[u]; else break; } return u; } void dfs(int u) { for (auto v : g[u]) { P[v] = u; J[v] = (L[u] - L[J[u]] == L[J[u]] - L[J[J[u]]]) ? J[J[u]] : u; L[v] = L[u] + 1; dfs(v); } rv[tout[u] = timer++] = u; } void init() { ShinLena; cin >> n >> q; for (int i = 0; i < n; ++i) cin >> P[i], (P[i] ? void(g[P[i]-1].push_back(i)) : void(root = i)); function<void(int)> dfs0 = [&](int u) { low[u] = u; for (auto v : g[u]) dfs0(v), low[u] = min(low[u], low[v]); }; dfs0(root); for (int i = 0; i < n; ++i) sort(ALL(g[i]), [](int a, int b){ return low[a]<low[b]; }); dfs(P[root] = J[root] = root); assert(timer == n); memset(lz, -1, sizeof lz); } void answer() { for (int l, r, t, k; q--;) { cin >> t >> k; if (t == 1) { for (l = 0, r = n - 1; l <= r; ) { int m = (l+r)/2; if (m + 1 - qry(0, m) >= k) r = m - 1; else l = m + 1; } update(0, r+1, 1); cout << rv[r+1] + 1 << '\n'; } else { r = highest(k-1); update(tout[r], tout[r], 0); cout << L[k-1] - L[r] << '\n'; } } } int main() { init(); answer(); 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...