이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rc(x) return cout<<x<<endl,0
#define pb push_back
#define mkp make_pair
#define in insert
#define er erase
#define fd find
#define fr first
#define sc second
typedef long long ll;
typedef long double ld;
const ll INF=0x3f3f3f3f3f3f3f3f;
const ll llinf=(1LL<<62);
const int inf=(1<<30);
const int nmax=1e5+50;
const int mod=1e9+7;
using namespace std;
int n,i,tt,mn[nmax],x,y,p[nmax],par[nmax][20],q,tp,rt,viz[nmax],l[nmax],vl[nmax];
vector<int>a[nmax];
set<pair<int,int> >s;
void dfs(int x)
{
mn[x]=x;
vector<pair<int,int> >vc;
for(int i=0;i<(int)a[x].size();i++)
{
int y=a[x][i];
dfs(y);
mn[x]=min(mn[x],mn[y]);
vc.pb(mkp(mn[y],y));
}
sort(vc.begin(),vc.end());
for(int i=0;i<(int)a[x].size();i++)a[x][i]=vc[i].sc;
}
void rec(int x)
{
for(int i=0;i<(int)a[x].size();i++)rec(a[x][i]);
vl[x]=++tt;
s.in(mkp(vl[x],x));
}
void build(int x)
{
par[x][0]=p[x];
l[x]=l[p[x]]+1;
for(int i=1;i<18;i++)par[x][i]=par[par[x][i-1]][i-1];
for(int i=0;i<(int)a[x].size();i++)build(a[x][i]);
}
int main()
{
//freopen("sol.in","r",stdin);
//freopen("sol.out","w",stdout);
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
cin>>n>>q;
for(i=1;i<=n;i++)
{
cin>>p[i];
if(!p[i])
{
rt=i;
continue;
}
a[p[i]].pb(i);
}
dfs(rt);
rec(rt);
build(rt);
//for(i=1;i<=n;i++)cout<<vl[i]<<" ";
//cout<<endl;
while(q--)
{
cin>>tp>>x;
if(tp==1)
{
for(i=1;i<=x;i++)
{
y=s.begin()->sc;
viz[s.begin()->sc]=1;
s.er(s.begin());
}
cout<<y<<'\n';
}
else
{
y=x;
for(i=17;i>=0;i--)if(viz[par[y][i]])y=par[y][i];
cout<<l[x]-l[y]<<'\n';
s.in(mkp(vl[y],y));
viz[y]=0;
}
}
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... |