luogu P4219 [BJOI2014]大融合

背景:

L C T LCT LCT维护子树信息的模板题。

题目传送门:

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

题意:

动态加边的一棵树,求当前联通时这一条边的负载(任意两点的路径经过这条边的次数)。

思路:

动态加边,显然用 L C T LCT LCT,然而 L C T LCT LCT 能维护重链啊。
怎么办。
我们知道轻重儿子(轻重链)是不断变化的,因此,在每一次转换轻重儿子时记录一下,更改一下即可。
s i z e size size记录轻儿子所连的子树大小, S i z e Size Size记录整棵子树的大小。
那么这三个函数相应变成:

void pushup(int x)
{
	tr[x].Size=tr[tr[x].son[0]].Size+tr[tr[x].son[1]].Size+tr[x].size+1;
}
void access(int x)
{
	for(int y=0;x;y=x,x=tr[x].fa)
		splay(x),tr[x].size+=tr[tr[x].son[1]].Size,tr[x].size-=tr[tr[x].son[1]=y].Size,pushup(x);
}
void link(int x,int y)
{
	makeroot(x);
	makeroot(y);
	tr[x].fa=y;
	tr[y].size+=tr[x].Size;
	pushup(y);
}

理解起来不难,因为父亲只存重儿子,因此就有: S i z e n o w = S i z e l c ( 左 儿 子 ) + S i z e r c ( 右 儿 子 ) + s i z e n o w ( 轻 儿 子 所 连 的 子 树 大 小 ) + 1 ( 自 己 ) Size_{now}=Size_{lc}(左儿子)+Size_{rc}(右儿子)+size_{now}(轻儿子所连的子树大小)+1(自己) Sizenow=Sizelc()+Sizerc()+sizenow()+1()

a c c e s s access access操作将 x x x到根的路径变为重链,因而在 n o w now now的右儿子变为轻儿子之前, s i z e size size需要加上它,而变完后 s i z e size size就要减少变为重儿子的那一部分。

l i n k link link操作是加上一条轻链,因而改变的是 s i z e size size,由定义可知要加上 S i z e Size Size

因为 d e l del del操作之前要 s p l i t split split,将其变为重链, s i z e size size a c c e s s access access中更新,因此不受影响,不需要改。

求解操作就是一个乘法原理,没什么。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;
	struct node{int d,fa,size,Size,op,son[2];} tr[100010];
	int n,m;
bool isroot(int x)
{
	return tr[tr[x].fa].son[0]!=x&&tr[tr[x].fa].son[1]!=x;
}
bool isson(int x)
{
	return x==tr[tr[x].fa].son[1];
}
void change(int x)
{
	if(!x)return;
	swap(tr[x].son[0],tr[x].son[1]);
	tr[x].op^=1;
}
void pushup(int x)
{
	tr[x].Size=tr[tr[x].son[0]].Size+tr[tr[x].son[1]].Size+tr[x].size+1;
}
void pushdown(int x)
{
	if(!tr[x].op) return;
	change(tr[x].son[0]),change(tr[x].son[1]);
	tr[x].op=0;
}
void rot(int x)
{
	int w=isson(x),y=tr[x].fa,yy=tr[y].fa;
	tr[y].son[w]=tr[x].son[w^1];
	if(tr[y].son[w]) tr[tr[y].son[w]].fa=y;
	tr[x].fa=yy;
	if(!isroot(y)) tr[yy].son[isson(y)]=x;
	tr[x].son[w^1]=y;tr[y].fa=x;
	pushup(y);
}
int sta[300010];
int top;
void splay(int x)
{
	sta[top=1]=x;
	for(int i=x;!isroot(i);i=tr[i].fa)
		sta[++top]=tr[i].fa;
	while(top) pushdown(sta[top--]);
	for(int y=tr[x].fa;!isroot(x);rot(x),y=tr[x].fa)
		if(!isroot(y)) isson(x)^isson(y)?rot(x):rot(y);
	pushup(x);
}
void access(int x)
{
	for(int y=0;x;y=x,x=tr[x].fa)
		splay(x),tr[x].size+=tr[tr[x].son[1]].Size,tr[x].size-=tr[tr[x].son[1]=y].Size,pushup(x);
}
void makeroot(int x)
{
	access(x);splay(x);change(x);
}
void link(int x,int y)
{
	makeroot(x);
	makeroot(y);
	tr[x].fa=y;
	tr[y].size+=tr[x].Size;
	pushup(y);
}
void split(int x,int y)
{
	makeroot(x);access(y);splay(y);
}
void del(int x,int y)
{
	split(x,y);
	tr[x].fa=tr[y].son[0]=0;
}
LL work(int x,int y)
{
	del(x,y);
	makeroot(x);makeroot(y);
	LL ans=(LL)tr[x].Size*tr[y].Size;
	link(x,y);
	return ans;
}
int main()
{
	char s[5];
	int x,y;
	scanf("%d %d",&n,&m);
	tr[0]=(node){0,0,0,0,0,0,0};
	for(int i=1;i<=n;i++)
		tr[i]=(node){0,0,0,1,0,0,0};
	for(int i=1;i<=m;i++)
	{
		scanf("%s %d %d",s+1,&x,&y);
		if(s[1]=='A') link(x,y); else printf("%lld\n",work(x,y));
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值