Main Topic
Given a sequence, each point has a weight of a[i], if a sheep standing on the point I will be bounced to the i+a[i] point, support the single-point modification operation, from a point to go through how many times will be bounced.
Solving
So that the Father node of each point is the node that will be bounced, and the answer to each point when asked is the depth of the point.
LCT Maintain the Size field
At first thought is to have to the tree, later found himself committed 2, according to the practice of non-direction can.
Do not have any hands on this problem. Because of a sentence of X->rever (), I hit the x->rev^=1, I have the following results, adjusted for a whole day!
Code
#include <cstdio>#include <algorithm>#define MAXN 200005Using namespace Std;int N, m, val[maxn];struct node{bool rev; int s; Node *fa, *LC, *RC; Node () {rev =false; s =0; FA = LC = RC = NULL; } node (bool xrev, int xs, node *XFA, node *xlc, node *XRC) {rev = Xrev; s = xs; FA = XFA; LC = XLC; rc = XRC; } inlinevoidUpdate (); InlinevoidRever ();voidPushdown ();} *nil =NewNode (), *T[MAXN], *s[maxn];inlinevoidNode:: Update () {s = LC -S + RC -S +1;} InlinevoidNode:: Rever () {Rev ^=1; Swap (LC, RC);}voidNode:: Pushdown () {if(rev) {rev =false; Lc -Rever (); Rc -Rever (); }}voidZig (node *x) {Node *y = x -Fa Y -LC = X -Rc X -Rc -fa = y; X -rc = y; X -FA = y -Faif(y = = y -Fa -LC) Y -Fa -LC = x;Else if(y = = y -Fa -RC) y -Fa -rc = x; Y -FA = x; Y -Update ();}voidZag (node *x) {Node *y = x -Fa Y -rc = x -Lc X -Lc -fa = y; X -LC = y; X -FA = y -Faif(y = = y -Fa -LC) Y -Fa -LC = x;Else if(y = = y -Fa -RC) y -Fa -rc = x; Y -FA = x; Y -Update ();}voidSplay (node *x) {int top =0; s[top++] = x; for(Node *i = x; i = = I -Fa -LC | | i = = I -Fa -Rc i = i -FA) {s[top++] = I -Fa } while(top--) S[top] -Pushdown (); Node *y = nil, *z = nil; while(x = = X -Fa -LC | | x = = X -Fa -RC) {y = X -Fa z = y -Faif(x = = y -LC) {if(Y = = Z -LC) Zig (y); Zig (x); }Else{if(Y = = Z -RC) zag (y); Zag (x); }} X -Update ();} InlinevoidAccess (node *x) { for(Node *y = nil; x! = Nil; y = x, x = x -FA) {splay (x); X -rc = y; X -Update (); }}inlinevoidMakeroot (node *x) {access (x); splay (x); x -Rever ();} InlinevoidLNK (node *x, node *y) {makeroot (x); X -fa = y;} InlinevoidCut (node *x, node *y) {makeroot (x); Access (y); Splay (y); X -FA = y -LC = NIL; Y -Update ();} inline int Query (node *x) {makeroot (t[n]); Access (x); Splay (x);returnX -Lc -s;} int main () {int opt, a, b; scanf"%d", &n); *nil = Node (false,0, nil, nil, nil); for(int i =0; I <= N; ++i) T[i] =NewNodefalse,1, nil, nil, nil); for(int i =0; I < n; ++i) {scanf ("%d", &val[i]); T[i] -FA = T[min (n, i + val[i]); } scanf ("%d", &m); while(m--) {scanf ("%d", &opt);if(opt = =1) {scanf ("%d", &a); printf"%d\n", query (T[a])); }Else{scanf ("%d%d", &a, &b); Cut (T[a], T[min (n, a + val[a])); Val[a] = b; LNK (T[a], T[min (n, a + val[a])); } } for(int i =0; I < n; ++i) {DeleteT[i]; T[i] = NULL; }DeleteNil nil = NULL;return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hnoi2010 the LCT of Flying sheep