嗯,重新写了个模板出来。
1 #include <cstdio> 2 #include <algorithm> 3 4 const int N = 300010; 5 6 int fa[N], s[N][2], val[N], sum[N], S[N], Sp; 7 bool rev[N]; 8 9 inline bool no_root(int x) { 10 return (s[fa[x]][0] == x) || (s[fa[x]][1] == x); 11 } 12 13 inline void pushup(int x) { 14 sum[x] = val[x] ^ sum[s[x][0]] ^ sum[s[x][1]]; 15 return; 16 } 17 18 inline void pushdown(int x) { 19 if(rev[x]) { 20 if(s[x][0]) { 21 rev[s[x][0]] ^= 1; 22 } 23 if(s[x][1]) { 24 rev[s[x][1]] ^= 1; 25 } 26 std::swap(s[x][0], s[x][1]); 27 rev[x] = 0; 28 } 29 return; 30 } 31 32 inline void rotate(int x) { 33 int y = fa[x]; 34 int z = fa[y]; 35 bool f = (s[y][1] == x); 36 37 fa[x] = z; 38 if(no_root(y)) { 39 s[z][s[z][1] == y] = x; 40 } 41 s[y][f] = s[x][!f]; 42 if(s[x][!f]) { 43 fa[s[x][!f]] = y; 44 } 45 s[x][!f] = y; 46 fa[y] = x; 47 48 pushup(y); 49 pushup(x); 50 return; 51 } 52 53 inline void splay(int x) { 54 int y = x; 55 S[++Sp] = y; 56 while(no_root(y)) { 57 y = fa[y]; 58 S[++Sp] = y; 59 } 60 while(Sp) { 61 pushdown(S[Sp]); 62 Sp--; 63 } 64 65 y = fa[x]; 66 int z = fa[y]; 67 while(no_root(x)) { 68 if(no_root(y)) { 69 (s[z][1] == y) ^ (s[y][1] == x) ? 70 rotate(x) : rotate(y); 71 } 72 rotate(x); 73 y = fa[x]; 74 z = fa[y]; 75 } 76 return; 77 } 78 79 inline void access(int x) { 80 int y = 0; 81 while(x) { 82 splay(x); 83 s[x][1] = y; 84 /*if(y) { 85 fa[y] = x; // no need change fa[y] 86 }*/ 87 pushup(x); 88 y = x; 89 x = fa[x]; 90 } 91 return; 92 } 93 94 inline void make_root(int x) { 95 access(x); 96 splay(x); 97 rev[x] ^= 1; 98 return; 99 } 100 101 inline int find_root(int x) { 102 access(x); 103 splay(x); 104 while(s[x][0]) { 105 x = s[x][0]; 106 pushdown(x); // important 107 } 108 return x; 109 } 110 111 inline void link(int x, int y) { 112 make_root(x); 113 if(find_root(y) != x) { // cannot change y 114 fa[x] = y; 115 } 116 return; 117 } 118 119 inline void cut(int x, int y) { 120 make_root(x); 121 access(y); 122 splay(y); 123 if(s[y][0] == x && fa[x] == y && !s[x][1]) { 124 fa[x] = s[y][0] = 0; 125 pushup(y); 126 } 127 return; 128 } 129 130 inline int ask(int x, int y) { 131 make_root(x); 132 access(y); 133 splay(y); 134 return sum[y]; 135 } 136 137 inline void change(int x, int c) { // only could change the value of root 138 splay(x); 139 val[x] = c; 140 pushup(x); 141 return; 142 } 143 144 int main() { 145 146 int n, m; 147 scanf("%d%d", &n, &m); 148 for(int i = 1; i <= n; i++) { 149 scanf("%d", &val[i]); 150 } 151 152 for(int i = 1, f, x, y; i <= m; i++) { 153 scanf("%d%d%d", &f, &x, &y); 154 if(!f) { 155 int t = ask(x, y); 156 printf("%d\n", t); 157 } 158 else if(f == 1) { 159 link(x, y); 160 } 161 else if(f == 2) { 162 cut(x, y); 163 } 164 else { 165 change(x, y); 166 } 167 } 168 169 return 0; 170 }