2002: [Hnoi2010]bounce Fly Sheep
Time Limit:10 Sec Memory limit:259 MB
submit:5708 solved:3005
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
HINTSource
Splay heuristic Merging
Problem Solving: LCT
1#include <bits/stdc++.h>2 using namespacestd;3 Const intMAXN =300010;4 structlct{5 intfa[maxn],ch[maxn][2],SZ[MAXN],PARENT[MAXN];6InlinevoidPushup (intx) {7SZ[X] =1+ sz[ch[x][0]] + sz[ch[x][1]];8 }9 voidRotateintXintKD) {Ten inty =Fa[x]; Onech[y][kd^1] =CH[X][KD]; AFA[CH[X][KD]] =y; -FA[X] =Fa[y]; -CH[X][KD] =y; theFa[y] =x; - if(fa[x]) Ch[fa[x]][y = = ch[fa[x]][1]] =x; - pushup (y); - } + voidSplay (intXintGoal =0){ - inty =x; + while(Fa[y]) y =Fa[y]; A if(X! =y) { atPARENT[X] =Parent[y]; -Parent[y] =0; - while(Fa[x]! =goal) { - if(Fa[fa[x]] = = goal) Rotate (x,x = = ch[fa[x]][0]); - Else{ - inty = Fa[x],z = Fa[y],s = (ch[z][0] ==y); in if(x = =Ch[y][s]) { -Rotate (x,s^1); to rotate (x,s); +}Else{ - rotate (y,s); the rotate (x,s); * } $ }Panax Notoginseng } - pushup (x); the } + } A voidAccessintx) { the for(inty =0; X x =Parent[x]) { + splay (x); -fa[ch[x][1]] =0; $parent[ch[x][1]] =x; $ch[x][1] =y; -Fa[y] =x; -Parent[y] =0; they =x; - pushup (x);Wuyi } the } - voidCutintx) { Wu access (x); - splay (x); Aboutparent[ch[x][0]] =Parent[x]; $PARENT[X] =0; -fa[ch[x][0]] =0; -ch[x][0] =0; - } A voidJoinintXinty) { + cut (x); thePARENT[X] =y; - } $ voidinit () { thememset (FA,0,sizeofFA); thememset (SZ,0,sizeofsz); thememset (CH,0,sizeofch); thememset (Parent,0,sizeofparent); - } in }LCT; the intMain () { the intN,m,x,y,z,op; About while(~SCANF ("%d",&N)) { the lct.init (); the for(inti =1; I <= N; ++i) { thescanf"%d",&x); + if(i + x > N) lct.parent[i] =0; - ElseLct.parent[i] = i +x; theLct.sz[i] =1;Bayi } thescanf"%d",&m); the while(m--){ -scanf"%d%d",&op,&x); -++x; the if(OP = =1){ the lct.access (x); the lct.splay (x); theprintf"%d\n", lct.sz[lct.ch[x][0]] +1); -}Else{ thescanf"%d",&y); the if(x + y > N) y =0; the ElseY + =x;94 lct.join (x, y); the } the } the }98 return 0; About}
View Code
Bzoj 2002: [Hnoi2010]bounce Fly sheep