我好菜…
我被卡在第一步。。。
看到这题有两种操作,第一种是发球,第二种是拿出球,考虑拿球很容易,只要找到最上面的的那个球修改为0即可,关键是如何处理第一个操作
考虑如果没有第二个操作,第一个操作也就是放球的顺序是固定的,可以展开成一个序列,加上第二个操作也不过就是在序列上留下几个空位
对于找到这个序列,就依据题意模拟,找出一个点子树的最小值,再将所有点的直系儿子加入vector,排序后,再一遍dfs处理即可。
开一个线段树维护上述序列0,1,个数(只用维护1,0可以用区间长度减出来)
对于操作1:每次只要找到第k个空位的位置,用找第k大一样,找到后将1到k的位置区间赋值为1即可,最后输出这个序列的这个位置的值
操作2:倍增找到最上面的有球的点,修改为0,输出当前点和最上面有球的点的dep之差即可
注意:因为我是区间赋值,pushdown不要+=,lazy初始值设-1
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#define re register long long
#define int long long
#define maxn 100050
using namespace std;
struct tu{
int u,v,nxt;
}e[maxn<<1];
int head[maxn],cnt;
void add(int u,int v){
e[++cnt].u=u;
e[cnt].v=v;
e[cnt].nxt=head[u];
head[u]=cnt;
}
int f[maxn][23];
int n,q;
int col[maxn];
int rt;
int dep[maxn],size[maxn];
void dfs(int u,int fa){
dep[u]=dep[fa]+1;
size[u]=1;
for(re i=head[u];i;i=e[i].nxt){
int v=e[i].v;
if(v==fa)continue;
dfs(v,u);
size[u]+=size[v];
}
}
int son[maxn],son2[maxn];
vector<int>sons[maxn];
int pos[maxn];
int w[maxn];
void dfsson(int u,int fa){
son2[u]=u;
for(re i=head[u];i;i=e[i].nxt){
int v=e[i].v;
if(v==fa)continue;
dfsson(v,u);
son2[u]=min(son2[u],son2[v]);
sons[u].push_back(v);
}
}
int vis[maxn];
int tot;
void dfs2(int u,int fa){
for(re i=0;i<sons[u].size();i++){
int v= sons[u][i];
dfs2(v,u);
}
pos[++tot] = u;
w[u]=tot;
}
//pos :落球的顺序将它展开是一个固定的序列
//w: 这个点在序列的第几个位置
struct tree{
int l,r,sum,lazy;
}t[maxn<<2];
void pushup(int now){
t[now].sum=t[now<<1].sum+t[now<<1|1].sum;
}
void pushdown(int x){
if(t[x].lazy!=-1){
t[x<<1].lazy=t[x].lazy;
t[x<<1|1].lazy=t[x].lazy;
t[x<<1].sum=(t[x<<1].r-t[x<<1].l+1)*t[x].lazy;
t[x<<1|1].sum=(t[x<<1|1].r-t[x<<1|1].l+1)*t[x].lazy;
t[x].lazy=-1;
}
}
void build(int now,int l,int r){
t[now].l=l;
t[now].r=r;
if(l==r){
return ;
}
int mid=(l+r)>>1;
build(now<<1,l,mid);
build(now<<1|1,mid+1,r);
}
void change(int now,int l,int r,int x){
if(l<=t[now].l&&t[now].r<=r){
t[now].sum=(t[now].r-t[now].l+1)*x;
t[now].lazy=x;
return;
}
pushdown(now);
int mid=(t[now].l+t[now].r)>>1;
if(l<=mid)change(now<<1,l,r,x);
if(r>mid)change(now<<1|1,l,r,x);
pushup(now);
}
int ask(int now,int p){
if(t[now].l==t[now].r){
return t[now].sum;
}
pushdown(now);
int mid=(t[now].l+t[now].r)>>1;
if(p<=mid)return ask(now<<1,p);
else return ask(now<<1|1,p);
/*
if(l<=t[now].l&&t[now].r<=r){
return t[now].sum;
}
pushdown(now);
int mid=(t[now].l+t[now].r)>>1;
int sons=0;
if(l<=mid)sons+=ask(now<<1,l,r);
if(r>mid)sons+=ask(now<<1|1,l,r);*/
}
bool comp(int pp,int qq){
return son2[pp]<son2[qq];
}
//找到第一个0
int find(int now,int x){
if(t[now].l ==t[now].r){
return t[now].l;
}
pushdown(now);
int left_len=(t[now<<1].r-t[now<<1].l+1);
if(left_len-t[now<<1].sum >= x){
//区间内0的个数
return find(now<<1,x);
}
else{
return find(now<<1|1,x-(left_len-t[now<<1].sum ));
}
}
int getfa(int u){
for(re i=22; i>=0; i--){
if(f[u][i] == 0) continue;
if(ask(1,w[f[u][i]])){
u=f[u][i];
}
}
return u;
}
signed main(){
freopen("ballmachine.in","r",stdin);
freopen("ballmachine.out","w",stdout);
scanf("%lld%lld",&n,&q);
for(re i=1;i<=n;i++){
int a1;
scanf("%lld",&a1);
f[i][0]=a1;
if(a1==0){
rt=i;
continue;
}
add(i,a1);
add(a1,i);
}
dfs(rt,0);
for(re j=1;j<=22;j++)
for(re i=1;i<=n;i++){
f[i][j]=f[f[i][j-1]][j-1];
}
dfsson(rt,0);
for(re i=1;i<=n;i++)
sort(sons[i].begin(),sons[i].end(),comp);
// for(re i=1;i<=n;i++)
/*while(tot<n){
if(!vis[i])dfs2(i);
}*/
dfs2(rt,0);
//for(re i=1;i<=tot;i++)
// printf("%lld ",pos[i]);
/*
for(re i=1;i<=q;i++){
int a1,a2;
scanf("%lld%lld",&a1,&a2);
//(a1,a2);
if(a1==1){
sub1(a2);
}
}*/
int now=0;
build(1,1,n);
for(re i=1;i<=4*n;i++)
t[i].lazy=-1;
for(re i=1;i<=q;i++){
int a1,a2;
scanf("%lld%lld",&a1,&a2);
if(a1==1){
now=find(1,a2);
// printf("%lld\n",now);
change(1,1,now,1);
//区间赋值
printf("%lld\n",pos[now]);
}
if(a1==2){
int ff=getfa(a2);
//倍增找第一个不是空位的父亲
int fff=ff;
ff=w[ff];
change(1,ff,ff,0);
//将那个位置修改为0
printf("%lld\n",dep[a2]-dep[fff]);
}
}
return 0;
}