[SDOI2011]染色

題面描述

滑稽

思路

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

之後過了幾天之後,纔想起來有這道題。

鐵頭娃LCT\operatorname{LCT}來了。

由於是統計不同的顏色段數,那麼一個顏色段與另一顏色段接壤的地方,也就是左端點的col\operatorname{col},以及右端點的col\operatorname{col}

轉化成LCT\operatorname{LCT}也就是t[rc].rcol\operatorname{t[rc].rcol}t[lc].lcol\operatorname{t[lc].lcol},之後拓展時,更新狀態即可。

注意,若t[rc].lcol=t[p].col\operatorname{t[rc].lcol}=\operatorname{t[p].col},相應區間數需要減一,t[lc].rcol\operatorname{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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章