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<iostream>
#include<stdio.h>
#include<math.h>
#include<map>
#include<string>
#include<algorithm>
#include<vector>
#include<string.h>
#include<utility>
#include<set>
#include<cmath>
#include<queue>
#include<deque>
#include<functional>
#include<stack>
#include<limits.h>
#include<iomanip>
#include<unordered_map>
#include<numeric>
#include<tuple>
using namespace std;
#define MOD 1000000009
#define int long long
#define ff first
#define ss second
#define endl '\n'
typedef pair<int,int> pp;
int order=0;
const int mxN=1e5+1;
vector<bool> e(mxN);
vector<vector<int> > p(mxN,vector<int>(20));
vector<int> lvl(mxN),o(mxN),v[mxN],dp(mxN,1e9);
void pre(int a, int d){
lvl[a]=d;
for(auto node: v[a]){
pre(node,d+1);
dp[a]=min(dp[a],dp[node]);
}
dp[a]=min(dp[a],a);
}
void dfs(int a){
vector<pp> t;
for(auto node: v[a]){
t.push_back({dp[node],node});
}
sort(t.begin(),t.end());
for(auto node: t) dfs(node.ss);
o[a]=++order;
}
int find(int a){
for(int i=19; i>=0; i--){
if(e[p[a][i]]) a=p[a][i];
}
return a;
}
signed main(){
// freopen("248.in", "r", stdin);
// freopen("248.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,m; cin>>n>>m;
for(int i=1; i<=n; i++){
cin>>p[i][0];
v[p[i][0]].push_back(i);
}
pre(0,0);
dfs(0);
for(int i=1; i<20; i++){
for(int j=1; j<=n; j++){
p[j][i]=p[p[j][i-1]][i-1];
}
}
priority_queue<pp,vector<pp>,greater<pp> > q;
for(int i=1; i<=n; i++) q.push({o[i],i});
while(m--){
int t,x; cin>>t>>x;
if(t==1){
int r;
while(x--){
r=q.top().ss;
e[r]=true;
q.pop();
}
cout<<r<<endl;
}else{
int c=find(x);
e[c]=false;
q.push({o[c],c});
cout<<lvl[x]-lvl[c]<<endl;
}
}
return 0;
}
# | 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... |