제출 #55471

#제출 시각아이디문제언어결과실행 시간메모리
55471DiuvenBall Machine (BOI13_ballmachine)C++11
7.54 / 100
267 ms24752 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; const int MX=100010, inf=2e9; int n, q, root; vector<int> G[MX]; int nu[MX], re[MX]; int st[MX][20]; void dfs(int v){ static int now=0; for(int i=1; i<20; i++) st[v][i]=st[st[v][i-1]][i-1]; for(int x:G[v]) dfs(x); nu[v]=++now; re[nu[v]]=v; } set<int> S; bool B[MX]; int put(){ int x=*S.begin(); S.erase(x); int v=re[x]; B[v]=true; // cout<<"PLACED: "<<v<<'\n'; return v; } int pop(int v){ S.insert(nu[v]); B[v]=false; return 0; int dist=0; for(int i=19; i>=0; i--) if(B[st[v][i]]) v=st[v][i], dist+=(1<<i); S.insert(nu[v]); B[v]=false; return dist; } int main(){ ios::sync_with_stdio(0); cin.tie(0); cin>>n>>q; for(int i=1; i<=n; i++){ int p; cin>>p; if(p==0) root=i; else G[p].push_back(i); st[i][0]=p; } dfs(root); for(int i=1; i<=n; i++) S.insert(i); for(int i=1; i<=q; i++){ int t, x; cin>>t>>x; if(t==1){ int ans=0; for(; x--; ) ans=put(); cout<<ans<<'\n'; } else{ cout<<pop(x)<<'\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...