[洛谷P3690]【模板】Link Cut Tree (动态树)
题目大意:给定$n$个点以及每个点的权值,要你处理接下来的$m$个操作。操作有$4$种。操作从$0到3编号。点从1到n编号。
- $0,x,y$:代表询问从$x$到$y$的路径上的点的权值的$xor$和。保证$x$到$y$是联通的。
- $1,x,y$:代表连接$x$到$y$,若$x$到$y$已经联通则无需连接。
- $2,x,y$:代表删除边$(x,y)$,不保证边$(x,y)$存在。
- $3,x,y$:代表将点$x$上的权值变成$y$。
题解:$LCT$
卡点:无
C++ Code:
#include <cstdio>
#define maxn 300010
#define lc(rt) son[rt][0]
#define rc(rt) son[rt][1]
using namespace std;
int n, m;
int V[maxn], s[maxn];
int son[maxn][2], tg[maxn], fa[maxn];
inline void swap(int &a, int &b) {a ^= b ^= a ^= b;}
inline void swap(int rt) {swap(lc(rt), rc(rt));}
inline int get(int rt, int flag = 1) {return son[fa[rt]][flag] == rt;}
inline bool is_root(int rt) {return !(get(rt, 0) || get(rt));}
inline void pushdown(int rt) {
swap(rt);
tg[lc(rt)] ^= 1, tg[rc(rt)] ^= 1, tg[rt] ^= 1;
}
inline void update(int rt) {s[rt] = s[lc(rt)] ^ s[rc(rt)] ^ V[rt];}
inline void rotate(int x) {
int y = fa[x], z = fa[y], b = get(x);
if (!is_root(y)) son[z][get(y)] = x;
fa[son[y][b] = son[x][!b]] = y; son[x][!b] = y;
fa[y] = x; fa[x] = z;
update(y); update(x);
}
int stack[maxn], top;
inline void splay(int x) {
stack[top = 1] = x;
for (int y = x; !is_root(y); stack[++top] = y = fa[y]);
for (; top; top--) if (tg[stack[top]]) pushdown(stack[top]);
for (; !is_root(x); rotate(x)) if (!is_root(fa[x]))
get(x) ^ get(fa[x]) ? rotate(x) : rotate(fa[x]);
update(x);
}
inline void access(int rt) {for (int t = 0; rt; rc(rt) = t, t = rt, rt = fa[rt]) splay(rt);}
inline void make_root(int rt) {access(rt), splay(rt), tg[rt] ^= 1;}
inline void link(int x, int y) {make_root(x); fa[x] = y;}
inline void cut(int x, int y) {make_root(x); access(y); splay(y); fa[x] = lc(y) = 0;}
inline bool connect(int x, int y) {
make_root(x); access(y); splay(y);
return fa[x] == y;
}
inline bool uni(int x, int y) {
make_root(x); access(y); splay(y);
int tmp = x;
while (tmp) tmp = fa[tmp];
return tmp == y;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &V[i]);
s[i] = V[i];
}
while (m --> 0) {
int op, x, y;
scanf("%d%d%d", &op, &x, &y);
if (op == 0) {
make_root(x); access(y); splay(y);
printf("%d\n", s[y]);
}
if (op == 1) if (!uni(x, y)) link(x, y);
if (op == 2) if (connect(x, y)) cut(x, y);
if (op == 3) {
access(x); splay(x);
V[x] = y;
update(x);
}
}
return 0;
}
[洛谷P3690]【模板】Link Cut Tree (动态树)的更多相关文章
- LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)
为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...
- 洛谷.3690.[模板]Link Cut Tree(动态树)
题目链接 LCT(良心总结) #include <cstdio> #include <cctype> #include <algorithm> #define gc ...
- 洛谷P3690 [模板] Link Cut Tree [LCT]
题目传送门 Link Cut Tree 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代 ...
- Link Cut Tree 动态树 小结
动态树有些类似 树链剖分+并查集 的思想,是用splay维护的 lct的根是动态的,"轻重链"也是动态的,所以并没有真正的轻重链 动态树的操作核心是把你要把 修改/询问/... 等 ...
- 洛谷P3690 Link Cut Tree (动态树)
干脆整个LCT模板吧. 缺个链上修改和子树操作,链上修改的话join(u,v)然后把v splay到树根再打个标记就好. 至于子树操作...以后有空的话再学(咕咕咕警告) #include<bi ...
- LCT(link cut tree) 动态树
模板参考:https://blog.csdn.net/saramanda/article/details/55253627 综合各位大大博客后整理的模板: #include<iostream&g ...
- 洛谷P2633 Count on a tree(主席树上树)
题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始为0,即第一个 ...
- 模板Link Cut Tree (动态树)
题目描述 给定N个点以及每个点的权值,要你处理接下来的M个操作.操作有4种.操作从0到3编号.点从1到N编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联 ...
- 洛谷 P2633 Count on a tree 主席树
在一棵树上,我们要求点 $(u,v)$ 之间路径的第$k$大数. 对于点 $i$ ,建立 $i$ 到根节点的一棵前缀主席树. 简单容斥后不难得出结果为$sumv[u]+sumv[v]−sumv[l ...
- 洛谷P2633 Count on a tree 主席树
传送门:主席树 解题报告: 传送门! umm这题我还麻油开始做 所以 先瞎扯一波我的想法,如果错了我就当反面教材解释这种典型错误,对了我就不管了QwQ 就直接dfs,在dfs的过程中建树 然后就直接查 ...
随机推荐
- YII2.0 用GII创建视图文件后访问404
使用GII的CRUD Generator创建searchModelClass 和控制器类文件,视图文件后,访问控制器地址后出现404的情况. 创建过程如图所示 后来发现是控制器类 Controller ...
- CPS---(Cyber-Physical Sytem,信息物理融合系统)
1.CPS定义 CPS是连接计算机虚拟世界与物理现实世界的系统.---We refer to systems that bridge the cyber-world of computing and ...
- CMDB介绍
CMDB https://lupython.gitee.io/2018/05/05/CMDB%E4%BB%8B%E7%BB%8D/ 尚泽凯博客地址 传统运维与自动化运维的区别 传统运维: 1.项目 ...
- Java学习笔记十一:Java中的方法
Java中的方法 一:什么是方法: 所谓方法,就是用来解决一类问题的代码的有序组合,是一个功能模块. 学过C语言或者其他语言的应该都知道函数这个东西,在Java中,其实方法就是函数,只不过叫法不同,在 ...
- ctf题目writeup(4)
2019.1.31 题目:这次都是web的了...(自己只略接触隐写杂项web这些简单的东西...) 题目地址:https://www.ichunqiu.com/battalion 1. 打开链接: ...
- makefile = 与 := 的区别
“=” make会将整个makefile展开后,再决定变量的值.也就是说,变量的值将会是整个makefile中最后被指定的值.看例子: x = foo y = $(x) bar ...
- VS2017 远程调试小记
VS2017 远程调试小记 支持windows\linux\macos, 直接连接项目点的上线版本代码进行调试.保证bug在同个环境下实时追踪. 注意点 双方的 msvsmon.exe版本需一致,最好 ...
- 2.PostgreSQL安装详细步骤(windows)【转】
感谢 Junn9527 PostgreSQL安装:一.windows下安装过程安装介质:postgresql-9.1.3-1-windows.exe(46M),安装过程非常简单,过程如下:1.开始安装 ...
- 『Python Kivy』官方乒乓球游戏示例解析
本篇文章用于对Kivy框架官方所给出的一个「乒乓球」小游戏的源码进行简单地解析.我会尽可能的将方方面面的内容都说清楚.在文章的最下方为官方所给出的这个小游戏的教程以及游戏源码. 由于篇幅所限,本文只简 ...
- 云计算之路-阿里云上:基于Xen的IO模型进一步分析“黑色0.1秒”问题
在发现云服务器读取OCS缓存的“黑色0.1秒”是发生在socket读取数据时,而且是发生在读取开始的字节,甚至在socket写数据时(比如写入缓存key)也会出现超过50ms的情况,我们的好奇心被激发 ...