跳到正文

个人记录

线段树染色

gouyiming200802约 7 分钟
原文
文章信息与版本

文章信息

发布
2021年10月13日
收录
2026年11月19日
更新
2026年11月19日
篇幅
约 7 分钟

版本

  1. v1当前

题目描述

给定一棵 n 个节点的无根树,共有 m 个操作,操作分为两种:

  1. 将节点 a 到节点 b 的路径上的所有点(包括 a 和 b)都染成颜色 c。

  2. 询问节点 a 到节点 b 的路径上的颜色段数量。

颜色段的定义是极长的连续相同颜色被认为是一段。例如 112221 由三段组成:11、222、1。

输入

输入的第一行是用空格隔开的两个整数,分别代表树的节点个数 n 和操作个数 m。

第二行有 n 个用空格隔开的整数,第 i个整数 wi 代表结点 i 的初始颜色。

第 3 到第 (n+1) 行,每行两个用空格隔开的整数 u,v代表树上存在一条连结节点 u 和节点 v 的边。

第 (n+2)到第 (n+m+1)行,每行描述一个操作,其格式为:

每行首先有一个字符op,代表本次操作的类型。

· 若op 为 C,则代表本次操作是一次染色操作,在一个空格后有三个用空格隔开的整数 a,b,c,代表将 a 到 b 的路径上所有点都染成颜色 c。

· 若 op为 Q,则代表本次操作是一次查询操作,在一个空格后有两个用空格隔开的整数 a,b,表示查询 a 到 b 路径上的颜色段数量。

输出

对于每次查询操作,输出一行一个整数代表答案。

样例输入

6 5

2 2 1 2 1 1

1 2

1 3

2 4

2 5

2 6

Q 3 5

C 2 1 1

Q 3 5

C 5 1 2

Q 3 5

样例输出

3

1

2

数据规模

对于 100% 的数据,1≤n,m≤10^5,1≤wi,c≤10^9,1≤a,b,u,v≤n,

op 一定为 C 或 Q,保证给出的图是一棵树。

题解

典型的板题,主要是ask_sum函数比较恶心,记住xc每次记录的是之前

一个的L,所以需要和R进行匹配,顶点时,xc在上,yc在下,所以ans-

=(xc==L)+(yc==R),150行的码,大家加油,慢慢磨吧。

C++
#include<bits/stdc++.h>
#define MAXN 100006
using namespace std;
char s[6];
int a,b,c,L,R,tempr;
int n,m,ans,w[MAXN];
int tot,to[MAXN<<1],head[MAXN<<1],nxt[MAXN<<1];
int cnt,seg[MAXN],rev[MAXN],top[MAXN],fa[MAXN],son[MAXN],siz[MAXN],dep[MAXN];
struct kl{
	int l,r,lazy,lc,rc,sum;
};
kl t[MAXN<<2];
void lian(int s,int t){
	tot++;
	to[tot]=t;
	nxt[tot]=head[s];
	head[s]=tot;
}
void dfs1(int x,int pre){
	dep[x]=dep[pre]+1;
	siz[x]=1;
	fa[x]=pre;
	for(int i=head[x];i;i=nxt[i]){
		int y=to[i];
		if(y==pre)continue;
		dfs1(y,x);
		siz[x]+=siz[y];
		if(siz[y]>siz[son[x]]){
			son[x]=y;
		}
	}
}
void dfs2(int x){
	top[x]=x==son[fa[x]]?top[fa[x]]:x;
	seg[x]=++cnt;
	rev[cnt]=x;
	if(son[x])dfs2(son[x]);
	for(int i=head[x];i;i=nxt[i]){
		int y=to[i];
		if(!top[y]){
			dfs2(y);
		}
	}
}
void push_up(int p){
	t[p].lc=t[p*2].lc;
	t[p].rc=t[p*2+1].rc;
	t[p].sum=t[p*2].sum+t[p*2+1].sum;
	if(t[p*2].rc==t[p*2+1].lc)t[p].sum--;
}
void push_down(int p){
	if(t[p].lazy==-1)return ;
	t[p*2].lazy=t[p*2+1].lazy=t[p].lazy;
	t[p*2].lc=t[p*2].rc=t[p].lazy;
	t[p*2+1].lc=t[p*2+1].rc=t[p].lazy;
	t[p*2].sum=t[p*2+1].sum=1;
	t[p].lazy=-1;
}
void build(int p,int l,int r){
	t[p].l=l,t[p].r=r;
	t[p].lazy=-1;
	if(l==r){
		t[p].sum=1;
		t[p].lc=t[p].rc=w[rev[l]];
		return ;
	}
	int mid=(l+r)>>1;
	build(p*2,l,mid);
	build(p*2+1,mid+1,r);
	push_up(p);
}
int query(int p,int l,int r){
	if(l<=t[p].l&&t[p].r<=r){
		if(t[p].l==l)L=t[p].lc;   
		if(t[p].r==r)R=t[p].rc;        
		return t[p].sum;
	}
	push_down(p);
	int ret=0;
	int mid=(t[p].l+t[p].r)/2;
	if(l<=mid)ret+=query(p*2,l,r);
	if(r>mid)ret+=query(p*2+1,l,r);
	if(l<=mid&&mid<r)ret-=(t[p*2].rc==t[p*2+1].lc);
	return ret;
}

int ask_sum(int x,int y){
	int ans=0;
	int xc=-1,yc=-1;
	while(top[x]!=top[y]){
		if(dep[top[x]]<dep[top[y]]){
			swap(x,y);
			swap(xc,yc);
		}
		ans+=query(1,seg[top[x]],seg[x]);
		if(xc==R)ans--;
		xc=L;
		x=fa[top[x]];
	}
	if(dep[x]>dep[y]){
		swap(x,y);
		swap(xc,yc);	
	}
	ans+=query(1,seg[x],seg[y]);
	ans-=(xc==L)+(yc==R);
	return ans;
} 
void updata(int p,int l,int r,int col){
	if(l<=t[p].l&&t[p].r<=r){
		t[p].sum=1;
		t[p].lc=t[p].rc=col;
		t[p].lazy=col;
		return ;
	}
	push_down(p);
	int mid=(t[p].l+t[p].r)/2;
	if(l<=mid)updata(p*2,l,r,col);
	if(r>mid)updata(p*2+1,l,r,col);
	push_up(p);
}
void change(int x,int y,int col){
	while(top[x]!=top[y]){
		if(dep[top[x]]<dep[top[y]]){
			swap(x,y);
		}
		updata(1,seg[top[x]],seg[x],col);
		x=fa[top[x]];
	}
	if(dep[x]<dep[y])swap(x,y);
	updata(1,seg[y],seg[x],col);
}
int main(){
	scanf("%d %d",&n,&m);
	for(int i=1;i<=n;i++)scanf("%d",&w[i]);
	for(int x,y,i=1;i<n;i++){
		scanf("%d %d",&x,&y);
		lian(x,y),lian(y,x);
	}
	dfs1(1,0);
	dfs2(1);
	build(1,1,n);
	for(int i=1;i<=m;i++){
		scanf("%s",s);
		if(s[0]=='Q'){
			scanf("%d %d",&a,&b);
			printf("%d\n",ask_sum(a,b));
		}
		else{
			scanf("%d %d %d",&a,&b,&c);
			change(a,b,c);
		}
	}
	return 0;
}
原文