BZOJ 2631 tree 动态树(Link-Cut-Tree)

题目大意:维护一种树形数据结构。支持下面操作:

1.树上两点之间的点权值+k。

2.删除一条边。添加一条边,保证加边之后还是一棵树。

3.树上两点之间点权值*k。

4.询问树上两点时间点的权值和。

思路:利用动态树维护这棵树,lct的裸题。假设不会下传标记的,先去做BZOJ1798,也是这种标记,仅仅只是在线段树上做,比这个要简单很多。

这个也是我的LCT的第一题,理解起来十分困难啊。。。

CODE:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 100010
#define MO 51061
using namespace std;

struct Complex{
	int size;
	unsigned int val,sum;
	Complex *son[2],*father;
	bool reverse;
	int m_mark,p_mark;

	bool Check() {
		return father->son[1] == this;
	}
	void Reverse();
	void Plus(unsigned int c);
	void Mulitiply(unsigned int c);
	void PushUp();
	void PushDown();
}*tree[MAX],*nil = new Complex();

int cnt,points,asks;
int head[MAX],total;
int next[MAX << 1],aim[MAX << 1];

char c[10];

void Pretreatment();

inline Complex *NewComplex(unsigned int val);

inline void Add(int x,int y);
void DFS(int x,int last);

inline void Splay(Complex *a);
inline void Rotate(Complex *a,bool dir);
inline void PushPath(Complex *a); 

inline void Access(Complex *a);
inline void ToRoot(Complex *a);
inline void Link(Complex *x,Complex *y);
inline void Cut(Complex *x,Complex *y);

int main()
{
	Pretreatment();
	cin >> points >> asks;
	for(int x,y,i = 1;i < points; ++i) {
		scanf("%d%d",&x,&y);
		Add(x,y),Add(y,x);
	}
	for(int i = 1;i <= points; ++i)
		tree[i] = NewComplex(1);
	DFS(1,-1);
	for(int x,y,z,i = 1;i <= asks; ++i) {
		scanf("%s%d%d",c,&x,&y);
		if(c[0] == ‘+‘) {
			scanf("%d",&z);
			ToRoot(tree[x]);
			Access(tree[y]);
			Splay(tree[y]);
			tree[y]->Plus(z);
		}
		else if(c[0] == ‘-‘) {
			Cut(tree[x],tree[y]);
			scanf("%d%d",&x,&y);
			Link(tree[x],tree[y]);
		}
		else if(c[0] == ‘*‘) {
			scanf("%d",&z);
			ToRoot(tree[x]);
			Access(tree[y]);
			Splay(tree[y]);
			tree[y]->Mulitiply(z);
		}
		else {
			ToRoot(tree[x]);
			Access(tree[y]);
			Splay(tree[y]);
			printf("%d\n",tree[y]->sum);
		}
	}
	return 0;
}

void Complex:: PushUp()
{
	sum = (son[0]->sum + son[1]->sum + val) % MO;
	size = son[0]->size + son[1]->size + 1;
}

void Complex:: PushDown()
{
	if(m_mark != 1) {
		son[0]->Mulitiply(m_mark);
		son[1]->Mulitiply(m_mark);
		m_mark = 1;
	}
	if(p_mark) {
		son[0]->Plus(p_mark);
		son[1]->Plus(p_mark);
		p_mark = 0;
	}
	if(reverse) {
		son[0]->Reverse();
		son[1]->Reverse();
		reverse = false;
	}
}

void Complex:: Reverse()
{
	reverse ^= 1;
	swap(son[0],son[1]);
}

void Complex:: Plus(unsigned int c)
{
	if(this == nil)	return ;
	val = (val + c) % MO;
	p_mark = (p_mark + c) % MO;
	sum = (sum + (size * c) % MO) % MO;
}

void Complex:: Mulitiply(unsigned int c)
{
	if(this == nil)	return ;
	m_mark = (m_mark * c) % MO;
	val = (val * c) % MO;
	p_mark = (p_mark * c) % MO;
	sum = (sum * c) % MO;
}

inline void Add(int x,int y)
{
	next[++total] = head[x];
	aim[total] = y;
	head[x] = total;
}

