Description
One day, Lostmonkey invented a super-elastic device, in order to show off in front of his sheep friends, he invited the little sheep to play a game. At the beginning of the game, Lostmonkey on the ground along a straight line to put n devices, each device set the initial elastic coefficient ki, when the sheep reached the first device, it will bounce Ki step back to the first I+ki device, if there is no i+ki device, then the sheep were bounced. The sheep wanted to know that when it started from the device I, it was bounced a few times. In order to make the game more interesting, Lostmonkey can modify the elastic coefficient of an elastic device, and the elastic coefficient is a positive integer at any time.
Input
The first line contains an integer n, which indicates that there are n devices on the ground, the device is numbered from 0 to N-1, and the next line has n positive integers, followed by the initial elastic coefficients of the n units. The third line has a positive integer m, the next m row each line has at least two number I, J, if I=1, you want to output from J is bounced several times after being bounced, if i=2 will also enter a positive integer k, indicating that the coefficient of the J-Elastic device is modified to K. For data n,m<=10000 of 20%, for 100% of data n<=200000,m<=100000
Output
For each i=1, you will output a required number of steps, one row.
Sample Input4
1 2 1 1
3
1 1
2 1 1
1 1
Sample Output2
3
/*the number of nodes per subtrees tree. This problem is very ingenious is the establishment of a n+1 node, but it is not clear how this n+1 node in the program play a role in ... */#include<iostream>#include<cstdio>#defineN 200010using namespacestd;intn,m;intnext[n],c[n][2],fa[n],size[n],st[n];BOOLRev[n];BOOLIsRootintk) { returnc[fa[k]][0]!=k&&c[fa[k]][1]!=K;}voidPushup (intx) {Size[x]=size[c[x][0]]+size[c[x][1]]+1;}voidPushdown (intk) { intl=c[k][0],r=c[k][1]; if(Rev[k]) {Rev[k]^=1; rev[l]^=1; rev[r]^=1; Swap (c[k][0],c[k][1]); }}voidRotateintx) { inty=fa[x],z=Fa[y],l,r; if(c[y][0]==X) l=0;ElseL=1; r=l^1; if(!isroot (y)) { if(c[z][0]==y) c[z][0]=x;Elsec[z][1]=x; } Fa[x]=z;fa[y]=x;fa[c[x][r]]=y; C[Y][L]=c[x][r];c[x][r]=y; Pushup (y);p ushup (x);}voidSplay (intx) { inttop=0; st[++top]=x; for(inti=x;! IsRoot (i); i=Fa[i]) st[++top]=Fa[i]; for(inti=top;i;i--) pushdown (St[i]); while(!isroot (x)) { inty=fa[x],z=Fa[y]; if(!isroot (y)) { if(c[y][0]==x^c[z][0]==y) rotate (x); Elserotate (y); } rotate (x); }}voidAccessintx) { intt=0; while(x) {splay (x); c[x][1]=T; T=x;x=fa[x];}}voidRever (intx) {access (x); splay (x); Rev[x]^=1;}voidLinkintXinty) {rever (x); Fa[x]=y;splay (x);}voidCutintXinty) {rever (x); Access (y); splay (y); c[y][0]=fa[x]=0;}intMain () {scanf ("%d",&N); for(intI=1; i<=n;i++){ intX;SCANF ("%d",&x); Fa[i]=x+i;size[i]=1; if(fa[i]>n+1) fa[i]=n+1; Next[i]=Fa[i]; } size[n+1]=1; scanf ("%d",&m); for(intI=1; i<=m;i++){ intF;SCANF ("%d",&f); if(f==1) {rever (n+1); intX;SCANF ("%d", &x); x + +; Access (x); splay (x);p rintf ("%d\n", size[c[x][0]]); } Else { intX,Y;SCANF ("%d%d", &x,&y); x + +; intT=min (n+1, x+y); Cut (x,next[x]); link (x,t); Next[x]=T; } } return 0;}
Flying Sheep (Bzoj 2002)