Submission #869671

#TimeUsernameProblemLanguageResultExecution timeMemory
86967112345678Ball Machine (BOI13_ballmachine)C++17
100 / 100
279 ms32848 KiB
#include <bits/stdc++.h> using namespace std; const int nx=1e5+5, kx=17; int n, q, x, pa[nx][kx], mn[nx], t, id[nx], rid[nx], c, lvl[nx], rt; vector<int> d[nx]; struct segtree { int d[4*nx], lz[4*nx]; void pushlz(int l, int r, int i) { if (!lz[i]) return; lz[i]=0; d[i]=r-l+1; if (l==r) return; lz[2*i]=lz[2*i+1]=1; } void update(int l, int r, int i, int ql, int qr) { pushlz(l, r, i); if (qr<l||r<ql) return; if (ql<=l&&r<=qr) return lz[i]=1, pushlz(l, r, i), void(); int md=(l+r)/2; update(l, md, 2*i, ql, qr); update(md+1, r, 2*i+1, ql, qr); d[i]=d[2*i]+d[2*i+1]; } void update2(int l, int r, int i, int idx) { pushlz(l, r, i); if (r<idx||idx<l) return; if (l==r) return void(d[i]=0); int md=(l+r)/2; update2(l, md, 2*i, idx); update2(md+1, r, 2*i+1, idx); d[i]=d[2*i]+d[2*i+1]; } int query(int l, int r, int i, int key) { pushlz(l, r, i); if (l==r) return l; int md=(l+r)/2; if (md-l+1-d[2*i]>=key) return query(l, md, 2*i, key); else return query(md+1, r, 2*i+1, key-(md-l+1-d[2*i])); } bool query2(int l, int r, int i, int idx) { pushlz(l, r, i); if (idx<l||r<idx) return 0; if (l==r) return d[i]==1; int md=(l+r)/2; return query2(l, md, 2*i, idx)||query2(md+1, r, 2*i+1, idx); } } s; void dfs(int u, int p) { pa[u][0]=p; lvl[u]=lvl[p]+1; mn[u]=u; for (int i=1; i<kx; i++) pa[u][i]=pa[pa[u][i-1]][i-1]; for (auto v:d[u]) dfs(v, u), mn[u]=min(mn[u], mn[v]); } void dfs2(int u) { vector<pair<int, int>> tmp; for (auto v:d[u]) tmp.push_back({mn[v], v}); sort(tmp.begin(), tmp.end()); for (auto [x, y]:tmp) dfs2(y); id[u]=++t; rid[t]=u; } int main() { cin.tie(NULL)->sync_with_stdio(false); cin>>n>>q; for (int i=1; i<=n; i++) { cin>>x; if (x!=0) d[x].push_back(i); else rt=i; } dfs(rt, rt); dfs2(rt); while (q--) { cin>>t>>x; if (t==1) { x=s.query(1, n, 1, x); s.update(1, n, 1, 1, x); cout<<rid[x]<<'\n'; } else { int sv=x; for (int i=kx-1; i>=0; i--) if (s.query2(1, n, 1, id[pa[x][i]])==1) x=pa[x][i]; s.update2(1, n, 1, id[x]); cout<<lvl[sv]-lvl[x]<<'\n'; } } } /* 8 9 0 1 2 2 3 3 4 6 1 8 2 5 2 7 2 8 1 3 2 8 2 8 2 8 2 8 */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...