SP12005 题解(LCT)

2 篇文章 0 订阅
2 篇文章 0 订阅

LCT动态树

这是一道树链剖分的模板题, 但模板题肯定不能用模板来做呀( doge

所以这是一篇 LCT 的题解。

LCT 板子也没什么好说的,唯一注意的就是这道题维护的是边权。

那么在连接 u u u v v v 时,就需要 多插入一个边权节点 $ n + i $ 来维护边权,还有 在 更新 s i z e size size 和 区间加的时候,需要判断此节点是否为边节点 ,不然就多加了。

$ update $要注意下

void update(int x){
	a[x].sum = a[a[x].son[0]].sum + a[a[x].son[1]].sum + a[x].val;
	a[x].siz = a[a[x].son[0]].siz + a[a[x].son[1]].siz + (x > n ? 1 : 0);
}

还有 p u s h a d d pushadd pushadd 要判断

void pushadd(int x, int add){
	if(x > n) {
		a[x].val += add;
		a[x].sum += a[x].siz * add;		
	}
	a[x].add += add;
}

输入的时候要连边

for(int i = 1; i < n; ++i){
	int u, v;
	scanf("%lld%lld", &u, &v);
	a[n + i].val = a[n + i].sum = a[n + i].add = 0, a[n + i].siz = 1;
	link(u, n + i);//
	link(n + i, v);//
}

完整 c o d e code code

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 5;
struct lct{
	int son[2], fa, sum, r, val, add, siz;
}a[N << 1];
int n, m, top, st[N << 1];
bool isroot(int x){
	return !(a[a[x].fa].son[0] == x || a[a[x].fa].son[1] == x);
}
void update(int x){
	a[x].sum = a[a[x].son[0]].sum + a[a[x].son[1]].sum + a[x].val;
	a[x].siz = a[a[x].son[0]].siz + a[a[x].son[1]].siz + (x > n ? 1 : 0);
}
void pushr(int x){
	swap(a[x].son[0], a[x].son[1]);
	a[x].r ^= 1;
}
void pushadd(int x, int add){
	if(x > n) {
		a[x].val += add;
		a[x].sum += a[x].siz * add;		
	}
	a[x].add += add;
}
void pushdown(int x){
	if(a[x].r){
		if(a[x].son[0]) pushr(a[x].son[0]);
		if(a[x].son[1]) pushr(a[x].son[1]);
		a[x].r = 0;
	}
	if(a[x].add){
		if(a[x].son[0]) pushadd(a[x].son[0], a[x].add);
		if(a[x].son[1]) pushadd(a[x].son[1], a[x].add);
		a[x].add = 0;
	}
}
void rotate(int x){
	int y = a[x].fa, z = a[y].fa, k = a[y].son[1] == x;
	if(!isroot(y)) a[z].son[a[z].son[1] == y] = x;
	a[x].fa = z;
	a[y].son[k] = a[x].son[k ^ 1], a[a[x].son[k ^ 1]].fa = y;
	a[x].son[k ^ 1] = y, a[y].fa = x;
	update(y), update(x);
}
void splay(int x){
	int t = x;
	st[++top] = t;
	while(!isroot(t)) st[++top] = a[t].fa, t = a[t].fa;
	while(top) pushdown(st[top--]);
	while(!isroot(x)){
		int y = a[x].fa, z = a[y].fa;
		if(!isroot(y)) rotate((a[z].son[1] == y) ^ (a[y].son[1] == x) ? x : y);
		rotate(x);
	}
	update(x);
}
void access(int x){
	for(int son = 0; x; x = a[son = x].fa){
		splay(x), a[x].son[1] = son;
		update(x);
	}
}
void makeroot(int x){
	access(x), splay(x), pushr(x);
}
int findroot(int x){
	access(x), splay(x);
	while(a[x].son[0]) pushdown(x), x = a[x].son[0];
	splay(x);
	return x;
}
void split(int x, int y){
	makeroot(x), access(y), splay(y);
}
void link(int x, int y){
	makeroot(x);
	if(findroot(y) != x) a[x].fa = y;
}
//void dfs(int x){
//	
//}
signed main(){
	scanf("%lld%lld", &n, &m);
	for(int i = 1; i < n; ++i){
		int u, v;
		scanf("%lld%lld", &u, &v);
		a[n + i].val = a[n + i].sum = a[n + i].add = 0, a[n + i].siz = 1;
		link(u, n + i);
		link(n + i, v);
	}
	while(m--){
		char c[2];
		int x, y;
		scanf("%s%lld%lld", c, &x, &y);
		if(c[0] == 'P'){
			split(x, y);
			pushadd(y, 1);
		}
		if(c[0] == 'Q'){
			split(x, y);
			printf("%lld\n", a[y].sum);
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值