洛谷P3690 Link Cut Tree (模板)
Link Cut Tree
刚开始写了个指针版。。调了一天然后放弃了。。
最后还是学了黄学长的板子!!
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
int X = 0, w = 0; char ch = 0;
while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
A ans = 1;
for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
return ans;
}
const int N = 300005;
int ch[N][2], val[N], sum[N], rev[N], tot, fa[N], st[N], n, m;
void newNode(int v){
val[++tot] = v, sum[tot] = v;
rev[tot] = ch[tot][0] = ch[tot][1] = fa[tot] = 0;
}
void reverse(int x){
swap(ch[x][0], ch[x][1]);
rev[x] ^= 1;
}
void push_up(int x){
sum[x] = val[x] ^ sum[ch[x][0]] ^ sum[ch[x][1]];
}
void push_down(int x){
if(rev[x]){
reverse(ch[x][0]), reverse(ch[x][1]);
//rev[x] ^= 1, rev[ch[x][0]] ^= 1, rev[ch[x][1]] ^= 1;
//swap(ch[x][0], ch[x][1]);
rev[x] ^= 1;
}
}
bool isRoot(int x){
return (!fa[x] || (ch[fa[x]][0] != x && ch[fa[x]][1] != x));
}
void rotate(int x){
int y = fa[x], z = fa[y], p = (ch[y][1] == x)^1;
ch[y][p^1] = ch[x][p], fa[ch[x][p]] = y;
if(!isRoot(y)) ch[z][ch[z][1] == y] = x;
fa[x] = z, ch[x][p] = y, fa[y] = x;
push_up(y), push_up(x);
}
void splay(int x){
int pos = 0; st[++pos] = x;
for(int i = x; !isRoot(i); i = fa[i]) st[++pos] = fa[i];
while(pos) push_down(st[pos --]);
while(!isRoot(x)){
int y = fa[x], z = fa[y];
if(!isRoot(y)){
if((ch[y][1] == x) ^ (ch[z][1] == y)) rotate(x);
else rotate(y);
}
rotate(x);
}
}
void access(int x){
for(int p = 0; x; p = x, x = fa[x])
splay(x), ch[x][1] = p, push_up(x);
}
void makeRoot(int x){
access(x), splay(x), reverse(x);
}
int findRoot(int x){
access(x), splay(x);
while(ch[x][0]) push_down(x), x = ch[x][0];
splay(x);
return x;
}
void split(int x, int y){
makeRoot(x), access(y), splay(y);
}
void link(int x, int y){
makeRoot(x);
if(findRoot(y) == x) return;
fa[x] = y;
push_up(y);
}
void cut(int x, int y){
makeRoot(x);
if(findRoot(y) != x || fa[y] != x || ch[y][0]) return;
ch[x][1] = fa[y] = 0;
push_up(x);
}
int main(){
n = read(), m = read();
for(int i = 1; i <= n; i ++){
int v = read(); newNode(v);
}
while(m --){
int opt = read(), x = read(), y = read();
if(opt == 0){
split(x, y); printf("%d\n", sum[y]);
}
else if(opt == 1){
link(x, y);
}
else if(opt == 2){
cut(x, y);
}
else if(opt == 3){
splay(x); val[x] = y;
}
}
return 0;
}
洛谷P3690 Link Cut Tree (模板)的更多相关文章
- 洛谷P3690 Link Cut Tree (动态树)
干脆整个LCT模板吧. 缺个链上修改和子树操作,链上修改的话join(u,v)然后把v splay到树根再打个标记就好. 至于子树操作...以后有空的话再学(咕咕咕警告) #include<bi ...
- 洛谷 P3690 Link Cut Tree
题目背景 动态树 题目描述 给定N个点以及每个点的权值,要你处理接下来的M个操作.操作有4种.操作从0到3编号.点从1到N编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor ...
- [模板][P3690]Link Cut Tree
Description: 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和 ...
- 【模板篇】Link Cut Tree模板(指针)
网上一片一片的LCT都是数组写的 orz 用指针写splay的人想用指针写LCT找板子都不好找QAQ 所以能A题了之后自然要来回报社会, 把自己的板子丢上来(然而根本没有人会看) LCT讲解就省省吧, ...
- 洛谷P3690 (动态树模板)
一位大佬写的代码.(加上我自己的一些习惯性写法) 存个模板. 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N= ...
- 【BZOJ 3282】Tree Link Cut Tree模板题
知道了为什么要换根(changeroot),access后为什么有时要splay,以及LCT的其他操作,算是比较全面的啦吧,,, 现在才知道这些,,,真心弱,,, #include<cstdio ...
- link cut tree模板(LCT模板)
update:2017.09.26 #include <bits/stdc++.h> using namespace std; struct Link_Cut_Tree { + ; ], ...
- 洛谷P3690 [模板] Link Cut Tree [LCT]
题目传送门 Link Cut Tree 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代 ...
- LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)
为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...
随机推荐
- 一文看懂Transformer内部原理(含PyTorch实现)
Transformer注解及PyTorch实现 原文:http://nlp.seas.harvard.edu/2018/04/03/attention.html 作者:Alexander Rush 转 ...
- C#.NET 大型通用信息化系统集成快速开发平台 4.1 版本 - 客户端多网络支持
客户端可以支持灵活的,中间层连接选择,由于我们系统的定位架构大型信息系统的,所以全国各地,甚至国外的用户也会有,所以需要支持全网络配置,只要配置了中间层,可以选择连接哪个中间层的服务程序.客户端可以进 ...
- Linux常见问题汇总
Linux问题: ifconfig查看IP地下载报错:bash: ifconfig: commandnotfound 解决方法: 先执行 export PATH="$PATH:/sbin&q ...
- github/gitlab同时管理多个ssh key
之前一直用github,但是github有一个不好的地方,要是创建私有的项目的话需要付费,而gitlab上则可以免费创建管理私有的项目.由于最近想把自己论文的一些东西整理一下,很多东西还是不方便公开, ...
- C++入门之初话多态与虚函数
多态性是面向对象程序设计的又一个重要思想,关于多态的详尽描述,请看本人的收藏https://www.cnblogs.com/hust-ghtao/p/3512461.html.这篇博文中,详尽的探讨了 ...
- 关于function和task的说明
1. 关于函数function调用,总结两个要点: 1. 函数调用一般产生一个值,这个值被赋值给某个变量 2. 函数所返回的值只能是一个,不可以是多个,不能像C语言中采用指针的方式返回多个值.因 ...
- 使用队列queue实现一个简单的生产者消费者模型
一.生产者消费者模型 我们去超市商店等地购买商品时,我们大部分人都会说自己是消费者,而超市的各大供货商.工厂等,自然而然地也就成了我们的生产者.如此一来,生产者有了,消费者也有了,那么将二者联系起来的 ...
- mysql cpu 100% 满 优化方案
解决MySQL CPU占用100%的经验总结 - karl_han的专栏 - CSDN博客 https://blog.csdn.net/karl_han/article/details/5630782 ...
- Docker bridge br0 pipework
Docker Centos7 下建立 Docker 桥接网络 - weifengCorp - 博客园https://www.cnblogs.com/weifeng1463/p/7468497.html ...
- asp.net mvc Areas 母版页动态获取数据进行渲染
经常需要将一些通用的页面元素抽离出来制作成母版页,但是这里的元素一般都是些基本元素,即不需要 进行后台数据交换的基本数据,但是对于一些需要通过后台查询的数据,我们应该怎么传递给前台的母版页呢 这里描述 ...