luogu3690 【模板】Link Cut Tree (动态树)
#include <iostream>
#include <cstdio>
using namespace std;
int n, m, val[300005], ch[300005][2], sum[300005], fa[300005], uu, vv, opt;
int rev[300005];
void pushDown(int x){
if(rev[x]){
swap(ch[x][0], ch[x][1]);
rev[ch[x][0]] ^= 1;
rev[ch[x][1]] ^= 1;
rev[x] = 0;
}
}
bool isRoot(int x){
return ch[fa[x]][0]!=x && ch[fa[x]][1]!=x;
}
void xf(int x){
if(!isRoot(x)) xf(fa[x]);
pushDown(x);
}
bool getW(int x){
return ch[fa[x]][1]==x;
}
void upd(int x){
sum[x] = sum[ch[x][0]] ^ sum[ch[x][1]] ^ val[x];
}
void rotate(int x){
int old=fa[x], oldf=fa[old], w=getW(x);
if(!isRoot(old)) ch[oldf][ch[oldf][1]==old] = x;
ch[old][w] = ch[x][w^1]; ch[x][w^1] = old;
fa[ch[x][w^1]] = x; fa[ch[old][w]] = old; fa[x] = oldf;
upd(old); upd(x);
}
void splay(int x){
xf(x);
while(!isRoot(x)){
int f=fa[x];
if(!isRoot(f)) rotate(getW(x)==getW(f)?f:x);
rotate(x);
}
}
void access(int x){
int y=0;
while(x){
splay(x);
ch[x][1] = y;
upd(x);
y = x;
x = fa[x];
}
}
void makeRoot(int x){
access(x);
splay(x);
rev[x] ^= 1;
}
int query(int u, int v){
makeRoot(u);
access(v);
splay(v);
return sum[v];
}
int findRoot(int x){
access(x);
splay(x);
while(ch[x][0])
x = ch[x][0];
splay(x);//谜之降低常数
return x;
}
void link(int u, int v){
makeRoot(u);
fa[u] = v;
}
void cut(int u, int v){
makeRoot(u);
access(v);
splay(v);
if(ch[u][0] || ch[u][1] || fa[u]!=v || ch[v][getW(u)^1]) return ;
ch[v][0] = fa[u] = 0;
}
void change(int u, int v){
val[u] = v;
access(u);
splay(u);
}
int main(){
cin>>n>>m;
for(int i=1; i<=n; i++)
scanf("%d", &val[i]);
while(m--){
scanf("%d %d %d", &opt, &uu, &vv);
if(opt==0) printf("%d\n", query(uu, vv));
else if(opt==1 && findRoot(uu)!=findRoot(vv))
link(uu, vv);
else if(opt==2 && findRoot(uu)==findRoot(vv))
cut(uu, vv);
else if(opt==3) change(uu, vv);
}
return 0;
}
luogu3690 【模板】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 ...
- Link Cut Tree 动态树 小结
动态树有些类似 树链剖分+并查集 的思想,是用splay维护的 lct的根是动态的,"轻重链"也是动态的,所以并没有真正的轻重链 动态树的操作核心是把你要把 修改/询问/... 等 ...
- LCT(link cut tree) 动态树
模板参考:https://blog.csdn.net/saramanda/article/details/55253627 综合各位大大博客后整理的模板: #include<iostream&g ...
- 洛谷P3690 Link Cut Tree (动态树)
干脆整个LCT模板吧. 缺个链上修改和子树操作,链上修改的话join(u,v)然后把v splay到树根再打个标记就好. 至于子树操作...以后有空的话再学(咕咕咕警告) #include<bi ...
- 洛谷P3690 [模板] Link Cut Tree [LCT]
题目传送门 Link Cut Tree 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代 ...
- 模板Link Cut Tree (动态树)
题目描述 给定N个点以及每个点的权值,要你处理接下来的M个操作.操作有4种.操作从0到3编号.点从1到N编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联 ...
- 【刷题】洛谷 P3690 【模板】Link Cut Tree (动态树)
题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor ...
- P3690 【模板】Link Cut Tree (动态树)
P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...
- LuoguP3690 【模板】Link Cut Tree (动态树) LCT模板
P3690 [模板]Link Cut Tree (动态树) 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两 ...
随机推荐
- css中伪类/伪元素详解
一.伪类和伪元素 伪类和伪元素都是用来修饰不在文档树中的部分,区别在于, 伪类用于当已有元素处于的某个状态时,为其添加对应的样式,这个状态是根据用户行为而动态变化的(如:hover/:active). ...
- Maven 中maven-assembly-plugin插件的使用笔记 SpringBoot环境
首先创建一个多模块的SpringBoot项目 项目结构 父pom的内容如下: <?xml version="1.0" encoding="UTF-8"?& ...
- UVA 674 Coin Change 硬币转换(完全背包,常规)
题意:有5种硬币,个数无限的,组成n元的不同方案有多少种? 思路:常规完全背包.重点在dp[0]=1,dp[j]中记录的是组成 j 元的方案数.状态转移方程dp[j+coin[i]]+=dp[j]. ...
- 洛谷 P2868 [USACO07DEC]观光奶牛Sightseeing Cows
题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...
- 洛谷 P3387 【模板】缩点
题目背景 缩点+DP 题目描述 给定一个n个点m条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之和最大.你只需要求出这个权值和. 允许多次经过一条边或者一个点,但是,重复经过的点,权值只 ...
- python——动态类型
1.变量,对象,引用 变量是一个系统表的元素,拥有指向对象的连接的空间: 对象是分配的一块内存: 引用是自动形成的从变量到对象的指针: 类型属于对象,而不是变量 a=3; 变量a变成对象3的一个引用 ...
- 用requests爬取图片
# coding=utf-8 from bs4 import BeautifulSoup import requests import urllib x = 1 def crawl(url): res ...
- c#中的自定义泛型类、泛型方法和泛型接口
泛型的产生其中一个原因就是为了解决原来集合类中元素的装箱和拆箱问题: 一.泛型类: /// <summary> /// 返回前台的消息 /// </summary> ...
- C#语言命名的9种规范
下面介绍C#语言命名的9种规范: a) 类 [规则1-1]使用Pascal规则命名类名,即首字母要大写. [规则1-2]使用能够反映类功能的名词或名词短语命名类. [规则1-3]不要使用“I”.“C” ...
- this经典试题
<body> <div class="container"> <h3>输出内容</h3> <pre> var name ...