[Lct] Luogu P3690 [template] Link Cut Tree (dynamic tree)

Title Description

Given n points and the weight of each point to be processed next m your operations. There are four kinds of operations. Operation number from 0 to 3. Point number from 1 to n.

0: followed by two integers (x, y), the representative asks xor weights point on the path from x to y and. Guarantee x to y is China Unicom.

1: followed by two integers (x, y), x represents a linking to y, if x is not necessary to connect y been connected.

2: followed by two integers (x, y), on behalf of the deletion of edge (x, y), does not guarantee edge (x, y) exists.

3: followed by two integers (x, y), on behalf of the weight value becomes the point x y.

Input and output formats

Input formats:

 

Line 1 two integers, respectively, n and m, the number of representative points, and operations.

Row 2 to row 1 + n, each line an integer, integer in [1, 10 ^ 9], the value of each point representation.

N + 2 th row to the line + m + 1 n, an integer of three rows each, representing the operation amount and the type of operation required.

 

Output formats:

 

For each operation a number 0, you should be output on the path from x to y xor point and right.

 

Sample input and output

Input Sample # 1:
3 3 
1
2
3
1 1 2
0 1 2 
0 1 1
Output Sample # 1:
3
1

Explanation

Data range:  . 1 \ Leq N, M \ Leq. 3 \ CDOT. 5 ^ {10} . 1 N , M . 3 . 1 0 . 5

 

Code

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #define N 300010
 5 using namespace std;
 6 int n,m,f[N],ch[N][2],v[N],s[N],st[N],tag[N];
 7 int nroot(int x) { return ch[f[x]][0]==x||ch[f[x]][1]==x; }
 8 void pushup(int x) { s[x]=s[ch[x][0]]^s[ch[x][1]]^v[x]; }
 9 void swap(int x) { ch[x][0]^=ch[x][1],ch[x][1]^=ch[x][0],ch[x][0]^=ch[x][1],tag[x]^=1; }
10 void pushdown(int x) { if (tag[x]) swap(ch[x][0]),swap(ch[x][1]),tag[x]=0; }
11 void rotate(int x)
12 {
13     int y=f[x],z=f[y],k=ch[y][1]==x,w=ch[x][!k];
14     if (nroot(y)) ch[z][ch[z][1]==y]=x;
15     ch[x][!k]=y,ch[y][k]=w,f[y]=x,f[x]=z;
16     if (w) f[w]=y;
17     pushup(y);
18 }
19 void splay(int x)
20 {
21     int y=x,r=0; st[++r]=y;
22     while (nroot(y)) st[++r]=y=f[y];
23     while (r) pushdown(st[r--]);
24     while (nroot(x))
25     {
26         y=f[x],r=f[y];
27         if (nroot(y)) (ch[r][1]==y)^(ch[y][1]==x)?rotate(x):rotate(y);
28         rotate(x);
29     }
30     pushup(x);
31 }
32 void access(int x) { for (int y=0;x;x=f[y=x]) splay(x),ch[x][1]=y,pushup(x); }
33 void makeroot(int x) { access(x),splay(x),swap(x); }
34 int findroot(int x)
35 {
36     makeroot(x),splay(x);
37     while (ch[x][0]) x=ch[x][0],pushdown(x);
38     splay(x); return x;
39 }
40 void split(int x,int y) { makeroot(x),access(y),splay(y); }
41 void link(int x,int y) { makeroot(x); if (findroot(y)!=x) f[x]=y; }
42 void cut(int x,int y) { split(x,y); if (ch[y][0]==x) f[x]=ch[y][0]=0,pushup(y); }
43 int main()
44 {
45     scanf("%d%d",&n,&m);
46     for (int i=1;i<=n;i++) scanf("%d",&v[i]);
47     for (int op,x,y;m;m--)
48     {
49         scanf("%d%d%d",&op,&x,&y);
50         if (op==0) split(x,y),printf("%d\n",s[y]);
51         if (op==1) link(x,y);
52         if (op==2) cut(x,y);
53         if (op==3) splay(x),v[x]=y;
54     }
55 }

 

Guess you like

Origin www.cnblogs.com/Comfortable/p/11129186.html