洛谷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的讲课) 链剖分,是指一类 ...
随机推荐
- android linux 传文件
EStrongs File Explorer 即: Es文件浏览器 网络 -> 远程管理器 设置 ->设置根目录 linux 使用浏览器访问即可.
- nginx Location 语法基础知识
URL地址匹配是Nginx配置中最灵活的部分 Location 支持正则表达式匹配,也支持条件匹配,用户可以通过location指令实现Nginx对动丶静态网页的过滤处理. Nginx locatio ...
- Mysql多实例添加到开机自启的方法
Mysql多实例配置成功后,想让配置成开机自启. 首先看一下Linux启动的知识点,顺序如下. 1 加载内核2 执行init程序3 /etc/rc.d/rc.sysinit # 由init执行的第 ...
- Python学习第二篇
list_num=list(range(1,1000001)) print(min(list_num)) print(max(list_num)) print(sum(list_num)) print ...
- 闽江学院软件学院2016级JAVA构建之法-学生自学兴趣小组招募通知
为提升我2016级学生提升JAVA软件开发学习氛围,鼓励更多同学通过自学.团队学习.在线(社区)学习等方式学习并掌握JAVA课程,尤其是鼓励同学们通过微软中国邹欣老师所倡导的"构建之法&qu ...
- 【转】mysql热备
mysql双机热备的实现 亲测可用
- PS调出通透唯美阳光外景女生照片
1.稍微增加了一点曝光度,让照片更明亮. 2.对比度的话我现在比习惯加一点,而且 一般导入PS之后我还会按照片情况去加对比度. 3.高光的部分一般会拉回来一点,根据照片调. 4.阴影部分加一点的话会让 ...
- java.net.NoRouteToHostException:Cannot assign requ
以下内容摘自:http://blog.sina.com.cn/s/blog_658c8cea0101l2sw.html 今天压力测试时, 刚开始出现了很多异常, 都是 java.net.NoRoute ...
- 福州大学软件工程1816 | W班 第4次作业(团队展示)成绩排名
作业链接 评分细则 队员姓名与学号(标记组长),其中4-7人一组,特殊情况经老师允许后可以突破限制:(1分) 队名(体现项目内容,并要求有亮点与个性):(1分) 拟作的团队项目描述:一句话(中英文不限 ...
- Python之缩进块
pycharm编辑器识别冒号,当换行后下一行默认是缩进块的位置: