Title Link: BZOJ-3282
Problem analysis
The problem is bare LCT, which contains Link, Cut, and ask for path information between two points.
Error writing code:Access (x) in the loop should be cut off the original son[x][1], but I wrote son[x][0] !
Code
#include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <cmath > #include <algorithm>using namespace std;inline void Read (int &num) {char c = getchar (); bool Neg = False;while (C < ' 0 ' | | c > ' 9 ') {if (c = = '-') Neg = True;c = GetChar ();} Num = C-' 0 '; c = GetChar (); while (c >= ' 0 ' && C <= ' 9 ') {num = num * + C-' 0 '; c = GetChar ();} if (Neg) Num =-num;} const int MAXN = 300000 + 5;int N, m;int A[MAXN], T[MAXN], FATHER[MAXN], Son[maxn][2];bool ISROOT[MAXN], Rev[maxn];inline void Update (int x) {t[x] = t[son[x][0]] ^ t[son[x][1]] ^ a[x];} inline void Reverse (int x) {Rev[x] =! Rev[x];swap (Son[x][0], son[x][1]);} inline void pushdown (int x) {if (! REV[X]) return; REV[X] = false;if (son[x][0]) Reverse (son[x][0]), if (son[x][1]) Reverse (son[x][1]);} void Rotate (int x) {int y = father[x], F; Pushdown (y); Pushdown (x); if (x = = Son[y][0]) F = 1;else f = 0;if (Isroot[y]) {isroot[y] = false;isroot[x] = true;} Else{if (y = = Son[Father[y]] [0]) son[father[y]][0] = X;else son[father[y]][1] = x;} FATHER[X] = Father[y]; son[y][f ^ 1] = Son[x][f];if (Son[x][f]) father[son[x][f]] = y; Son[x][f] = y; Father[y] = x; Update (y); Update (x);} void splay (int x) {int y;while (!isroot[x]) {y = father[x];if (Isroot[y]) {Rotate (x); break;} if (y = = Son[father[y]][0]) {if (x = = Son[y][0]) {Rotate (y); Rotate (x);} Else{rotate (x); Rotate (x);}} Else{if (x = = Son[y][1]) {Rotate (y); Rotate (x);} Else{rotate (x); Rotate (x);}}} int Access (int x) {int y = 0;while (x! = 0) {splay (x); Pushdown (x); if (son[x][1]) isroot[son[x][1]] = true; SON[X][1] = y;if (y) isroot[y] = false; Update (x); y = x;x = father[x];} return y;} void make_root (int x) {int t = Access (x); Reverse (t);} int find_root (int x) {int t = Access (x); while (son[t][0]! = 0) T = Son[t][0];return t;} int main () {scanf ("%d%d", &n, &m); for (int i = 1; I <= n; ++i) {Read (a[i]); T[i] = a[i];isroot[i] = true; Father[i] = 0;} int f, x, Y, T, FX, fy;for (int i = 1; I <= m; ++i) {Read (f); Read (x); Read (y); switch (f{case 0:make_root (x); t = Access (y);p rintf ("%d\n", T[t]), break;case 1:fx = Find_root (x), FY = Find_root (y); if (FX! = FY ) {make_root (x); Splay (x); Father[x] = y;} Break;case 2:make_root (x); Access (y); Splay (y); Pushdown (y); if (son[y][0] = = x) {Isroot[son[y][0]] = true; Father[son[y][0]] = 0; Son[y][0] = 0; Update (y);} Break;case 3:splay (x); A[x] = y; Update (x); break;}} return 0;}
[Bzoj 3282] Tree "LCT"