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 inf INT_MAX
#define longlonginf LONG_LONG_MAX
#define mod 998244353
#define MAXN 100005
#define pii pair<ll,ll>
#define ll long long
#define deb(x) cerr<<"[ "<<#x<<" = "<<x<<" ]";
#define yes() cout<<"YES\n";
#define no() cout<<"NO\n";
using namespace std;
ll n,m,cur,q,x;
ll h;
ll ans = 0;
string subtask;
ll t;
ll par[MAXN];
vector<vector<ll>> child(MAXN);
ll root;
ll v[4*MAXN];
ll k;
bool vis[4*MAXN];
map<int,int> s;
map<int,int> mp;
void build(ll x){
if(child[x].size() == 0 ){
v[x] = x;
return;
}
for(int i = 0 ; i < (int) child[x].size() ; i++){
build(child[x][i]);
}
v[x] = x;
for(int i = 0 ; i < (int) child[x].size() ; i++){
v[x] = min(v[x],child[x][i]);
}
}
bool by_v(int a,int b){
return v[a] < v[b];
}
void add(ll x){
//cout<<"traversing ; "<<x<<"\n";
if( child[x].size() == 0 ) {
//cout<<"visiting : "<<x<<"\n";
vis[x] = 1;
ans = x;
s[cur] = x;
mp[x] = cur;
return;
}
int cnt = 0;
for(int i = 0 ; i < (int) child[x].size() ; i++){
if( vis[child[x][i]] == 0 ){
add(child[x][i]);
}
else cnt++;
}
if( cnt == (int) child[x].size() ){
//cout<<"visiting : "<<x<<"\n";
vis[x] = 1;
ans = x;
s[cur] = x;
mp[x] = cur;
return;
}
}
void remove(int x,ll d){
if( vis[par[x]] == 0 ){
vis[x] = 0;
s.erase(mp[x]);
//cout<<"erased : "<<x<<"\n";
cout<<d<<"\n";
return;
}
remove(par[x],d+1);
}
void solve(){
cin>>n>>q;
for(int i = 1 ; i <= n ; i++){
cin>>x;
if( x == 0 ) root = i;
par[i] = x;
child[x].push_back(i);
}
//init
build(root);
for(int i = 1 ; i <= n ; i++) sort(child[i].begin(),child[i].end(),by_v);
memset(vis,0,sizeof(vis));
//add n times
for(int i = 1 ; i <= n ; i++){
cur = i;
add(1);
}
//queries
while(q--){
cin>>x;
if( x == 1 ){
cin>>k;
k--;
while(k--){
s.erase(s.begin());
}
cout<<s.begin()->second<<"\n";
s.erase(s.begin());
}
else{
cin>>k;
remove(k,0);
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T = 1;
//cin>>T;
for(int i = 0 ; i < T ; i++){
//cout<<"Case #"<<i+1<<": ";
solve();
}
return 0;
}
/*
you thought u declared it huh?
not i but x
logical operator
wrong example/proof
thoroughly
wrong variables
thinking it wrong
bruh just try some test case
capitals ;-;
wrong data structure lol
count memory usement
corner case
oversized array
orders
statements
size initializer
while con
map -> array
wrong digits??
swapped variables??
check if theres any variabled
that got declared twice
find some pattern
name collision
constraints??!
mod !!
resets
*/
# | 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... |