[BJOI2014]大融合

XI.[BJOI2014]大融合

终于来了……我们终于要用LCT来维护子树信息了。

因为我们看到,LCT是通过将原树拆成一堆链而起效的。在树链剖分中,我们通过dfs序来访问一棵子树;但是因为LCT的链是动态变化的,因此并没有一组固定的访问顺序。

那怎么办呢?

我们考虑最原始的想法:对于每个节点,再额外维护所有虚子节点的状态。比如说,本题我们维护的就是虚子节点的子树大小之和。

我们设 t[x].s1 表示一个节点所有子节点(不管虚实)的子树大小之和,即在原树中,它自己的子树大小。再设 t[x].s2 为所有虚子节点的大小之和。

那么,pushup 函数将会长成这样:

void pushup(int x){
	t[x].s1=t[x].s2+t[lson].s1+t[rson].s1+1;
}

我们再考虑其它函数会有什么变化。显然,只有当节点间的虚实关系(边由实转序、连边、断边等)发生变化时,s2 才会发生变化。

splay 函数:在同一棵实splay中操作,没有虚实关系变化。

access 函数:有变化!

我们拎出来 access 函数:

void access(int x){
	for(int y=0;x;x=t[y=x].fa)splay(x),rson=y,pushup(x);
}

可以看到,我们有 rson=y ,虚实关系产生了变化!!!

对于\(rson\),这是实转虚,s2 应该加上\(rson\)s1 ;对于\(y\),这是虚转实,s2 应该减去\(y\)s1

因此我们最终有:

void access(int x){
	for(int y=0;x;x=t[y=x].fa)splay(x),t[x].s2+=t[rson].s1-t[y].s1,rson=y,pushup(x);
}

makerootfindrootsplit 函数:并没有虚实变化(变化全在函数内调用的 access 函数,已经在 access 时改过了),没有变化。

link :情况有变!我们这时必须要将\(y\)s2 加上\(x\)s1 ,因为\(x\)成为了\(y\)的虚儿子!

但是,\(y\)一变,\(y\)的父亲也要跟着变,\(y\)的爷爷也是……

我们有办法。直接将\(y\) access 后移到\(root\)(注意是splay的\(root\),不是整棵树的\(ROOT\)),这样\(y\)就没有父亲了!

因此我们的 link 函数就变成了这样:

void link(int x,int y){
	split(x,y);
	t[y].s2+=t[x].s1;
	t[x].fa=y;
    pushup(y);
}

等等,哪来的 split

偷懒一下,我们只是把 makeroot(x),access(y),splay(y) 三合一。实际上\(x\)\(y\)并不连通。

至于 cut ,这题并不需要。不过代码还是放出来:

void cut(int x,int y){
	split(x,y);
  	t[x].fa=t[y].ch[0]=0;
	pushup(y);
}

然后,当我们要求出一个节点\(x\)的子树大小时:

int query(int x,int y){
	split(x,y);
	return t[x].s1;
}

其中\(y\)就是指定的\(x\)的父亲(不然不知道是哪颗子树),在这题中就是给定询问的边的另一端点。

完整版放出:

#include<bits/stdc++.h>
using namespace std;
#define lson t[x].ch[0]
#define rson t[x].ch[1]
int n,m;
struct node{
	int ch[2],s1,s2,fa;
	bool rev;
}t[100100];
int identify(int x){
	if(t[t[x].fa].ch[0]==x)return 0;
	if(t[t[x].fa].ch[1]==x)return 1;
	return -1;
}
void pushup(int x){
	t[x].s1=t[x].s2+t[lson].s1+t[rson].s1+1;
}
void REV(int x){
	t[x].rev^=1,swap(lson,rson);
}
void pushdown(int x){
	if(!t[x].rev)return;
	if(lson)REV(lson);
	if(rson)REV(rson);
	t[x].rev=0;
}
void rotate(int x){
	int y=t[x].fa;
	int z=t[y].fa;
	int dirx=identify(x);
	int diry=identify(y);
	int b=t[x].ch[!dirx];
	if(diry!=-1)t[z].ch[diry]=x;t[x].fa=z;
	if(b)t[b].fa=y;t[y].ch[dirx]=b;
	t[x].ch[!dirx]=y,t[y].fa=x;
	pushup(y),pushup(x);
}
void pushall(int x){
	if(identify(x)!=-1)pushall(t[x].fa);
	pushdown(x);
}
void splay(int x){
	pushall(x);
	while(identify(x)!=-1){
		int fa=t[x].fa;
		if(identify(fa)==-1)rotate(x);
		else if(identify(fa)==identify(x))rotate(fa),rotate(x);
		else rotate(x),rotate(x);
	}
}
void access(int x){
	for(int y=0;x;x=t[y=x].fa)splay(x),t[x].s2+=t[rson].s1-t[y].s1,rson=y,pushup(x);
}
void makeroot(int x){
	access(x),splay(x),REV(x);
}
int findroot(int x){
	access(x),splay(x);
	pushdown(x);
	while(lson)x=lson,pushdown(x);
	splay(x);
	return x;
}
void split(int x,int y){
	makeroot(x),access(y),splay(y);
}
void link(int x,int y){
	split(x,y);
	t[y].s2+=t[x].s1;
	t[x].fa=y;
}
int query(int x,int y){
	split(x,y);
	return t[x].s1;
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1,x,y;i<=m;i++){
		char s[10];
		scanf("%s%d%d",s,&x,&y);
		if(s[0]=='A')link(x,y);
		else printf("%lld\n",1ll*query(x,y)*query(y,x));
	}
	return 0;
}

原文地址:https://www.cnblogs.com/Troverld/p/14602002.html