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. vue文档全局api笔记2

    1.Vue.filter( id, [definition] ) 在组件内注册 <template> <div id="app"> <div clas ...

  2. Python-os模块-60

    os 模块: 和操作系统打交道的模块 os模块是与操作系统交互的一个接口 os.makedirs('dirname1/dirname2') 可生成多层递归目录 os.removedirs('dirna ...

  3. Python IO模型

    这篇博客是本人借鉴一些大神的博客并结合自己的学习过程写下的. 事件驱动模型 事件驱动模型是一种编程范式,这里程序的执行流由外部事件来决定.它的特点是包含一个事件循环,当外部事件发生时,不断从队列里取出 ...

  4. Innodb 实现高并发、redo/undo MVCC原理

    一.并发控制   因为并发情况下有可能出现不同线程对同一资源进行变动,所以必须要对并发进行控制以保证数据的同一与安全.   可以参考CPython解释器中的GIL全局解释器锁,所以说python中没有 ...

  5. asp.net mvc Dateset读取Excel数据

    //处理Excel //读取Excel [NonAction] public static DataSet ExcelToDS(string Path) { //根据情况选择合适的连接字符,参考msd ...

  6. vue单页面模板说明文档(1)

    Introduction This boilerplate is targeted towards large, serious projects and assumes you are somewh ...

  7. Cookie-parser

    let express = require('express'); let app =new express(); // 引入cookie-parser; let cookieParser = req ...

  8. [转帖]中关村:LED屏幕和OLED屏幕有什么区别?答案在这里

    LED屏幕和OLED屏幕有什么区别?答案在这里   中关村在线 01-0810:40 目前的电视市场,更新换代的频率越来越快,无论是国产品牌还是合资品牌,都不约而同的推出了全新产品.这离不开人们对更好 ...

  9. [转帖]前端-chromeF12 谷歌开发者工具详解 Console篇

    前端-chromeF12 谷歌开发者工具详解 Console篇 https://blog.csdn.net/qq_39892932/article/details/82655866 趁着搞 cloud ...

  10. Sublime Text3配置

    { "default_encoding": "UTF-8", "font_size": 16.0, "tab_size" ...