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 (模板)的更多相关文章

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

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

  2. 洛谷 P3690 Link Cut Tree

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

  3. [模板][P3690]Link Cut Tree

    Description: 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和 ...

  4. 【模板篇】Link Cut Tree模板(指针)

    网上一片一片的LCT都是数组写的 orz 用指针写splay的人想用指针写LCT找板子都不好找QAQ 所以能A题了之后自然要来回报社会, 把自己的板子丢上来(然而根本没有人会看) LCT讲解就省省吧, ...

  5. 洛谷P3690 (动态树模板)

    一位大佬写的代码.(加上我自己的一些习惯性写法) 存个模板. 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N= ...

  6. 【BZOJ 3282】Tree Link Cut Tree模板题

    知道了为什么要换根(changeroot),access后为什么有时要splay,以及LCT的其他操作,算是比较全面的啦吧,,, 现在才知道这些,,,真心弱,,, #include<cstdio ...

  7. link cut tree模板(LCT模板)

    update:2017.09.26 #include <bits/stdc++.h> using namespace std; struct Link_Cut_Tree { + ; ], ...

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

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

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

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

随机推荐

  1. Python全栈开发之路 【第一篇】:Python 介绍

    本节内容 一.Python介绍 python的创始人为荷兰人——吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本 ...

  2. Cat8 八类网线是什么?与Cat5、Cat6、Cat7网线的区别?

    若您身处于网络通信行业,相信您应该了解一些以太网线缆,比如说超五类网线.六类网线和七类网线等等.但是您知道Cat8 八类网线 是什么吗?它与五类网线.六类/超六类网线及七类/超七类网线有着怎么样的区别 ...

  3. Unit 2.前端之html--table(表格),form(表单)标签

    一.table标签 作用:定义html表格.一个table标签元素至少包含 thead(表头),tbody(表主题),还可以有tfoot(表底部) html表格游table元素及一个或者多个tr,th ...

  4. jq简单仿上传文件

    html: <div> <input id="lefile" type="file" style="display:none&quo ...

  5. v-show 与 v-if区别

    关于条件渲染 所谓条件渲染,就是根据不同的条件,使用不同的模板来生成 html. 在 Vue.js 中,使用 v-if 和 v-show 指令来控制条件渲染. 区别 v-show 会在app初始化的时 ...

  6. 【译】Focused and Diffuse Modes(专注与发散模式)

    Focused and Diffuse Modes ---文章来源:coursera 面对一个问题,当无论如何都想不出办法时,你会怎么做呢?对于僵尸们来说,只需简单地不断用脑袋撞墙即可.然而活生生的大 ...

  7. python之路--模块和包

    一 . 模块 ⾸先,我们先看⼀个老⽣常谈的问题. 什么是模块. 模块就是⼀个包含了python定义和声明的⽂件, ⽂件名就是模块的名字加上.py后缀. 换句话说我们⽬前写的所有的py⽂件都可以看成是⼀ ...

  8. 用 Python 写一个多进程兼容的 TimedRotatingFileHandler

    我前面有篇文章已经详细介绍了一下 Python 的日志模块.Python 提供了非常多的可以运用在各种不同场景的 Log Handler. TimedRotatingFileHandler 是 Pyt ...

  9. elasticsearch的映射

    一.简介: 映射:在创建索引时,可以预先定义字段的类型(映射类型,也就是type,一个索引可以有一个或多个类型)及相关属性. Elasticsearch会根据JSON源数据的基础类型猜测你想要的字段映 ...

  10. AVL树,红黑树

    AVL树 https://baike.baidu.com/item/AVL%E6%A0%91/10986648 在计算机科学中,AVL树是最先发明的自平衡二叉查找树.在AVL树中任何节点的两个子树的高 ...