明显可以树链剖分。线段树每个节点需要维护这个区间的段数s,下放标记f,还有区间最左的颜色l和最右的颜色r。往上推的时候,s为左右两段的s之和,如果左边的r和右边的l相等,s要-1。
在树链剖分求路径上总s时,再把每一段加起来同时,还要判断上一次的终点和这一次的起点颜色是否相同,相同的话也要-1。因为是x和y同时跳,所以同时记录prex和prey,线段树查询时多传递两个值为这一段的起点和终点颜色,再比较并更新prex和prey就好了。当跳到fx==fy时,要用最后一段的左右两端颜色分别和prex,prey比较。
#include<cstdio>
#include<cstring>
const int N=100010;
struct edge{
int y,next;
}data[N*2];
struct node{
int a,b,s,l,r,f;
}tree[N*4];
int n,m,a1[N],h[N],num,num1;
int son[N],siz[N],faz[N],dep[N],tid[N],rnk[N],top[N];
char str1[5];
inline int read(){
int x=0,f=0;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return f?-x:x;
}
void dfs1(int u,int fa,int step){
son[u]=-1;faz[u]=fa;siz[u]=1;dep[u]=step;
for(int i=h[u];i!=-1;i=data[i].next){
int v=data[i].y;
if(v!=fa){
dfs1(v,u,step+1);
siz[u]+=siz[v];
if(son[u]==-1||siz[son[u]]<siz[v])son[u]=v;
}
}
}
void dfs2(int u,int fa,int t){
top[u]=t;tid[u]=++num1;rnk[num1]=u;
if(son[u]==-1)return;
dfs2(son[u],u,t);
for(int i=h[u];i!=-1;i=data[i].next){
int v=data[i].y;
if(v!=fa&&v!=son[u])dfs2(v,u,v);
}
}
inline void update(int p){
tree[p].l=tree[p<<1].l;tree[p].r=tree[p<<1|1].r;
tree[p].s=tree[p<<1].s+tree[p<<1|1].s;
if(tree[p<<1].r==tree[p<<1|1].l)tree[p].s--;
}
void build(int p,int a,int b){
tree[p].a=a;tree[p].b=b;tree[p].f=0;
if(a==b){tree[p].s=1;tree[p].l=tree[p].r=a1[rnk[a]];return;}
int mid=(a+b)>>1;
build(p<<1,a,mid);build(p<<1|1,mid+1,b);
update(p);
}
inline void new1(int p,int c){
tree[p].s=1;tree[p].f=c;tree[p].l=tree[p].r=c;
}
inline void pushdown(int p){
if(tree[p].f){
new1(p<<1,tree[p].f);new1(p<<1|1,tree[p].f);
tree[p].f=0;
}
}
void change(int p,int a,int b,int c){
if(a<=tree[p].a&&tree[p].b<=b){new1(p,c);return;}
pushdown(p);
int mid=(tree[p].a+tree[p].b)>>1;
if(a<=mid)change(p<<1,a,b,c);
if(mid<b)change(p<<1|1,a,b,c);
update(p);
}
int query(int p,int a,int b,int &l1,int &r1){
if(a<=tree[p].a&&tree[p].b<=b){
l1=tree[p].l;r1=tree[p].r;
return tree[p].s;
}
pushdown(p);
int mid=(tree[p].a+tree[p].b)>>1,q1=0,q2=0,ans=0,ll1,rr1,ll2,rr2;
if(a<=mid)q1=query(p<<1,a,b,ll1,rr1);
if(mid<b)q2=query(p<<1|1,a,b,ll2,rr2);
update(p);
l1=q1?ll1:ll2;r1=q2?rr2:rr1;
ans=q1+q2;
if(q1&&q2&&tree[p<<1].r==tree[p<<1|1].l)ans--;
return ans;
}
void lca_change(int x,int y,int z){
int fx=top[x],fy=top[y];
while(fx!=fy){
if(dep[fx]<dep[fy]){
change(1,tid[fy],tid[y],z);
y=faz[fy];fy=top[y];
}else{
change(1,tid[fx],tid[x],z);
x=faz[fx];fx=top[x];
}
}
if(dep[x]<dep[y])change(1,tid[x],tid[y],z);
else change(1,tid[y],tid[x],z);
}
int lca_query(int x,int y){
int fx=top[x],fy=top[y],ans=0;
int ll,rr,prey=-1,prex=-1;
while(fx!=fy){
if(dep[fx]<dep[fy]){
ans+=query(1,tid[fy],tid[y],ll,rr);
if(rr==prey)ans--;
prey=ll;
y=faz[fy];fy=top[y];
}else{
ans+=query(1,tid[fx],tid[x],ll,rr);
if(rr==prex)ans--;
prex=ll;
x=faz[fx];fx=top[x];
}
}
if(dep[x]<dep[y]){
ans+=query(1,tid[x],tid[y],ll,rr);
if(rr==prey)ans--;
if(ll==prex)ans--;
}else{
ans+=query(1,tid[y],tid[x],ll,rr);
if(rr==prex)ans--;
if(ll==prey)ans--;
}
return ans;
}
int main(){
n=read();m=read();
for(int i=1;i<=n;i++)a1[i]=read();
memset(h,-1,sizeof h);num=0;
for(int x,y,i=1;i<n;i++){
x=read();y=read();
data[++num].y=y;data[num].next=h[x];h[x]=num;
data[++num].y=x;data[num].next=h[y];h[y]=num;
}
dfs1(1,0,1);dfs2(1,0,1);build(1,1,n);
for(int x,y,z,i=1;i<=m;i++){
scanf("%s",str1);
if(str1[0]=='C'){
x=read();y=read();z=read();
lca_change(x,y,z);
}else{
x=read();y=read();
printf("%d\n",lca_query(x,y));
}
}
return 0;
}