一个没有维护任何东西的动态树模板

忘了怎么写可以直接来粘

int ch[300010][2], fa[300010], st[300010];
bool lazy[300010]; bool nroot(int x) { return ch[fa[x]][0] == x || ch[fa[x]][1] == x; }
void rev(int x) { swap(ch[x][0], ch[x][1]), lazy[x] ^= 1; }
void pushup(int x) { /*维护一个pre*/ } void pushdown(int x)
{
if (lazy[x])
{
if (ch[x][0]) rev(ch[x][0]);
if (ch[x][1]) rev(ch[x][1]);
lazy[x] = 0;
}
} void rotate(int x)
{
int y = fa[x], z = fa[y], k = ch[y][1] == x, w = ch[x][k ^ 1];
if (nroot(y)) { ch[z][ch[z][1] == y] = x; } ch[x][k ^ 1] = y, ch[y][k] = w;
if (w) { fa[w] = y; } fa[y] = x; fa[x] = z; pushup(y), pushup(x);
} void splay(int x)
{
int y = x, top = 0;
st[++top] = y;
while (nroot(y)) st[++top] = y = fa[y];
while (top > 0) pushdown(st[top--]);
while (nroot(x))
{
int y = fa[x], z = fa[y];
if (nroot(y)) rotate((ch[y][1] == x) ^ (ch[z][1] == y) ? x : y);
rotate(x);
}
pushup(x);
} void access(int x)
{
for (int y = 0; x > 0; x = fa[y = x])
splay(x), ch[x][1] = y, pushup(x);
} void makert(int x)
{
access(x), splay(x), rev(x);
} int findrt(int x)
{
access(x), splay(x);
while (ch[x][0]) pushdown(x), x = ch[x][0];
return x;
} void link(int x, int y)
{
makert(x);
if (findrt(y) != x) fa[x] = y;
} void cut(int x, int y)
{
makert(x);
if (findrt(y) == x && fa[x] == y && ch[x][1] == 0)
ch[y][0] = fa[x] = 0, pushup(y);
}

upd:压行Link-Cut Tree模板

bool nroot(int x) { return ch[fa[x]][0] == x || ch[fa[x]][1] == x; }
void rev(int x) { swap(ch[x][0], ch[x][1]), lazy[x] ^= 1; }
void pushup(int x) { /*维护一个pre*/ }
void pushdown(int x) { if (lazy[x]) { if (ch[x][0]) { rev(ch[x][0]); } if (ch[x][1]) { rev(ch[x][1]); } lazy[x] = 0; } }
void rotate(int x)
{
int y = fa[x], z = fa[y], k = ch[y][1] == x, w = ch[x][k ^ 1];
if (nroot(y)) { ch[z][ch[z][1] == y] = x; } ch[x][k ^ 1] = y, ch[y][k] = w;
if (w) { fa[w] = y; } fa[y] = x; fa[x] = z; pushup(y), pushup(x);
}
void splay(int x)
{
int y = x, top = 0; st[++top] = y; while (nroot(y)) { st[++top] = y = fa[y]; } while (top > 0) { pushdown(st[top--]); }
while (nroot(x)) { int y = fa[x], z = fa[y]; if (nroot(y)) { rotate((ch[y][1] == x) ^ (ch[z][1] == y) ? x : y); } rotate(x); }
}
void access(int x) { for (int y = 0; x > 0; x = fa[y = x]) splay(x), ch[x][1] = y, pushup(x); }
void makert(int x) { access(x), splay(x), rev(x); }
int findrt(int x) { access(x), splay(x); while (ch[x][0]) { pushdown(x), x = ch[x][0]; } return x; }
void link(int x, int y) { makert(x); if (findrt(y) != x) fa[x] = y; }
void cut(int x, int y) { makert(x); if (findrt(y) == x && fa[x] == y && ch[x][1] == 0) ch[y][0] = fa[x] = 0, pushup(y); }

通用动态树(Link-Cut Tree)模板的更多相关文章

  1. 动态树(Link Cut Tree) :SPOJ 375 Query on a tree

    QTREE - Query on a tree #number-theory You are given a tree (an acyclic undirected connected graph) ...

  2. 洛谷P3690 Link Cut Tree (模板)

    Link Cut Tree 刚开始写了个指针版..调了一天然后放弃了.. 最后还是学了黄学长的板子!! #include <bits/stdc++.h> #define INF 0x3f3 ...

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

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

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

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

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

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

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

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

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

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

  8. P3690 【模板】Link Cut Tree (动态树)

    P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...

  9. 【刷题】洛谷 P3690 【模板】Link Cut Tree (动态树)

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

  10. LuoguP3690 【模板】Link Cut Tree (动态树) LCT模板

    P3690 [模板]Link Cut Tree (动态树) 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两 ...

随机推荐

  1. 搭建Easyui环境在Myeclipse或Eclipse中

    转自:https://www.cnblogs.com/henuyuxiang/p/4283018.html 1.下载Easyui.网址:http://www.jeasyui.com/download/ ...

  2. .Net Core 迁移之坑一 《WebAPI Get请求参数传入输入带有[]不识别问题》

    在Framwork 体系下 WebAPI项目 会有很多默认特性,例如:Get查询竟然支持三种数组查询方式 1.https://localhost:44390/api/values?status=1&a ...

  3. CentOS 调用.Net 的Web Service,提示连接超时解决方案

    我是使用axis调用.NET 的Web Service ,在Window下跑没有问题,将项目部署到Linux下,发现Web Service 连接超时,百度了下,发现是因为Linux不能直接跑.Net, ...

  4. Mac mysql-忘记数据库密码

    第一步: 关闭mysql服务:苹果->系统偏好设置最下边点mysql 在弹出页面中关闭mysql服务(点击stop mysql server) 第二步: 进入终端输入:cd /usr/local ...

  5. 【bzoj1024】[SCOI2009]生日快乐

    1024: [SCOI2009]生日快乐 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2372  Solved: 1717[Submit][Statu ...

  6. C++——STL之vector, list, deque容器对比与常用函数

    STL 三种顺序容器的特性对比: vector 可变数组,内存空间是连续的,容量不会进行缩减.支持高效随机存取,即支持[]和at()操作.尾部插入删除效率高,其他位置插删效率较低: list 双向链表 ...

  7. php+mysql网站无限级栏目分类-递归获取树形结构函数

    如果网站采用了无限级栏目结构,我们可以将网站所有栏目获取出来组成一个树形结构.数据库结构: 函数代码: //获得指定文章分类的子分类组成的树形结构 function cateTree($pid=0,$ ...

  8. python3--装饰器高级学习版

    __author__ = "Aaron Fan"import time #导入time模块user,passwd = 'alex','abc123' #用户名密码def auth( ...

  9. Part9---代码搬移不可少

    1.回顾ARM启动流程就可知道需要执行代码搬移 2.代码搬移 1)起点:NAND FLASH,今天的起点是SRAM垫脚石.为什么?因为我们要从nandflash取搬移数据需要先对其进行初始化,二而我们 ...

  10. 3.python 发送邮件之smtplib模块

    SMTP(Simple Mail Transfer Protocol)是简单邮件传输协议,它是一组用于由源地址到目的地址的邮件传输规则. python中对SMTP进行了简单的封装,可以发送纯文本邮件, ...