이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
const int nmax=100005;
vector<int> v[nmax];
int mn[nmax],r[nmax],wh[nmax],aib[nmax],negru[nmax],lev[nmax];
int tt[17][nmax];
int nr,n,q,i,j,x,R,type,poz,orig;
bool comp(int unu,int doi)
{
return (mn[unu]<mn[doi]);
}
void dfs1(int x)
{
for(int i=0;i<v[x].size();i++)
{
lev[v[x][i]]=lev[x]+1;
dfs1(v[x][i]);
}
sort(v[x].begin(),v[x].end(),comp);
mn[x]=x;
if(v[x].size()) mn[x]=min(mn[x],mn[v[x][0]]);
}
void dfs2(int x)
{
for(int i=0;i<v[x].size();i++)
dfs2(v[x][i]);
r[++nr]=x;wh[x]=nr;
}
inline int lbit(int x)
{
return ((x^(x-1))&x);
}
void update(int poz,int val)
{
for(int idx=poz;idx<=n;idx+=lbit(idx))
aib[idx]+=val;
}
int get_poz()
{
int ret=0;
for(int p=16;p>=0;p--)
if(ret+(1<<p)<=n&&aib[ret+(1<<p)]==(1<<p))
ret+=(1<<p);
ret++;
return ret;
}
int main()
{
//freopen("data.in","r",stdin);
cin>>n>>q;
for(i=1;i<=n;i++)
{
cin>>x;
tt[0][i]=x;
if(x)v[x].push_back(i);
else R=i;
}
for(i=1;i<=16;i++)
for(j=1;j<=n;j++)
tt[i][j]=tt[i-1][tt[i-1][j]];
dfs1(R);
dfs2(R);
for(i=1;i<=q;i++)
{
cin>>type>>x;
if(type==1)
{
for(j=1;j<=x;j++)
{
poz=get_poz();
update(poz,1);
negru[r[poz]]=1;
}
cout<<r[poz]<<'\n';
}
else
{
orig=x;
for(int p=16;p>=0;p--)
if(negru[tt[p][x]])
x=tt[p][x];
update(wh[x],-1);
negru[x]=0;
cout<<lev[orig]-lev[x]<<'\n';
}
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
ballmachine.cpp: In function 'void dfs1(int)':
ballmachine.cpp:17:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v[x].size();i++)
~^~~~~~~~~~~~
ballmachine.cpp: In function 'void dfs2(int)':
ballmachine.cpp:28:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v[x].size();i++)
~^~~~~~~~~~~~
# | 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... |