[SDOI2011]染色

题面描述

滑稽

思路

初看不可做,再看不可做。

之后过了几天之后,才想起来有这道题。

铁头娃 LCT ⁡ \operatorname{LCT} LCT来了。

由于是统计不同的颜色段数,那么一个颜色段与另一颜色段接壤的地方,也就是左端点的 col ⁡ \operatorname{col} col,以及右端点的 col ⁡ \operatorname{col} col

转化成 LCT ⁡ \operatorname{LCT} LCT也就是 t[rc].rcol ⁡ \operatorname{t[rc].rcol} t[rc].rcol t[lc].lcol ⁡ \operatorname{t[lc].lcol} t[lc].lcol,之后拓展时,更新状态即可。

注意,若 t[rc].lcol ⁡ = t[p].col ⁡ \operatorname{t[rc].lcol}=\operatorname{t[p].col} t[rc].lcol=t[p].col,相应区间数需要减一, t[lc].rcol ⁡ \operatorname{t[lc].rcol} t[lc].rcol同理。

AC code

#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<cstring>
#define gc getchar()
using namespace std;
const int N=1e5+10;
inline void qr(int &x)
{
	x=0;int f=1;char c=gc;
	while(c<'0'||c>'9'){if(c=='-')f=-1;c=gc;}
	while(c>='0'&&c<='9'){x=x*10+(c^48);c=gc;}
	x*=f;
}
void qw(int x)
{
	if(x<0)x=-x,putchar('-');
	if(x/10)qw(x/10);
	putchar(x%10+48);
}
struct node{int f,son[2],lcol,rcol,s,col,lz;bool rv;}t[N];
inline bool nroot(int p){return t[t[p].f].son[0]==p||t[t[p].f].son[1]==p;}
inline void update(int p)
{
	int lc=t[p].son[0],rc=t[p].son[1];
	t[p].lcol=lc?t[lc].lcol:t[p].col;
	t[p].rcol=rc?t[rc].rcol:t[p].col;
	if(lc&&rc)t[p].s=t[lc].s+t[rc].s+1-(t[p].col==t[lc].rcol)-(t[p].col==t[rc].lcol);
	else if(lc)t[p].s=t[lc].s+1-(t[p].col==t[lc].rcol);
	else if(rc)t[p].s=t[rc].s+1-(t[p].col==t[rc].lcol);
	else t[p].s=1;
}
void crv(int p){if(!p)return ;swap(t[p].lcol,t[p].rcol);swap(t[p].son[0],t[p].son[1]);t[p].rv^=1;}
void ccol(int p,int col){if(!p)return ;t[p].lcol=t[p].rcol=t[p].col=col;t[p].s=1;t[p].lz=col;}
void pushdown(int p)
{
	if(t[p].rv)t[p].rv=0,crv(t[p].son[0]),crv(t[p].son[1]);
	if(t[p].lz)ccol(t[p].son[0],t[p].lz),ccol(t[p].son[1],t[p].lz),t[p].lz=0;
}
void rotate(int p,int w)
{
	int f=t[p].f,gf=t[f].f;
	int r=t[p].son[w],R=f;t[R].son[w^1]=r;if(r)t[r].f=R;
	r=p;R=gf;if(nroot(f))t[R].son[t[R].son[1]==f]=r;t[r].f=R;
	r=f;R=p;t[R].son[w]=r;t[r].f=R;update(f),update(p);
}
void sdfs(int p){if(nroot(p))sdfs(t[p].f);pushdown(p);}
void splay(int p)
{
	for(sdfs(p);nroot(p);)
	{
		int f=t[p].f,gf=t[f].f;
		if(!nroot(f))rotate(p,t[f].son[0]==p);
		else
		{
			if(t[f].son[0]==p&&t[gf].son[0]==f)rotate(f,1),rotate(p,1);
			else if(t[f].son[0]==p&&t[gf].son[1]==f)rotate(p,1),rotate(p,0);
			else if(t[f].son[1]==p&&t[gf].son[0]==f)rotate(p,0),rotate(p,1);
			else rotate(f,0),rotate(p,0);
		}
	}
}
void access(int x)
{
	for(int y=0;x;x=t[y=x].f)
		splay(x),t[x].son[1]=y,update(x);
}
void makeroot(int x)
{
	access(x),splay(x),crv(x);
}
void split(int x,int y)
{
	makeroot(x);access(y);splay(y);
}
void change(int x,int y)
{
	int lz;qr(lz);
	split(x,y);ccol(y,lz);
}
void query(int x,int y)
{
	split(x,y);
	qw(t[y].s);puts("");
}
struct edge{int y,next;}a[N<<1];int len,last[N];
void ins(int x,int y){a[++len]=(edge){y,last[x]};last[x]=len;}
void dfs(int x)
{
	for(int k=last[x],y;k;k=a[k].next)
		if((y=a[k].y)!=t[x].f)
			t[y].f=x,dfs(y);
}
int main()
{
	int n,m;qr(n),qr(m);
	for(int i=1;i<=n;i++)qr(t[i].col),t[i].lcol=t[i].rcol=t[i].col,t[i].s=1;
	for(int i=1,x,y;i<n;i++)qr(x),qr(y),ins(x,y),ins(y,x);
	dfs(1);
	while(m--)
	{
		char s[3];int x,y;scanf("%s",s+1);qr(x),qr(y);
		if(s[1]=='Q')query(x,y);
		else change(x,y);
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值