题目大意:给定$n$个点以及每个点的权值,要你处理接下来的$m$个操作。操作有$4$种。操作从$0到3编号。点从1到n编号。

  1. $0,x,y$:代表询问从$x$到$y$的路径上的点的权值的$xor$和。保证$x$到$y$是联通的。
  2. $1,x,y$:代表连接$x$到$y$,若$x$到$y$已经联通则无需连接。
  3. $2,x,y$:代表删除边$(x,y)$,不保证边$(x,y)$存在。
  4. $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 (动态树)的更多相关文章

  1. LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)

    为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...

  2. 洛谷.3690.[模板]Link Cut Tree(动态树)

    题目链接 LCT(良心总结) #include <cstdio> #include <cctype> #include <algorithm> #define gc ...

  3. 洛谷P3690 [模板] Link Cut Tree [LCT]

    题目传送门 Link Cut Tree 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代 ...

  4. Link Cut Tree 动态树 小结

    动态树有些类似 树链剖分+并查集 的思想,是用splay维护的 lct的根是动态的,"轻重链"也是动态的,所以并没有真正的轻重链 动态树的操作核心是把你要把 修改/询问/... 等 ...

  5. 洛谷P3690 Link Cut Tree (动态树)

    干脆整个LCT模板吧. 缺个链上修改和子树操作,链上修改的话join(u,v)然后把v splay到树根再打个标记就好. 至于子树操作...以后有空的话再学(咕咕咕警告) #include<bi ...

  6. LCT(link cut tree) 动态树

    模板参考:https://blog.csdn.net/saramanda/article/details/55253627 综合各位大大博客后整理的模板: #include<iostream&g ...

  7. 洛谷P2633 Count on a tree(主席树上树)

    题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始为0,即第一个 ...

  8. 模板Link Cut Tree (动态树)

    题目描述 给定N个点以及每个点的权值,要你处理接下来的M个操作.操作有4种.操作从0到3编号.点从1到N编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联 ...

  9. 洛谷 P2633 Count on a tree 主席树

    在一棵树上,我们要求点 $(u,v)$ 之间路径的第$k$大数. 对于点 $i$  ,建立 $i$  到根节点的一棵前缀主席树. 简单容斥后不难得出结果为$sumv[u]+sumv[v]−sumv[l ...

  10. 洛谷P2633 Count on a tree 主席树

    传送门:主席树 解题报告: 传送门! umm这题我还麻油开始做 所以 先瞎扯一波我的想法,如果错了我就当反面教材解释这种典型错误,对了我就不管了QwQ 就直接dfs,在dfs的过程中建树 然后就直接查 ...

随机推荐

  1. 【控制连接实现信息共享---linux和设备下ssh和远程连接telnet服务的简单搭建】

    SSH的配置 空密码登陆ssh server 如果要登录ssh server通常要在server和client之间采取具有共同加密的秘钥,若每次当client想要了:连接ssh server时都要手工 ...

  2. 【PBR的基本配置】

    PBR基于策略路由的配置 一:基于报文协议的本地PBR 1:首先进行理论分析:在SW1上利用基于报文报文协议类型的PBR,在sw1与sw3的连接链路上,利用acl制定允许tcp的报文通过3000,并与 ...

  3. Angular2入门学习

    最近项目使用angular2,1和2版本变化大变样.下面总结一些学习网址及安装步骤. 中文官网(必看): https://angular.cn 懒人学习: http://www.imooc.com/l ...

  4. php后端跨域Header头

    header("Access-Control-Allow-Origin: http://a.com"); // 允许a.com发起的跨域请求 //如果需要设置允许所有域名发起的跨域 ...

  5. 网站mysql防止sql注入攻击 3种方法总结

    mysql数据库一直以来都遭受到sql注入攻击的影响,很多网站,包括目前的PC端以及手机端都在使用php+mysql数据库这种架构,大多数网站受到的攻击都是与sql注入攻击有关,那么mysql数据库如 ...

  6. Kubernetes-tutorials(五)

    The tutorials use Katacoda to run a virtual terminal in your web browser that runs Minikube, a small ...

  7. 最短路径算法 1.Floyed-Warshall算法

    这几周开始正式系统学习图论,新学期开始新的记录.由于二模和生物地理两门高考的临近,时间比较仓促,所以暂时跳过图论的(一)和(二),即图的储存和遍历.从最短路径算法学起,首先要学习的是Floyed-Wa ...

  8. ajax同步和异步的切换

    ajax为网页提供了非常不错的异步机制,但是有时候两个ajax放在一起,希望第一个完成后再继续第二个ajax的执行.这时候可以将第一个ajax代码带上同步参数即可,如下: $.ajax({ async ...

  9. C++语言入门知识点(详细版)【持续更新每周三更】,小舒舒戳这里!!!

    时间过得好快啊,LITTLESUN已经在这块新地图摸打滚爬了一个多月了.前一段时间出了点小意外一直没能更新博客,昨天被小舒舒催更了(惭愧惭愧)便准备着手来一篇回忆录回首一下这一个月走过的风风雨雨,也希 ...

  10. How to find your web part

         When we deploy a web part, we can find it on any pages through the follow steps:      Firstly, ...