[SDOI2011]染色

给定一棵有n(n<=100000)个节点的无根树和m个操作,操作有2类: 1、将节点a到节点b路径上所有点都染成颜色c; 2、询问节点a到节点n路径上的颜色段数量(连续相同颜色被认为是同一段),如“112221”由3段组成:“11”、“222”和“1”。 请你写一个程序依次完成这m个操作。

维护稍微麻烦一点的动态树,记得每次Splay之前Relax一下,把标记push下来

//Lib
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<ctime>

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<queue>
using namespace std;
//Macro
#define rep(i,a,b) for(int i=a,tt=b;i<=tt;++i)
#define rrep(i,a,b) for(int i=a,tt=b;i>=tt;--i)
#define erep(i,e,x) for(int i=x;i;i=e[i].next)
#define irep(i,x) for(__typedef(x.begin()) i=x.begin();i!=x.end();i++)
#define read() (strtol(ipos,&ipos,10))
#define sqr(x) ((x)*(x))
#define pb push_back
#define PS system("pause");
typedef long long ll;
typedef pair<int,int> pii;
const int oo=~0U>>1;
const double inf=1e20;
const double eps=1e-6;
string name="",in=".in",out=".out";
//Var
struct T
{
	int LC,RC,FA,COVER,COLOR,CNT,LCR,RCR;
	#define lc(x) tree[x].LC
	#define rc(x) tree[x].RC
	#define fa(x) tree[x].FA
	#define cnt(x) tree[x].CNT
	#define cover(x) tree[x].COVER
	#define color(x) tree[x].COLOR
	#define lcr(x) tree[x].LCR
	#define rcr(x) tree[x].RCR
}tree[100008];
struct E
{
	int next,node;
}e[200008];
int h[100008],w[100008];
int n,m,tot;
inline void Update(int x)
{
	if(x==0)return;
	if(!lc(x))lcr(x)=color(x);else lcr(x)=lcr(lc(x));
	if(!rc(x))rcr(x)=color(x);else rcr(x)=rcr(rc(x));
	cnt(x)=cnt(lc(x))+cnt(rc(x))+1;
	if(rcr(lc(x))==color(x))cnt(x)--;
	if(lcr(rc(x))==color(x))cnt(x)--;
}
inline void Zig(int x)
{
	int y=fa(x),z=fa(y);
	if(lc(z)==y)lc(z)=x;else if(rc(z)==y)rc(z)=x;fa(x)=z;
	fa(rc(x))=y;lc(y)=rc(x);rc(x)=y;fa(y)=x;
	Update(y);
}
inline void Zag(int x)
{
	int y=fa(x),z=fa(y);
	if(lc(z)==y)lc(z)=x;else if(rc(z)==y)rc(z)=x;fa(x)=z;
	fa(lc(x))=y;rc(y)=lc(x);lc(x)=y;fa(y)=x;
	Update(y);
}
inline bool isRoot(int x){return lc(fa(x))!=x&&rc(fa(x))!=x;}
inline void Set(int x,int y)
{
	if(x==0)return;
	color(x)=lcr(x)=rcr(x)=cover(x)=y;
	cnt(x)=1;
}
inline void Push(int x)
{
	if(x==0)return;
	if(cover(x))
	{
		Set(lc(x),cover(x));
		Set(rc(x),cover(x));
		cover(x)=0;
	}
}
void Relax(int x){if(!isRoot(x))Relax(fa(x));Push(x);}
void Splay(int x)
{
	Relax(x);
	for(int y,z;!isRoot(x);)
	{
		y=fa(x),z=fa(y);
		if(isRoot(y))
			if(lc(y)==x)Zig(x);
			else Zag(x);
		else
			if(lc(z)==y)
				if(lc(y)==x)Zig(y),Zig(x);
				else Zag(x),Zig(x);
			else
				if(rc(y)==x)Zag(y),Zag(x);
				else Zig(x),Zag(x);
	}
	Update(x);
}
inline void Expose(int x)
{
	for(int y=0;x;x=fa(x))
	{
		Splay(x);
		rc(x)=y;Update(x);y=x;
	}
}
inline void add(int a,int b){e[++tot].next=h[a];e[tot].node=b;h[a]=tot;}
void DFS(int u,int fa)
{
	color(u)=w[u];
	erep(i,e,h[u])if(e[i].node!=fa){DFS(e[i].node,u);fa(e[i].node)=u;}
}
void Init()
{
	int a,b;
	scanf("%d%d",&n,&m);
	rep(i,1,n)scanf("%d",w+i);
	rep(i,1,n-1)
	{
		scanf("%d%d",&a,&b);
		add(a,b);add(b,a);
	}
	DFS(1,0);
}
void Change(int x,int y,int c)
{
	Expose(y);
	for(y=0;x;x=fa(x))
	{
		Splay(x);
		if(!fa(x))
		{
			Set(rc(x),c);Push(rc(x));
			color(x)=c;Update(x);
			Set(y,c);Push(y);
		}
		rc(x)=y;Update(x);y=x;
	}
}
int Query(int x,int y)
{
	Expose(y);int cc;
	for(y=0;x;x=fa(x))
	{
		Splay(x);
		if(!fa(x))
		{
			cc=cnt(rc(x))+cnt(y)+1;
			if(lcr(rc(x))==color(x))cc--;
			if(lcr(y)==color(x))cc--;
			return cc;
		}
		rc(x)=y;Update(x);y=x;
	}
}
void Work()
{
	char ch[10];int a,b,c;
	rep(i,1,m)
	{
		scanf("%s",ch);
		if(ch[0]=='C')scanf("%d%d%d",&a,&b,&c),Change(a,b,c);
		else scanf("%d%d",&a,&b),printf("%d\n",Query(a,b));
	}

}
int main()
{
	//freopen((name+in).c_str(),"r",stdin);
	//freopen((name+out).c_str(),"w",stdout);
	Init();
	Work();
	return 0;
}


你可能感兴趣的:([SDOI2011]染色)