splay
void rotate(int o) {
if (!o || !t[o].fa) return;
int f = t[o].fa, g = t[f].fa;
if (g) {
if (ck(f)) t[g].ls = o;
else t[g].rs = o;
}
t[o].fa = g;
if (ck(o)) {
t[f].ls = t[o].rs;
if (t[o].rs) t[t[o].rs].fa = f;
t[o].rs = f;
}
else {
t[f].rs = t[o].ls;
if (t[o].ls) t[t[o].ls].fa = f;
t[o].ls = f;
}
t[f].fa = o;
pushup(f); pushup(o);
}
void splay(int o, int goal = 0) {
if (!o || t[o].fa == goal) return;
int f = t[o].fa, g = t[f].fa;
if (g != goal) {
if (ck(o) == ck(f)) rotate(f);
else rotate(o);
}
rotate(o);
splay(o, goal);
if (!goal) root = o;
}
void insert(int& o, int x, int fa = 0) {
if (!o) {
o = ++tot;
t[o] = { 0, 0, fa, 1, x };
splay(o);
return;
}
if (x <= t[o].val) insert(t[o].ls, x, o);
else insert(t[o].rs, x, o);
pushup(o);
}
int nex(int o, int x, int type) {
if (!o) return 0;
if ((type && t[o].val <= x) || (!type && t[o].val >= x))
return nex(type ? t[o].rs : t[o].ls, x, type);
int res = nex(type ? t[o].ls : t[o].rs, x, type);
return res ? res : o;
}
void del(int x) {
int pre = nex(root, x, 0);
int nxt = nex(root, x, 1);
splay(pre); splay(nxt, pre);
t[nxt].ls = 0;
pushup(nxt); pushup(pre);
}
// 按值分裂:将树分为<=p的l和>p的r
void split(int p, int& l, int& r) {
if (!root) { l = r = 0; return; }
splay(p); // 将p旋转到根
if (t[p].val <= p) {
l = p;
r = t[p].rs;
t[r].fa = 0;
t[p].rs = 0;
} else {
r = p;
l = t[p].ls;
t[l].fa = 0;
t[p].ls = 0;
}
pushup(p);
}
// 合并两棵树(l的所有节点<r的所有节点)
int merge(int l, int r) {
if (!l) return r;
if (!r) return l;
// 找到l的最大节点并splay到根
int u = l;
while (t[u].rs) u = t[u].rs;
splay(u);
// 连接r作为右子树
t[u].rs = r;
t[r].fa = u;
pushup(u);
return u;
}
lct
P3690 【模板】动态树(LCT)
题目描述
给定
n
n
n 个点以及每个点的权值,要你处理接下来的
m
m
m 个操作。
操作有四种,操作从
0
0
0 到
3
3
3 编号。点从
1
1
1 到
n
n
n 编号。
0 x y
代表询问从 x x x 到 y y y 的路径上的点的权值的 xor \text{xor} xor 和。保证 x x x 到 y y y 是联通的。1 x y
代表连接 x x x 到 y y y,若 x x x 到 y y y 已经联通则无需连接。2 x y
代表删除边 ( x , y ) (x,y) (x,y),不保证边 ( x , y ) (x,y) (x,y) 存在。3 x y
代表将点 x x x 上的权值变成 y y y。
输入格式
第一行两个整数,分别为 n n n 和 m m m,代表点数和操作数。
接下来 n n n 行,每行一个整数,第 ( i + 1 ) (i + 1) (i+1) 行的整数 a i a_i ai 表示节点 i i i 的权值。
接下来 m m m 行,每行三个整数,分别代表操作类型和操作所需的量。
输出格式
对于每一个 0 0 0 号操作,你须输出一行一个整数,表示 x x x 到 y y y 的路径上点权的 xor \text{xor} xor 和。
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
struct node {
int fa, ch[2], sum, val, lazy;
} t[N]; // lazy用来标记reverse()的左右翻转
#define lc t[x].ch[0] // 左儿子
#define rc t[x].ch[1] // 右儿子
bool isRoot(int x) {
int g = t[x].fa;
return t[g].ch[0] != x && t[g].ch[1] != x; // 若为根,则父节点不应该有这个儿子
}
void pushup(int x) { // 本题求路径异或和.上传信息
t[x].sum = t[x].val ^ t[lc].sum ^ t[rc].sum;
}
void reverse(int x) {
if (!x) return;
swap(lc, rc); // 翻转x的左右儿子
t[x].lazy ^= 1; // Lazy标记,先不翻转儿子的后代,后面再翻转
}
void pushdown(int x) { // 递归翻转x的儿子的后代,并释放Lazy标记
if (t[x].lazy) {
reverse(lc);
reverse(rc);
t[x].lazy = 0;
}
}
void push(int x) {
if (!isRoot(x)) push(t[x].fa); // 从根到x全部翻转
pushdown(x);
}
void rotate(int x) {
int y = t[x].fa;
int z = t[y].fa;
int k = t[y].ch[1] == x;
if (!isRoot(y)) t[z].ch[t[z].ch[1] == y] = x;
t[x].fa = z;
t[y].ch[k] = t[x].ch[k ^ 1];
if (t[x].ch[k ^ 1]) t[t[x].ch[k ^ 1]].fa = y;
t[y].fa = x;
t[x].ch[k ^ 1] = y;
pushup(y);
pushup(x);
}
void splay(int x) { // 提根: 把x旋转为它所在的Splay树的根
int y, z;
push(x); // 先翻转处理x的所有子孙的Lazy标记
while (!isRoot(x)) {
y = t[x].fa, z = t[y].fa;
if (!isRoot(y))
(t[z].ch[0] == y) ^ (t[y].ch[0] == x) ? rotate(x) : rotate(y);
rotate(x);
}
pushup(x);
}
void access(int x) { // 在原树上建一条实链,起点是根,终点是x
for (int child = 0; x; child = x, x = t[x].fa) { // 从x向上走,沿着虚边走
splay(x);
rc = child; // 右孩子是child,建立了一条实边
pushup(x);
}
}
void makeroot(int x) { // 把x在原树上旋转到根的位置
access(x);
splay(x);
reverse(x);
}
void split(int x, int y) { // 把原树上以x为起点,以y为终点的路径,生成一条实链
makeroot(x);
access(y);
splay(y);
}
int findroot(int x) { // 查找x在原树上的根
access(x);
splay(x);
while (lc) pushdown(x), x = lc; // 找Splay树最左端的节点
splay(x);
return x;
}
void link(int x, int y) { // 在节点x和y之间连接一条边
makeroot(x);
if (findroot(y) != x) t[x].fa = y;
}
void cut(int x, int y) { // 将x,y的边切断
makeroot(x);
if (findroot(y) == x && t[y].fa == x && !t[y].ch[0]) {
t[y].fa = t[x].ch[1] = 0;
pushup(x);
}
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
scanf("%d", &t[i].val);
t[i].sum = t[i].val;
}
while (m--) {
int opt, a, b;
scanf("%d%d%d", &opt, &a, &b);
switch (opt) {
case 0:
split(a, b);
printf("%d\n", t[b].sum);
break;
case 1:
if (findroot(a) != findroot(b)) link(a, b);
break;
case 2:
cut(a, b);
break;
case 3:
splay(a);
t[a].val = b;
pushup(a);
break;
}
}
return 0;
}