2002: [Hnoi2010]Bounce 弹飞绵羊
Description
某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏。游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置设定初始弹力系数ki,当绵羊达到第i个装置时,它会往后弹ki步,达到第i+ki个装置,若不存在第i+ki个装置,则绵羊被弹飞。绵羊想知道当它从第i个装置起步时,被弹几次后会被弹飞。为了使得游戏更有趣,Lostmonkey可以修改某个弹力装置的弹力系数,任何时候弹力系数均为正整数。
Input
第一行包含一个整数n,表示地上有n个装置,装置的编号从0到n-1,接下来一行有n个正整数,依次为那n个装置的初始弹力系数。第三行有一个正整数m,接下来m行每行至少有两个数i、j,若i=1,你要输出从j出发被弹几次后被弹飞,若i=2则还会再输入一个正整数k,表示第j个弹力装置的系数被修改成k。对于20%的数据n,m<=10000,对于100%的数据n<=200000,m<=100000
Output
对于每个i=1的情况,你都要输出一个需要的步数,占一行。
Sample Input
4
1 2 1 1
3
1 1
2 1 1
1 1
1 2 1 1
3
1 1
2 1 1
1 1
Sample Output
2
3
3
也是LCT。。。因为每个点连的都是一条链,所以直接查询子树数目就好了。。。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<cstdio> 6 #include<algorithm> 7 #include<string> 8 #include<map> 9 #include<queue> 10 #include<vector> 11 #include<set> 12 #define inf 1000000000 13 #define maxn 200000+5 14 #define maxm 10000+5 15 #define eps 1e-10 16 #define ll long long 17 #define for0(i,n) for(int i=0;i<=(n);i++) 18 #define for1(i,n) for(int i=1;i<=(n);i++) 19 #define for2(i,x,y) for(int i=(x);i<=(y);i++) 20 #define for3(i,x,y) for(int i=(x);i>=(y);i--) 21 #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go) 22 using namespace std; 23 int read(){ 24 int x=0,f=1;char ch=getchar(); 25 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 26 while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();} 27 return x*f; 28 } 29 int fa[maxn],c[maxn][2],sta[maxn],m,n,s[maxn],next[maxn]; 30 bool rev[maxn]; 31 bool isroot(int x){ 32 return c[fa[x]][0]!=x&&c[fa[x]][1]!=x; 33 } 34 void pushup(int x){ 35 s[x]=s[c[x][0]]+s[c[x][1]]+1; 36 } 37 void rotate(int x){ 38 int y=fa[x],z=fa[y],l=(c[y][1]==x),r=l^1; 39 if(!isroot(y))c[z][c[z][1]==y]=x; 40 fa[x]=z;fa[y]=x;fa[c[x][r]]=y; 41 c[y][l]=c[x][r];c[x][r]=y; 42 pushup(y);pushup(x); 43 } 44 void pushdown(int x){ 45 if(!rev[x])return ; 46 rev[x]^=1;rev[c[x][0]]^=1;rev[c[x][1]]^=1; 47 swap(c[x][0],c[x][1]); 48 } 49 void splay(int x){ 50 int top=0;sta[++top]=x; 51 for(int y=x;!isroot(y);y=fa[y])sta[++top]=fa[y]; 52 for(;top;)pushdown(sta[top--]); 53 while(!isroot(x)){ 54 int y=fa[x],z=fa[y]; 55 if(!isroot(y)){ 56 if(c[z][0]==y^c[y][0]==x)rotate(x); 57 else rotate(y); 58 } 59 rotate(x); 60 } 61 } 62 void access(int x){ 63 for(int y=0;x;x=fa[x]){ 64 splay(x);c[x][1]=y;y=x; 65 } 66 } 67 void makeroot(int x){ 68 access(x);splay(x);rev[x]^=1; 69 } 70 int find(int x){ 71 access(x);splay(x); 72 while(c[x][0])x=c[x][0]; 73 return x; 74 } 75 void link(int x,int y){ 76 makeroot(x);fa[x]=y;splay(x); 77 } 78 void cut(int x,int y){ 79 makeroot(x);access(y);splay(y);c[y][0]=fa[x]=0; 80 } 81 int main(){ 82 //freopen("input.txt","r",stdin); 83 //freopen("output.txt","w",stdout); 84 n=read(); 85 for1(i,n){ 86 int x=read(); 87 s[i]=1; 88 next[i]=min(n+1,i+x); 89 fa[i]=next[i]; 90 } 91 s[n+1]=1; 92 m=read(); 93 for1(i,m){ 94 int x=read(); 95 if(x==1){ 96 int y=read()+1; 97 makeroot(n+1);access(y);splay(y);printf("%d\n",s[c[y][0]]); 98 } 99 else{ 100 x=read()+1;int y=read(),tmp=min(x+y,n+1); 101 cut(x,next[x]);link(x,tmp);next[x]=tmp; 102 } 103 } 104 return 0; 105 }