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

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

https://www.luogu.org/problemnew/show/P3690

分析:

  LCT模板

代码:

注意一下cut!

  1 #include<cstdio>
  2 #include<algorithm>
  3
  4 using namespace std;
  5
  6 const int N = 300100;
  7
  8 int val[N],fa[N],ch[N][2],rev[N],sum[N],st[N],top;
  9
 10 inline char nc() {
 11     static char buf[100000],*_p1 = buf,*_p2 = buf;
 12     return _p1==_p2&&(_p2=(_p1=buf)+fread(buf,1,100000,stdin),_p1==_p2) ? EOF :*_p1++;
 13 }
 14 inline int read() {
 15     int x = 0,f = 1;char ch=nc();
 16     for (; ch<‘0‘||ch>‘9‘; ch = nc()) if (ch == ‘-‘) f = -1;
 17     for (; ch>=‘0‘&&ch<=‘9‘; ch = nc()) x = x*10+ch-‘0‘;
 18     return x * f;
 19 }
 20
 21 inline void pushup(int x) {
 22     sum[x] = sum[ch[x][1]] ^ sum[ch[x][0]] ^ val[x];
 23 }
 24 inline void pushdown(int x) {
 25     int l = ch[x][0],r = ch[x][1];
 26     if (rev[x]) {
 27         rev[l] ^= 1;rev[r] ^= 1;
 28         swap(ch[x][0],ch[x][1]);
 29         rev[x] ^= 1;
 30     }
 31 }
 32 inline bool isroot(int x) {
 33     return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;
 34 }
 35 inline int son(int x) {
 36     return ch[fa[x]][1]==x;
 37 }
 38 inline void rotate(int x) {
 39     int y = fa[x],z = fa[y],b = son(x),c = son(y),a = ch[x][!b];
 40     if (!isroot(y)) ch[z][c] = x;fa[x] = z;
 41     ch[x][!b] = y;fa[y] = x;
 42     ch[y][b] = a;if (a) fa[a] = y;
 43     pushup(y);pushup(x);
 44 }
 45 inline void splay(int x) {
 46     top = 0;st[++top] = x;
 47     for (int i=x; !isroot(i); i=fa[i]) st[++top] = fa[i];
 48     while (top) pushdown(st[top--]);
 49     while (!isroot(x)) {
 50         int y = fa[x];
 51         if (!isroot(y)) {
 52             if (son(x)==son(y)) rotate(y);
 53             else rotate(x);
 54         }
 55         rotate(x);
 56     }
 57 }
 58 inline void access(int x) {
 59     for (int last=0; x; last=x,x=fa[x]) {
 60         splay(x);ch[x][1] = last;pushup(x);
 61     }
 62 }
 63 inline void makeroot(int x) {
 64     access(x);
 65     splay(x);
 66     rev[x] ^= 1;
 67 }
 68 inline int find(int x) {
 69     access(x);splay(x);
 70     while (ch[x][0]) x = ch[x][0];
 71     return x;
 72 }
 73 inline void link(int x,int y) {
 74     makeroot(x);
 75     fa[x] = y;
 76 }
 77 inline void cut(int x,int y) {
 78     makeroot(x);access(y);splay(y);
 79     if (fa[x] == y && !ch[x][1]) ch[y][0] = fa[x] = 0;
 80 }
 81
 82 inline void update(int x,int y) {
 83     makeroot(x);val[x] = y;pushup(x);
 84 }
 85 inline int query(int x,int y) {
 86     makeroot(x);access(y);splay(y);
 87     return sum[y];
 88 }
 89
 90 int main() {
 91     int n = read(),m = read(),opt,x,y;
 92     for (int i=1; i<=n; ++i) sum[i] = val[i] = read();
 93     while (m--) {
 94         opt = read(),x = read(),y = read();
 95         if (opt==0) printf("%d\n",query(x,y));
 96         else if (opt==1) {
 97             if (find(x)!=find(y)) link(x,y);
 98         }
 99         else if (opt==2) {
100             if (find(x)==find(y)) cut(x,y);
101         }
102         else update(x,y);
103     }
104     return 0;
105 }

