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:后接两 ...
随机推荐
- They say Rome wasn't built in a day, and yet what a difference a day makes.
They say Rome wasn't built in a day, and yet what a difference a day makes.有人说罗马不是一天建成的,但一天却能改变很多事.
- macOS Sierra 最新系统找回允许任何软件安装
终端输入就可以了 安装macOS Sierra后,会发现系统偏好设置的“安全与隐私”中默认已经去除了允许“任何来源”App的选项,无法运行一些第三方应用. 如果需要恢复允许“任何来源”的选项,即关闭G ...
- 创建XML的用法
注意:在实际开发中,注意createElement().createAttribute().createTextNode().appendchild()等方法的具体使用. // root根节点的属性数 ...
- 11gR2 新特性: Rebootless Restart
众所周知,当集群出现问题时,例如某个节点丢失网络心跳,或者不能够访问表决盘,或者节点出现了严重的性能问题等,CRS会选择将某个节点的OS 重启,以便保证集群的一致性.当然,大部分的重启都是由CRS的核 ...
- [C++讨论课] 课堂记录(一)
今天第一次参加c++讨论课,记录下了各组同学的展示的问题或者解决方法,也有一些知识点上的内容,供以后复习参考. 1.常量指针和指针常量问题 常量指针:指向常量的指针,例如const int *p = ...
- Array - Container With Most Water
/** * 此为暴力解法 * Find two lines, which together with x-axis forms a container, such that the container ...
- python 基础之while无限循环
用户登录程序 username = "chenxi" passwed = "testki" counter = 0 while counter < 3: ...
- 爬虫3_python2
# coding=utf-8 import urllib params=urllib.urlencode({'t':1,'eggs':2,'bacon':0})#现在大多数网站都是动态网页,需要你动态 ...
- c#和Java中的多态
多态:让一个对象表现出多种类型,写出通用的代码,最大限度的屏蔽各个子类之间的差异性. c#举例: 将父类的方法标记为虚方法 ,使用关键字 virtual,这个函数可以被子类重新写一个遍. //真的鸭子 ...
- nodejs个人博客系统
说明:本人目前还是一名C#程程序,在公司干过一年的前端(ps切图,html+css,js),二年的后台C#(b/s,c/s)的开发.因为想转型所以学习了nodejs这门感觉非常棒的一门语言.于是写了一 ...