# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
198014 | dndhk | Ball Machine (BOI13_ballmachine) | C++14 | 222 ms | 24904 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>
#define pb push_back
using namespace std;
const int MAX_N = 100000;
const int MAX_K = 20;
int N, Q, R, p[MAX_N+1];
vector<int> gp[MAX_N+1];
int up[MAX_N+1][MAX_K+1];
vector<int> v;
int num[MAX_N+1], lv[MAX_N+1];
priority_queue<int, vector<int>, greater<int> > pq;
int chk[MAX_N+1];
void dfs(int x){
up[x][0] = p[x];
for(int i=1; i<MAX_K; i++){
up[x][i] = up[up[x][i-1]][i-1];
}
for(int i : gp[x]){
lv[i] = lv[x]+1;
dfs(i);
}
pq.push(v.size());
num[x] = v.size();
v.pb(x);
}
int main(){
scanf("%d%d", &N, &Q);
for(int i=1; i<=N; i++){
scanf("%d", &p[i]);
if(p[i]==0){
R = i;
}
}
for(int i=1; i<=N; i++){
if(p[i]==0) continue;
if(!chk[i]){
int n = i;
while(n!=R){
if(chk[n]) break;
gp[p[n]].pb(n);
chk[n] = true;
n = p[n];
}
}
}
for(int i=1; i<=N; i++) chk[i] = false;
lv[R] = 1; dfs(R);
for(int i=1; i<=Q; i++){
int x, y; scanf("%d%d", &x, &y);
if(x==1){
while(y--){
int n = pq.top(); pq.pop();
n = v[n];
chk[n] = true;
//cout<<n<<" ";
if(y==0){
printf("%d\n", n);
}
}
}else{
x = y;
for(int i=MAX_K-1; i>=0; i--){
if(up[y][i]==0) continue;
if(chk[up[y][i]]){
y = up[y][i];
}
}
chk[y] = false;
pq.push(num[y]);
printf("%d\n", lv[x]-lv[y]);
}
}
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... |