原文地址:https://www.cnblogs.com/mjtcn/p/9299399.html

时间: 2024-08-09 18:32:20

P3690 【模板】Link Cut Tree (动态树)的相关文章

Link Cut Tree 动态树 小结

动态树有些类似 树链剖分+并查集 的思想,是用splay维护的 lct的根是动态的,"轻重链"也是动态的,所以并没有真正的轻重链 动态树的操作核心是把你要把 修改/询问/... 等等一系列的操作的树链放到一个splay里,然后用splay根据相对深度大小来维护这个树链 lct利用了splay的神奇性质,通过"认爹不认子"来达到记录多个子树的目的 lct的核心,access函数的意义是,在从这个点到它所在联通块中 相对深度最小的点 (可以理解为子树根) 的树链上,打通

P3690 Link Cut Tree (动态树)

干脆整个LCT模板吧. 缺个链上修改和子树操作,链上修改的话join(u,v)然后把v splay到树根再打个标记就好. 至于子树操作...以后有空的话再学(咕咕咕警告) 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int N=1e5+10; 5 int n,m,a[N],Xor[N],fa[N],ch[N][2],flp[N],sta[N],tp; 6 #define l(u

[模板]Link Cut Tree

传送门 Description 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联通的. 1:后接两个整数(x,y),代表连接x到y,若x到y已经联通则无需连接. 2:后接两个整数(x,y),代表删除边(x,y),不保证边(x,y)存在. 3:后接两个整数(x,y),代表将点x上的权值变成y. Solution \(Link\ Cut\ Tree\)模板题

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

P3690 [模板]Link Cut Tree (动态树) 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联通的. 1:后接两个整数(x,y),代表连接x到y,若x到y已经联通则无需连接. 2:后接两个整数(x,y),代表删除边(x,y),不保证边(x,y)存在. 3:后接两个整数(x,y),代表将点x上的权值变成y. 输入输出

AC日记——【模板】Link Cut Tree 洛谷 P3690

[模板]Link Cut Tree 思路: LCT模板: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 300005 int n,m,val[maxn]; int top,ch[maxn][2],f[maxn],xr[maxn],q[maxn],rev[maxn]; inline void in(int &now) { int if_z=1;now=0; char Cget=getchar(); while

脑洞大开加偏执人格——可持久化treap版的Link Cut Tree

一直没有点动态树这个科技树,因为听说只能用Splay,用Treap的话多一个log.有一天脑洞大开,想到也许Treap也能从底向上Split.仔细思考了一下,发现翻转标记不好写,再仔细思考了一下,发现还是可以写的,只需要实时交换答案二元组里的两棵树,最后在吧提出来的访问节点放回去就行了.本着只学一种平衡树的想法,脑洞大开加偏执人格的开始写可持久化Treap版的Link Cut Tree... 写了才发现,常数硕大啊!!!代码超长啊!!!因为merge是从上到下,split从下到上,pushdow

Link Cut Tree学习笔记

从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子树信息 小结 动态树问题和Link Cut Tree 动态树问题是一类要求维护一个有根树森林,支持对树的分割, 合并等操作的问题. Link Cut Tree(林可砍树?简称LCT)是解决这一类问题的一种数据结构. 一些无聊的定义 Link Cut Tree维护的是动态森林中每棵树的任意链剖分. P

bzoj2049 [Sdoi2008]Cave 洞穴勘测 link cut tree入门

link cut tree入门题 首先说明本人只会写自底向上的数组版(都说了不写指针.不写自顶向下QAQ……) 突然发现link cut tree不难写... 说一下各个函数作用: bool isroot(int x):判断x是否为所在重链(splay)的根 void down(int x):下放各种标记 void rotate(int x):在x所在重链(splay)中将x旋转到fa[x]的位置上 void splay(int x):在x坐在重链(splay)中将x旋转到根 void acce

link cut tree 入门

鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. #include<cstdio> #include<cstring> #include<cctype> #include<algorithm> #include<queue> using namespace std; #define rep(i,s,