bzoj2002 动态树

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define maxn 210000
int ch[maxn][2],pre[maxn];
int siz[maxn];
bool rt[maxn];
void pushup(int x){
siz[x]=1+siz[ch[x][0]]+siz[ch[x][1]];
}
void rotate(int x){
int y=pre[x],k=ch[y][0]==x;
ch[y][!k]=ch[x][k];
pre[ch[x][k]]=y;
pre[x]=pre[y];
pre[y]=x;
ch[x][k]=y;
if(rt[y])rt[y]=0,rt[x]=1;
else ch[pre[x]][ch[pre[x]][1]==y]=x;
pushup(y);
}
void splay(int x){
while(!rt[x]){
int y=pre[x],z=pre[y];
if(rt[y])rotate(x);
else if((ch[z][1]==y)==(ch[y][1]==x))rotate(y),rotate(x);
else rotate(x),rotate(x);
}
pushup(x);
}
void acess(int x){
int y=0;
for(;x;x=pre[y=x]){
splay(x);
rt[ch[x][1]]=1,rt[ch[x][1]=y]=0;
pushup(x);
}
}
int a[maxn];
int n,m;
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
for(int i=0;i<=n+1;i++){
pre[i]=0;
ch[i][0]=ch[i][1]=0;
rt[i]=1;
siz[i]=1;
}
siz[0]=0;
for(int i=1;i<=n;i++){
int t=i+a[i];
if(t>n+1)t=n+1;
pre[i]=t;
}
int op;
int u,v;
scanf("%d",&m);
while(m--){
scanf("%d",&op);
if(op==1){
scanf("%d",&u);
u++;
acess(u);
splay(u);
printf("%d\n",siz[ch[u][0]]);
}else{
scanf("%d%d",&u,&v);
u++;
acess(u);
splay(u);
pre[ch[u][0]]=pre[u];
pre[u]=0;
rt[ch[u][0]]=1;
ch[u][0]=0;
pushup(u);
int t=u+v;
if(t>n)t=n+1;
pre[u]=t;
}
}
}

posted @ 2014-02-15 21:56  wangyucheng  阅读(116)  评论(0编辑  收藏