题意:有n个蹦蹦床,每个蹦蹦床有一个ki值,表示当跳到第i个蹦蹦床,会直接弹到第i+ki个蹦蹦床。当不存在第i+ki的时候(即i+ki>n),则被弹飞。
两个操作:
1 x:询问初始位置在x,到最后被弹飞,整个过程中,跳到过几个蹦蹦床。
2 x y :把第x个蹦蹦床的kx值改为y
思路:
一开始,给每个蹦蹦床连边,对于下标为i的蹦蹦床,如果i+ki<=n,则把i和i+ki连起来,否则,把i和n+1连起来(n+1是一个虚拟的点,用来表示已经被弹飞)。
对于1操作:把x点作为splay树的根,然后access一下n+1,最后把n+1splay上去,此时这棵splay树的节点数就是答案了(当然要减去1,n+1点是虚拟点)
对于2操作:先删边,这里需要判断i+ki是否大于n,如果i+ki<=n,就删除i和i+ki的边,否则,删除i和n+1的边。然后就是把ki改为y,再连边,连边操作和一开始的连边操作一样。
参考代码:
#include<stdio.h>
#include<algorithm>
#define N 200005
using namespace std;
inline void read(int &ret)//正整数
{
char c;
ret=0;
while((c=getchar())<'0'||c>'9');
while(c>='0'&&c<='9')
{
ret=ret*10+(c-'0');
c=getchar();
}
}
int rev[N],sz[N],ch[N][2],pre[N],st[N],t,n,val[N];
bool is_root(int x){return !pre[x]||(ch[pre[x]][0]!=x&&ch[pre[x]][1]!=x);}
bool side(int x){return ch[pre[x]][1]==x;}
void push_rev(int x)
{
if(!rev[x])return;
rev[x]=0;swap(ch[x][0],ch[x][1]);
if(ch[x][0])rev[ch[x][0]]^=1;
if(ch[x][1])rev[ch[x][1]]^=1;
}
void update(int x)
{
sz[x]=1;
if(ch[x][0])sz[x]+=sz[ch[x][0]];
if(ch[x][1])sz[x]+=sz[ch[x][1]];
}
void rotate(int x)
{
int y=pre[x],z=pre[y];
bool f=ch[y][1]==x;
if(!is_root(y))ch[z][ch[z][1]==y]=x;
ch[y][f]=ch[x][!f];ch[x][!f]=y;
pre[x]=z;pre[y]=x;pre[ch[y][f]]=y;
update(y);update(x);
}
void splay(int x)
{
st[t=1]=x;
for(int y=x;!is_root(y);st[++t]=y=pre[y]);
for(;t;t--)if(rev[st[t]])push_rev(st[t]);
while(!is_root(x))
{
if(!is_root(pre[x]))
{
if(side(x)==side(pre[x]))rotate(pre[x]);
else rotate(x);
}
rotate(x);
}
update(x);
}
void access(int x)
{
for(int t=0;x;ch[x][1]=t,t=x,x=pre[x])splay(x);
}
void make_root(int x)
{
access(x);
splay(x);
rev[x]^=1;
}
void link(int x,int y)
{
make_root(x);
pre[x]=y;
}
int findroot(int x)
{
access(x);
splay(x);
push_rev(x);
while(ch[x][0])push_rev(x),x=ch[x][0];
splay(x);
return x;
}
void cut(int x,int y)
{
make_root(x);
if(findroot(y)==x&&pre[y]==x&&!ch[y][0])
{
pre[y]=ch[x][1]=0;
update(x);
}
}
void path(int x,int y)
{
make_root(x);
access(y);
splay(y);
}
int main()
{
int n;
read(n);
for(int i=1;i<=n;i++)
{
read(val[i]);
if(i+val[i]<=n)link(i,i+val[i]);
else link(i,n+1);
}
int m,op,x,y;
read(m);
for(int i=1;i<=m;i++)
{
read(op);
if(op==1)
{
read(x);
x++;
make_root(x);
access(n+1);
splay(n+1);
printf("%d\n",sz[n+1]-1);
}
else
{
read(x),read(y);
x++;
if(x+val[x]<=n)cut(x,x+val[x]);
else cut(x,n+1);
if(x+y<=n)link(x,x+y);
else link(x,n+1);
val[x]=y;
}
}
return 0;
}