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<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<string>
#include<chrono>
#include<functional>
#include<iterator>
#include<random>
#include<unordered_set>
#include<array>
#include<map>
#include<iomanip>
#include<assert.h>
#include<bitset>
using namespace std;
typedef long long int llint;
typedef long double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
/*
cout<<setprecision(20)
cin.tie(0);
ios::sync_with_stdio(false);
*/
const llint big=2.19e15+1;
const long double pai=3.141592653589793238462643383279502884197;
const long double eps=1e-15;
template <class T,class U>void mineq(T& a,U b){if(a>b){a=b;}}
template <class T,class U>void maxeq(T& a,U b){if(a<b){a=b;}}
llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);}
llint lcm(llint a,llint b){return a/gcd(a,b)*b;}
template<class T> void SO(T& ve){sort(ve.begin(),ve.end());}
template<class T> void REV(T& ve){reverse(ve.begin(),ve.end());}
template<class T>llint LBI(vector<T>&ar,T in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();}
template<class T>llint UBI(vector<T>&ar,T in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();}
//オイラーツアー
//addは自明
//eraseは留年二分探索
vector<int>oya;
vector<vector<int>>ko;
vector<int>tour;
vector<int>sai;
int ddfs(int ter){
sai[ter]=ter;
for(auto it:ko[ter]){mineq(sai[ter],ddfs(it));}
return sai[ter];
}
void dfs(int ter){
vector<pair<int,int>>aaa;
for(auto it:ko[ter]){aaa.pub(mp(sai[it],it));}
SO(aaa);
for(auto it:aaa){dfs(it.sec);}
tour.pub(ter);
}
int main(void){
cin.tie(0);
ios::sync_with_stdio(false);
int i,n,q;cin>>n>>q;
//まずはオイラーツアーを構築する
oya.res(n);
ko.res(n);
int rot=-1;
for(i=0;i<n;i++){
int a;cin>>a;a--;
if(a<0){rot=i;}
else{
oya[i]=a;
ko[a].pub(i);
}
}
sai.res(n);
ddfs(rot);
tour.reserve(n);
dfs(rot);
vector<int>yusen(n);
for(i=0;i<n;i++){yusen[tour[i]]=i;}
priority_queue<int,vector<int>,greater<int>> que;
vector<bool>aru(n+1);//aru[n]は常に空
oya[rot]=n;
for(i=0;i<n;i++){que.push(i);}
array<vector<int>,17>dab;
dab[0]=oya;
dab[0].pub(n);
for(int h=1;h<17;h++){
dab[h].res(n+1);
for(i=0;i<=n;i++){dab[h][i]=dab[h-1][dab[h-1][i]];}
}
while(q--){
int type;cin>>type;
if(type==1){
int k;cin>>k;
while(k--){
//入る場所を求める
int bas=tour[que.top()];
que.pop();
aru[bas]=1;
if(k==0){cout<<bas+1<<"\n";}
}
}else{
int x;cin>>x;
//ない場所 を二分探索で求める
int ans=0,bas=x-1;
for(int h=16;h>=0;h--){
if(aru[dab[h][bas]]){
ans+=(1<<h);
bas=dab[h][bas];
}
}
aru[bas]=0;
que.push(bas);
cout<<ans<<"\n";
}
}
}
# | 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... |