[Luo Gu P3203] [bzoj2002] [Hnoi2010] Bounce Danfei sheep (LCT)

Topic Link

LCT first time the subject still little excited QAQ

If a given relationship to simplify the subject, will be seen as Danfei root of the tree, the entire sequence can be seen as a tree. Then modify the operation is to modify the parent of a node, the depth of a query is a query node.

If modify operation, it is first off the side edge again even if a query operation, it will be attached to the root node x with a splay then Fengyun splay of the query nodes, this is the size siz +1 we maintain.

. 1 #include <bits / STDC ++ H.>
 2  the using  namespace STD;
 . 3 typedef Long  Long LL;
 . 4  const  int MAXN = 3E5 + 10 ;
 . 5  int FA [MAXN], CH [MAXN] [ 2 ], SIZ [MAXN], Val [MAXN], the lazy [MAXN], ST [MAXN]; // parent node, left son node, the number of the number of child nodes, the current value, the lazy marker, the current auxiliary array. 
. 6 inline int Read () {
 . 7      int X = 0 , F = . 1 ; char CH = getchar ();
 . 8      the while (CH < ' 0' || CH> ' . 9 ' ) { IF (CH == ' - ' ) F = - . 1 ; CH = getchar ();}
 . 9      the while (CH> = ' 0 ' && CH <= ' . 9 ' ) = {X * x 10 + CH - 48 ; CH = getchar ();}
 10      return x * F;
 . 11  }
 12 is inline BOOL isRoot ( int x) { // determines whether x is for splay root 
13 is      return CH [FA [x] ] [ 0] != x && ch[fa[x]][1] != x;
14 }
15 inline void pushup(int x) {
16     siz[x] = siz[ch[x][1]] + siz[ch[x][0]] + 1;
17 }
18 inline void pushdown(int x) {
19     if (lazy[x]) {
20         swap(ch[x][0], ch[x][1]);
21         if (ch[x][0])lazy[ch[x][0]] ^= 1;
22         if (ch[x][1])lazy[ch[x][1]] ^= 1;
23         lazy[x] = 0;
24     }
25 }
26 inline void rotate(int x) {
27     int y = fa[x], z = fa[y];
28     int k = ch[y][1] == x;
29     if (!isroot(y))
30         ch[z][ch[z][1] == y] = x;
31     fa[x] = z; ch[y][k] = ch[x][k ^ 1]; fa[ch[x][k ^ 1]] = y;
32     ch[x][k ^ 1] = y; fa[y] = x;
33     pushup(y);
34     pushup(x);
35 }
36 inline void splay(int x) {
37     int f = x, len = 0;
38     st[++len] = f;
39     while (!isroot(f))st[++len] = f = fa[f];
40     while (len)pushdown(st[len--]);
41     while (!isroot(x)) {
42         int y = fa[x];
43 is          int Z = ; FA [Y]
 44 is          IF (! IsRoot (Y))
 45              Rotate ((CH [Y] [ 0 ] == X) ^ (CH [Z] [ 0 ?] == Y) X: Y );
 46 is          Rotate (x);
 47      }
 48      a pushup (x);
 49  }
 50 inline void Access ( int x) { // get through to the root node x real chain 
51 is      for ( int Y = 0 ; x; x = FA [Y = X])
 52 is          The splay (X), CH [X] [ . 1 ] = Y, a pushup (X);
53 is  }
 54 is inline void makeroot ( int x) { // x to the original root of the tree becomes 
55      Access (x); The splay (x); the lazy [x] = ^ . 1 ;
 56 is  }
 57 is  int FindRoot ( int x) { // Get the root 
58      Access (X), The splay (X);
 59      the while (CH [X] [ 0 ])
 60          pushdown (X), X = CH [X] [ 0 ];
 61 is      The splay (X);
 62 is      return X;
 63 is  }
 64 inline void Split ( int x, int y) { makeroot(x); access(y); splay(y); }
65 inline void Link(int x, int y) { makeroot(x); fa[x] = y; }
66 inline void cut(int x, int y) { makeroot(x); access(y); splay(y); fa[x] = ch[y][0] = 0; pushup(y); }//合法断边
67 int main() {
68     int n, m;
69     n = read();
70     for (int i = 1; i <= n; i++) {
71         val[i] = read();
72         if (i + val[i] <= n)
73             Link(i, i + val[i]);
74         else
75             Link(i, n + 1);
76     }
77     m = read();
78     while (m--) {
79         int opt, x, y;
80         opt = read(), x = read() + 1;
81         if (opt == 2) {
82             y = read();
83             cut(x, x + val[x] <= n ? x + val[x] : n + 1);
84             Link(x, x + y <= n ? x + y : n + 1);
85             val[x] = y;
86         }
87         else {
88             split(x, n + 1);
89             printf("%d\n", siz[n + 1] - 1);
90         }
91     }92 }

 

Guess you like

Origin www.cnblogs.com/sainsist/p/11414631.html