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

忘了怎么写可以直接来粘

  1. int ch[300010][2], fa[300010], st[300010];
  2. bool lazy[300010];
  3. bool nroot(int x) { return ch[fa[x]][0] == x || ch[fa[x]][1] == x; }
  4. void rev(int x) { swap(ch[x][0], ch[x][1]), lazy[x] ^= 1; }
  5. void pushup(int x) { /*维护一个pre*/ }
  6. void pushdown(int x)
  7. {
  8. if (lazy[x])
  9. {
  10. if (ch[x][0]) rev(ch[x][0]);
  11. if (ch[x][1]) rev(ch[x][1]);
  12. lazy[x] = 0;
  13. }
  14. }
  15. void rotate(int x)
  16. {
  17. int y = fa[x], z = fa[y], k = ch[y][1] == x, w = ch[x][k ^ 1];
  18. if (nroot(y)) { ch[z][ch[z][1] == y] = x; } ch[x][k ^ 1] = y, ch[y][k] = w;
  19. if (w) { fa[w] = y; } fa[y] = x; fa[x] = z; pushup(y), pushup(x);
  20. }
  21. void splay(int x)
  22. {
  23. int y = x, top = 0;
  24. st[++top] = y;
  25. while (nroot(y)) st[++top] = y = fa[y];
  26. while (top > 0) pushdown(st[top--]);
  27. while (nroot(x))
  28. {
  29. int y = fa[x], z = fa[y];
  30. if (nroot(y)) rotate((ch[y][1] == x) ^ (ch[z][1] == y) ? x : y);
  31. rotate(x);
  32. }
  33. pushup(x);
  34. }
  35. void access(int x)
  36. {
  37. for (int y = 0; x > 0; x = fa[y = x])
  38. splay(x), ch[x][1] = y, pushup(x);
  39. }
  40. void makert(int x)
  41. {
  42. access(x), splay(x), rev(x);
  43. }
  44. int findrt(int x)
  45. {
  46. access(x), splay(x);
  47. while (ch[x][0]) pushdown(x), x = ch[x][0];
  48. return x;
  49. }
  50. void link(int x, int y)
  51. {
  52. makert(x);
  53. if (findrt(y) != x) fa[x] = y;
  54. }
  55. void cut(int x, int y)
  56. {
  57. makert(x);
  58. if (findrt(y) == x && fa[x] == y && ch[x][1] == 0)
  59. ch[y][0] = fa[x] = 0, pushup(y);
  60. }

upd:压行Link-Cut Tree模板

  1. bool nroot(int x) { return ch[fa[x]][0] == x || ch[fa[x]][1] == x; }
  2. void rev(int x) { swap(ch[x][0], ch[x][1]), lazy[x] ^= 1; }
  3. void pushup(int x) { /*维护一个pre*/ }
  4. 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; } }
  5. void rotate(int x)
  6. {
  7. int y = fa[x], z = fa[y], k = ch[y][1] == x, w = ch[x][k ^ 1];
  8. if (nroot(y)) { ch[z][ch[z][1] == y] = x; } ch[x][k ^ 1] = y, ch[y][k] = w;
  9. if (w) { fa[w] = y; } fa[y] = x; fa[x] = z; pushup(y), pushup(x);
  10. }
  11. void splay(int x)
  12. {
  13. int y = x, top = 0; st[++top] = y; while (nroot(y)) { st[++top] = y = fa[y]; } while (top > 0) { pushdown(st[top--]); }
  14. 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); }
  15. }
  16. void access(int x) { for (int y = 0; x > 0; x = fa[y = x]) splay(x), ch[x][1] = y, pushup(x); }
  17. void makert(int x) { access(x), splay(x), rev(x); }
  18. int findrt(int x) { access(x), splay(x); while (ch[x][0]) { pushdown(x), x = ch[x][0]; } return x; }
  19. void link(int x, int y) { makert(x); if (findrt(y) != x) fa[x] = y; }
  20. 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. el表达式动态拼接变量_c:set的用法

    转自:https://blog.csdn.net/xb12369/article/details/39581955如 何在${}中使用${},例:${user.name_${user.id}},use ...

  2. BurpSuite—-Spider模块(蜘蛛爬行)

    一.简介 Burp Spider 是一个映射 web 应用程序的工具.它使用多种智能技术对一个应用程序的内容和功能进行全面的清查. Burp Spider 通过跟踪 HTML 和 JavaScript ...

  3. MSDE2000

    安装MSDE2000的时候,遇到的两个问题 sqlserver 小版本 SQL安装问题.系统说:为了安全,要求使用SA密码,请使用SAPWD开关提供同一密码

  4. Betsy's Tour 漫游小镇(dfs)

    Description 一个正方形的镇区分为 N2 个小方块(1 <= N <= 7).农场位于方格的左上角,集市位于左下角.贝茜穿过小镇,从左上角走到左下角,刚好经过每个方格一次.当 N ...

  5. (转)Mac下MySql安装经历(含安装错误排查、卸载多种折腾)

    在安装mysql的时候,活活折腾我两天.结果终于被我折腾成功了……一开始我就放了个错误:我下了32位版本的mysql:mysql-5.5.8-osx10.6-x86.dmg 须知在mac下装的是64位 ...

  6. 【总结整理】如何判断伪需求(摘自pmcafe)

    1.客户不会直接提需求,都是给解决方案,所以得到用户的反馈之后,先反推一下是很必要的,为什么客户会有这样的方案 总结:方案不合适 例如:客户只会说我要快马,反推一下,其实客户是想要更快,这样的话,解决 ...

  7. SpringBoot15 sell02 订单模块

    1 订单模块 1.1 MySQL数据表 订单模块涉及到两个数据表: 订单表:主要存储订单相关的基本信息 DROP TABLE IF EXISTS `order_master`; CREATE TABL ...

  8. Android Studio 编译提示 No installed build tools found. Please install the Android build tools

    添加 ANDROID_HOME=D:\Android\adt-bundle-windows\sdk 系统变量即可

  9. win10 Kinect2 Visualstudio2015 opencv3环境搭建

    1.下载kinect SDK ( Kinect for Windows SDK 2.0 ):  https://www.microsoft.com/en-us/download/details.asp ...

  10. SqlServer------范式小结

    说明:大多数初学者对于关系数据库中的范式很是头疼,我本人也是,所以今天又看了视频,总结了一下内容,尽量语言通俗易懂,少用专业术语以及概念. 首先要理解几个键值. 超键:在关系模式中,能唯一标识元组的属 ...