1566 染色 省队选拔赛山东
时间限制: 2 s 空间限制: 256000 KB 题目等级 : 大师 Master
题目描述 Description
给定一棵有n个节点的无根树和m个操作,操作有2类:
1、将节点a到节点b路径上所有点都染成颜色c;
2、询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段),如“112221”由3段组成:“11”、“222”和“1”。
请你写一个程序依次完成这m个操作。
输入描述 Input Description
第一行包含2个整数n和m,分别表示节点数和操作数;
第二行包含n个正整数表示n个节点的初始颜色
下面n-1行每行包含两个整数 和 ,表示x和y之间有一条无向边。
下面m行每行描述一个操作:
“C a b c”表示这是一个染色操作,把节点a到节点b路径上所有点(包括a和b)都染成颜色c;
“Q a b”表示这是一个询问操作,询问节点a到节点b(包括a和b)路径上的颜色段数量。
输出描述 Output Description
对于每个询问操作,输出一行答案。
样例输入 Sample Input
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
样例输出 Sample Output
3
1
2
数据范围及提示 Data Size & Hint
对于100%的数据1≤n≤104,1≤m≤105,1≤m≤109;
高级数据结构–线段树维护 最大子段和
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<deque>
using namespace std;
#define maxn 1000010
#define INF 0x5fffffff
#define lson now<<1
#define rson now<<1|1
struct Node{
int l,r,ans,flag,lc,rc;
}tre[1000010];
struct Edge{
int to,next;
}e[maxn];
int head[maxn],fa[maxn],siz[maxn],top[maxn],son[maxn],n,m;
int color[maxn],tot,dep[maxn],vis[maxn],visx,pre[maxn];
void Add_Edge(int u,int v){
e[++tot].to=v;e[tot].next=head[u];
head[u]=tot;
}
void UpDate(int now){
tre[now].lc=tre[now<<1].lc;
tre[now].rc=tre[now<<1|1].rc;
if(tre[lson].rc==tre[rson].lc)
tre[now].ans=tre[lson].ans+tre[rson].ans-1;
else tre[now].ans=tre[lson].ans+tre[rson].ans;
}
void Built(int now,int l,int r){
tre[now].l=l;tre[now].r=r;
if(l==r){
tre[now].lc=tre[now].rc=color[pre[l]];
tre[now].flag=color[pre[l]];
tre[now].ans=1; return;
}
int mid=(l+r)>>1;
Built(now<<1,l,mid);Built(now<<1|1,mid+1,r);
UpDate(now);
}
void DFS_First(int u,int father,int deepth){
fa[u]=father;dep[u]=deepth;son[u]=0;siz[u]=1;
for(int i=head[u];i;i=e[i].next){
int v=e[i].to;
if(v==father) continue;
DFS_First(v,u,deepth+1);
siz[u]+=siz[v];
if(siz[v]>siz[son[u]]) son[u]=v;
}
}
void DFS_Second(int u,int Top){
vis[u]= ++visx;
top[u]=Top;
pre[vis[u]]=u;
if(son[u]) DFS_Second(son[u],Top);
for(int i=head[u];i;i=e[i].next){
int v=e[i].to;
if(v!=fa[u]&&v!=son[u]) DFS_Second(v,v);
}
}
void Flag_Down(int now){
tre[lson].lc=tre[lson].rc=tre[lson].flag=tre[now].flag;
tre[rson].lc=tre[rson].rc=tre[rson].flag=tre[now].flag;
tre[lson].ans=tre[rson].ans=1;
tre[now].flag=0;
}
void change(int now,int l,int r,int c){
if(l<=tre[now].l&&tre[now].r<=r){
tre[now].lc=tre[now].rc=tre[now].flag=c;
tre[now].ans=1; return;
}
if(tre[now].flag) Flag_Down(now);
int mid=(tre[now].l+tre[now].r)>>1;
if(l<=mid) change(now<<1,l,r,c);
if(r>mid) change(now<<1|1,l,r,c);
UpDate(now);
}
void Change(int x,int y,int z){// 从x到y 全部染成z
while(top[x]!=top[y]){
if(dep[top[x]] < dep[top[y]]) swap(x,y);
change(1,vis[top[x]],vis[x],z);
x=fa[top[x]];
}
if(dep[x] > dep[y]) swap(x,y);
change(1,vis[x],vis[y],z);
}
int query(int now,int l,int r){
if(l<=tre[now].l&&tre[now].r<=r) return tre[now].ans;
if(tre[now].flag) Flag_Down(now);
int ans=0;
int mid=(tre[now].l+tre[now].r)>>1;
if(l<=mid) ans+=query(now<<1,l,r);
if(r>mid) ans+=query(now<<1|1,l,r);
if(l<=mid&&mid<r&&tre[now<<1].rc==tre[now<<1|1].lc) ans--;
return ans;
}
int Get_Color(int u,int id){
if(tre[u].l==tre[u].r) return tre[u].lc;
if(tre[u].flag) Flag_Down(u);
int mid=(tre[u].l+tre[u].r)>>1;
if(id<=mid) return Get_Color(u<<1,id);
else return Get_Color(u<<1|1,id);
}
int Query(int x,int y){
int ans=0;
while(top[x]!=top[y]){
if(dep[top[x]] < dep[top[y]]) swap(x,y);
ans+=query(1,vis[top[x]],vis[x]);
if(Get_Color(1,vis[top[x]])==
Get_Color(1,vis[fa[top[x]]]))ans--;
x=fa[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
ans+=query(1,vis[x],vis[y]);
return ans;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",&color[i]);
for(int i=1,u,v;i<n;i++){
scanf("%d%d",&u,&v);
Add_Edge(u,v);Add_Edge(v,u);
}
DFS_First(1,1,1);
DFS_Second(1,1);
Built(1,1,n);
char c;
for(int test=1;test<=m;test++){
cin>>c;
if(c=='C'){
int l,r,x;
scanf("%d%d%d",&l,&r,&x);
Change(l,r,x);
}
else{
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",Query(l,r));
}
}
return 0;
}