【BZOJ 3282】Tree Link Cut Tree模板题

知道了为什么要换根(changeroot),access后为什么有时要splay,以及LCT的其他操作,算是比较全面的啦吧,,,

现在才知道这些,,,真心弱,,,

#include<cstdio>
#include<algorithm>
#define read(x) x=getint()
using namespace std;
const int N=300003;
inline int getint(){char c;int ret=0;for(c=getchar();c<‘0‘||c>‘9‘;c=getchar());for(;c>=‘0‘&&c<=‘9‘;c=getchar())ret=ret*10+c-‘0‘;return ret;}
struct node *null;
struct node{
	node();
	node *fa,*ch[2];
	int d,rev,w;
	void push() {if(rev) {ch[0]->rev^=1; ch[1]->rev^=1; rev=0; swap(ch[0],ch[1]);}}
	void count() {w=ch[0]->w^ch[1]->w^d;}
	void setc(node *r,bool c) {ch[c]=r; r->fa=this;}
	bool check() {return fa==null||((fa->ch[0]!=this)&&(fa->ch[1]!=this));}
	bool pl() {return fa->ch[1]==this;}
}*T[N];
node::node() {d=rev=w=0; fa=ch[0]=ch[1]=null;}
int n;
inline void Build() {null=new node; *null=node();}
inline void rotate(node *r){
	node *f=r->fa;
	bool c=r->pl();
	if (!f->check()) f->fa->setc(r,f->pl());
	else r->fa=f->fa;
	f->setc(r->ch[!c],c); r->setc(f,!c);
	f->count();
}
inline void update(node *r) {if (!r->check()) update(r->fa); r->push();}
inline void splay(node *r){
	update(r);
	for(;!r->check();rotate(r))
		if (!r->fa->check()) rotate(r->fa->pl()==r->pl()?r->fa:r);
	r->count();
}
inline node *access(node *r){
	node *y=null;
	for(;r!=null;y=r,r=r->fa){
		splay(r);
		r->setc(y,1);
		r->count();
	}
	return y;
}
inline void changeroot(node *r) {access(r)->rev^=1; splay(r);}
inline void cut(node *r,node *y) {changeroot(r); access(y); splay(y); y->ch[0]->fa=null; y->ch[0]=null;}
inline void link(node *r,node *y) {changeroot(r); r->fa=y;}
inline node *findroot(node *r) {access(r); splay(r); while(r->ch[0]!=null) r=r->ch[0]; return r;}
int main(){
	Build();
	read(n); int x,q,a,b; read(q);
	for(int i=1;i<=n;i++) {T[i]=new node; read(T[i]->d); T[i]->w=T[i]->d;}
	while (q--){
		read(x); read(a); read(b);
		switch (x){
			case 0:
				changeroot(T[a]); access(T[b]); splay(T[b]); printf("%d\n",T[b]->w);
			break;
			case 1:
				if (findroot(T[a])!=findroot(T[b])) link(T[a],T[b]);
			break;
			case 2:
				if (findroot(T[a])==findroot(T[b])) cut(T[a],T[b]);
			break;
			case 3:
				changeroot(T[a]); T[a]->d=b; T[a]->count();
			break;
		}
	}
	return 0;
}

这样就行啦

时间: 2024-08-02 10:00:52

【BZOJ 3282】Tree 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. 输入输出

[模板]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\)模板题

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

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;

F - Link/Cut Tree CodeForces - 614A(水题)

Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, which is based on Splay trees. Specifically, he is now studying the expose procedure. Unfortunately, Rostislav is unable to understand the definition of this procedure

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,

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

Codeforces Round #339 (Div. 2) A. Link/Cut Tree

A. Link/Cut Tree Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, which is based on Splay trees. Specifically, he is now studying the expose procedure. Unfortunately, Rostislav is unable to understand the definition