void Pretreatment()
{
	nil->size = 0;
	nil->son[0] = nil->son[1] = nil->father = nil;
}

inline Complex *NewComplex(unsigned int val)
{
	Complex *re = new Complex();
	re->val = re->sum = val;
	re->reverse = false;
	re->son[0] = re->son[1] = re->father = nil;
	re->p_mark = 0;
	re->m_mark = 1;
	re->size = 1;
	return re;
}

void DFS(int x,int last)
{
	for(int i = head[x];i;i = next[i]) {
		if(aim[i] == last)	continue;
		tree[aim[i]]->father = tree[x];
		DFS(aim[i],x);
	}
}

inline void Splay(Complex *a)
{
	PushPath(a);
	while(a == a->father->son[0] || a == a->father->son[1]) {
		Complex *p = a->father->father;
		if(p->son[0] != a->father && p->son[1] != a->father)
			Rotate(a,!a->Check());
		else if(!a->father->Check()) {
			if(!a->Check())
				Rotate(a->father,true),Rotate(a,true);
			else	Rotate(a,false),Rotate(a,true);
		}
		else {
			if(a->Check())
				Rotate(a->father,false),Rotate(a,false);
			else	Rotate(a,true),Rotate(a,false);
		}
	}
	a->PushUp();
}

inline void Rotate(Complex *a,bool dir)
{
	Complex *f = a->father;
	f->son[!dir] = a->son[dir];
	f->son[!dir]->father = f;
	a->son[dir] = f;
	a->father = f->father;
	if(f->father->son[0] == f || f->father->son[1] == f)
		f->father->son[f->Check()] = a;
	f->father = a;
	f->PushUp();
}

inline void PushPath(Complex *a)
{
	static Complex *stack[MAX];
	int top = 0;
	for(;a->father->son[0] == a || a->father->son[1] == a;a = a->father)
		stack[++top] = a;
	stack[++top] = a;
	while(top)
		stack[top--]->PushDown();
}

inline void Access(Complex *a)
{
	Complex *last = nil;
	while(a != nil) {
		Splay(a);
		a->son[1] = last;
		a->PushUp();
		last = a;
		a = a->father;
	}
}

inline void ToRoot(Complex *a)
{
	Access(a);
	Splay(a);
	a->Reverse();
}

inline void Link(Complex *x,Complex *y)
{
	ToRoot(x);
	x->father = y;
}

inline void Cut(Complex *x,Complex *y)
{
	ToRoot(x);
	Access(y);
	Splay(y);
	y->son[0]->father = nil;
	y->son[0] = nil;
	y->PushUp();
}
时间: 2024-10-05 04:39:08

BZOJ 2631 tree 动态树(Link-Cut-Tree)的相关文章

SPOJ 题目913QTREE2 - Query on a tree II(Link Cut Tree 查询路径第k个点)

QTREE2 - Query on a tree II no tags You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. Each edge has an integer value assigned to it, representing its length. We will ask you to perfrom some i

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. 输入输出

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;

动态树之link-cut tree

说好的专题... lct的一些概念看论文 杨哲<QTREE解法的一些研究> 简单易懂. 首先不要把lct想象得很难,其实很水的.lct就是很多splay树维护的树... lct的access操作就是在原树中拓展一条点到根的类二叉树出来(用splay来维护) 这里,splay树是按深度作为关键字的,当然,在无向图中(无环)可以任意指定一个点为根,这点要切记(因为在这里操作时,有些操作需要换根,所以一定要理解) link操作就是将点作为这颗类二叉树的根,然后合并 cut操作就是将点作为这颗二叉树的

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

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

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

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,

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

HDOJ 题目3966 Aragorn&#39;s Story(Link Cut Tree成段加减点权,查询点权)

Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5505    Accepted Submission(s): 1441 Problem Description Our protagonist is the handsome human prince Aragorn comes from The Lor

Link Cut Tree(无图慎入)

类似树链剖分(其实直接记住就可以了),提前放代码 1 #include<cstdio> 2 #include<cstdlib> 3 #include<iostream> 4 #include<algorithm> 5 #include<cstring> 6 #include<climits> 7 #include<cmath> 8 #define N (int)(3e5+5) 9 using namespace std;