# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
897175 | Mathias | Ball Machine (BOI13_ballmachine) | C++14 | 1097 ms | 26968 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5+7;
const int LOG = 19;
int jp[MAXN][LOG];
int ojciec[MAXN];
int depth[MAXN];
bool ball[MAXN];
vector<int> g[MAXN];
set<pair<int,int> > s;
int post=0;
void DFS(int x){
depth[x]=depth[ojciec[x]]+1;
jp[x][0]=ojciec[x];
for(int i=1;i<LOG;i++) jp[x][i]=jp[jp[x][i-1]][i-1];
for(int i=0;i<g[x].size();i++){
DFS(g[x][i]);
}
s.insert({++post,x});
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n,q,x,r,a,b;
pair<int,int> p;
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>x;
ojciec[i]=x;
if(x==0) r=i;
else g[x].push_back(i);
}
DFS(r);
while(q--){
cin>>a>>b;
if(a==1){
while(b--){
auto it=s.begin();
s.erase(it);
p=*it;
ball[p.second]=1;
if(b==0){
cout<<p.second<<'\n';
}
}
}
else{
x=b;
for(int i=LOG-1;i>=0;i--){
if(ball[jp[x][i]]==1){
x=jp[x][i];
}
}
ball[x]=0;
//cout<<b<<' '<<x<<'\n';
cout<<depth[b]-depth[x]<<'\n';
}
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |