洛谷2486 【SDOI2011】染色(线段树+树链剖分)

传送门

【题目分析】

果然重构才是真理吗。。。。。

因为涉及路径操作,所以考虑树链剖分,线段树维护题意中的:

1.区间颜色段数。

2.区间左右颜色。

3.区间覆盖标记。

因为线段树的问题主要就是考虑push_up和push_down,此题push_up时记得将区间左右颜色赋值,然后合并的时候注意如果左区间右端颜色与右区间左端颜色相同的话那么颜色段数要-1,push_down时如果有区间覆盖标记,就将整段区间的颜色赋值为修改颜色,颜色段数为1,记得下传。

然后就是路径修改询问,修改常规,询问的时候因为要将一段一段的路径拼起来,所以要检查每接一段的交界处的颜色是否一样。

细节稍多(重载运算符的时候忘return了。。好窒息)

【代码~】

#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e5+10;
const int MAXM=2e5+10;

int n,q,cnt;
int a[MAXN];
int head[MAXN],depth[MAXN],fa[MAXN],son[MAXN],siz[MAXN],top[MAXN];
int nxt[MAXM],to[MAXM];
int dfn[MAXN],ys[MAXN],tot;
struct Tree{
	int l,r;
	int cnt;
	int lc,rc;
	int cov;
	friend inline Tree operator+(const Tree &a,const Tree &b){
		Tree c;
		c.l=a.l,c.r=b.r;
		c.cnt=a.cnt+b.cnt;
		if(a.rc==b.lc)
		  c.cnt--;
		c.lc=a.lc,c.rc=b.rc;
		return c;
	}
}tr[MAXN<<2];

int Read(){
	int i=0,f=1;
	char c;
	for(c=getchar();(c>'9'||c<'0')&&c!='-';c=getchar());
	if(c=='-')
	  f=-1,c=getchar();
	for(;c>='0'&&c<='9';c=getchar())
	  i=(i<<3)+(i<<1)+c-'0';
	return i*f;
}

void add(int x,int y){
	nxt[cnt]=head[x];
	head[x]=cnt;
	to[cnt]=y;
	cnt++;
}

void dfs1(int u,int f){
	siz[u]=1;
	for(int i=head[u];i!=-1;i=nxt[i]){
		int v=to[i];
		if(v==f)
		  continue;
		fa[v]=u;
		depth[v]=depth[u]+1;
		dfs1(v,u);
		siz[u]+=siz[v];
		if(siz[v]>siz[son[u]])
		  son[u]=v;
	}
}

void dfs2(int u,int tp){
	top[u]=tp;
	dfn[u]=++tot;
	ys[tot]=u;
	if(!son[u])
	  return ;
	dfs2(son[u],tp);
	for(int i=head[u];i!=-1;i=nxt[i]){
		int v=to[i];
		if(v==fa[u]||v==son[u])
		  continue;
		dfs2(v,v);
	}
}

void push_up(int root){
	tr[root].cnt=tr[root<<1].cnt+tr[root<<1|1].cnt;
	if(tr[root<<1].rc==tr[root<<1|1].lc)
	  tr[root].cnt--;
	tr[root].lc=tr[root<<1].lc;
	tr[root].rc=tr[root<<1|1].rc;
}

void push_now(int root,int col){
	tr[root].cnt=1;
	tr[root].lc=tr[root].rc=col;
	tr[root].cov=col;
}

void push_down(int root){
	if(tr[root].cov!=-1){
		push_now(root<<1,tr[root].cov);
		push_now(root<<1|1,tr[root].cov);
		tr[root].cov=-1;
	}
}

void build(int root,int l,int r){
	tr[root].l=l,tr[root].r=r;
	tr[root].cov=-1;
	if(l==r){
		tr[root].cnt=1;
		tr[root].lc=tr[root].rc=a[ys[l]];
		return ;
	}
	int mid=l+r>>1;
	build(root<<1,l,mid);
	build(root<<1|1,mid+1,r);
	push_up(root);
}

void update(int root,int l,int r,int L,int R,int key){
	if(l>R||r<L)
	  return ;
	if(L<=l&&r<=R){
		push_now(root,key);
		return ;
	}
	push_down(root);
	int mid=l+r>>1;
	if(R<=mid)
	  update(root<<1,l,mid,L,R,key);
	else{
		if(L>mid)
		  update(root<<1|1,mid+1,r,L,R,key);
		else
		  update(root<<1,l,mid,L,mid,key),update(root<<1|1,mid+1,r,mid+1,R,key);
	}
	push_up(root);
}

Tree query(int root,int l,int r,int L,int R){
	if(L<=l&&r<=R){
		return tr[root];
	}
	push_down(root);
	int mid=l+r>>1;
	if(R<=mid)
	  return query(root<<1,l,mid,L,R);
	else{
		if(L>mid)
		  return query(root<<1|1,mid+1,r,L,R);
		else{
			Tree tr1=query(root<<1,l,mid,L,mid);
			Tree tr2=query(root<<1|1,mid+1,r,mid+1,R);
			return tr1+tr2;
		}
	}
}

void update_path(int x,int y,int key){
	while(top[x]!=top[y]){
		if(depth[top[x]]<depth[top[y]])
		  swap(x,y);
		update(1,1,n,dfn[top[x]],dfn[x],key);
		x=fa[top[x]];
	}
	if(depth[x]<depth[y])
	  swap(x,y);
	update(1,1,n,dfn[y],dfn[x],key);
}

int query_path(int x,int y){
	int ret=0,lc=0,rc=0;
	Tree ans;
	while(top[x]!=top[y]){
		if(depth[top[x]]<depth[top[y]])
		  swap(x,y),swap(lc,rc);
		ans=query(1,1,n,dfn[top[x]],dfn[x]);
		ret+=ans.cnt;
		if(ans.rc==lc)
		  ret--;
		lc=ans.lc;
		x=fa[top[x]];
	}
	if(depth[x]<depth[y])
	 swap(x,y),swap(lc,rc);
	ans=query(1,1,n,dfn[y],dfn[x]);
	ret+=ans.cnt;
	if(ans.rc==lc)
	  ret--;
	if(ans.lc==rc)
	  ret--;
	return ret;
}

int main(){
	memset(head,-1,sizeof(head));
	n=Read(),q=Read();
	for(int i=1;i<=n;++i){
		a[i]=Read();
	}
	for(int i=1;i<n;++i){
		int x=Read(),y=Read();
		add(x,y);
		add(y,x);
	}
	dfs1(1,-1);
	dfs2(1,1);
	build(1,1,n);
	while(q--){
		char cz[5];
		scanf("%s",cz);
		int l=Read(),r=Read();
		if(cz[0]=='C'){
			int k=Read();
			update_path(l,r,k);
		}
		else
		  cout<<query_path(l,r)<<'\n';
	}
	return 0;
}

 

posted @ 2018-12-30 15:25  Ishtar~  阅读(199)  评论(0编辑  收藏  举报