注意线段树的合并就好了。
【代码】
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#define N 100005
#define M 5005
#define INF 1e9
#define mod 1000000007
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pa;
int read()
{
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();}
while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
int n,m,cnt,ind;
int b[N<<1],p[N],nextedge[N<<1];
int sz[N],son[N],fa[N],pos[N],ftree[N],top[N],deep[N],a[N];
class Seg_Tree{
public:
int lc,rc,l,r,sum,tag;
}e[N<<2];
void Add(int x,int y)
{
cnt++;
b[cnt]=y;
nextedge[cnt]=p[x];
p[x]=cnt;
}
void Anode(int x,int y){
Add(x,y);Add(y,x);
}
void Dfs(int x)
{
sz[x]=1;
for(int i=p[x];i;i=nextedge[i])
{
int v=b[i];
if(v==fa[x]) continue;
deep[v]=deep[x]+1;
fa[v]=x;
Dfs(v);
sz[x]+=sz[v];
son[x]=sz[son[x]]>sz[v]?son[x]:v;
}
}
void dfs(int x,int root)
{
pos[x]=++ind;ftree[pos[x]]=x;top[x]=root;
if(!son[x]) return;
dfs(son[x],root);
for(int i=p[x];i;i=nextedge[i])
{
int v=b[i];
if(v!=fa[x]&&v!=son[x]) dfs(v,v);
}
}
void Fresh(int p,int z)
{
e[p].tag=z;e[p].sum=1;
e[p].lc=e[p].rc=z;
}
void pushup(int p)
{
e[p].lc=e[p<<1].lc,e[p].rc=e[p<<1|1].rc;
e[p].sum=e[p<<1].sum+e[p<<1|1].sum-(e[p<<1].rc==e[p<<1|1].lc);
}
void pushdown(int p)
{
if(e[p].tag!=-1)
{
e[p<<1].tag=e[p<<1|1].tag=e[p].tag;
Fresh(p<<1,e[p].tag);Fresh(p<<1|1,e[p].tag);
e[p].tag=-1;
}
}
void Build(int p,int l,int r)
{
e[p].l=l,e[p].r=r;e[p].tag=-1;
if(l==r)
{
e[p].lc=e[p].rc=a[ftree[l]];
e[p].sum=1;return;
}
int mid=l+r>>1;
Build(p<<1,l,mid);Build(p<<1|1,mid+1,r);
pushup(p);
}
void Change(int p,int x,int y,int z)
{
int l=e[p].l,r=e[p].r,mid=l+r>>1;
if(l==x&&y==r)
{
Fresh(p,z);
return;
}
pushdown(p);
if(y<=mid) Change(p<<1,x,y,z);
else if(x>mid) Change(p<<1|1,x,y,z);
else Change(p<<1,x,mid,z),Change(p<<1|1,mid+1,y,z);
pushup(p);
}
void Solve_Change(int x,int y,int z)
{
int fx=top[x],fy=top[y];
while(fx!=fy)
{
if(deep[fx]<deep[fy]) swap(x,y),swap(fx,fy);
Change(1,pos[fx],pos[x],z);
x=fa[fx],fx=top[x];
}
if(deep[x]<deep[y]) swap(x,y);
Change(1,pos[y],pos[x],z);
}
int Query(int p,int x,int y)
{
int l=e[p].l,r=e[p].r,mid=l+r>>1;
if(l==x&&y==r) return e[p].sum;
pushdown(p);
if(y<=mid) return Query(p<<1,x,y);
if(x>mid) return Query(p<<1|1,x,y);
return Query(p<<1,x,mid)+Query(p<<1|1,mid+1,y)-(e[p<<1].rc==e[p<<1|1].lc);
}
int Get_C(int p,int x)
{
int l=e[p].l,r=e[p].r,mid=l+r>>1;
if(l==x&&r==x) return e[p].lc;
pushdown(p);
if(x<=mid) return Get_C(p<<1,x);
return Get_C(p<<1|1,x);
}
void Solve_Query(int x,int y)
{
int fx=top[x],fy=top[y];
int rtn=0;
while(fx!=fy)
{
if(deep[fx]<deep[fy]) swap(x,y),swap(fx,fy);
rtn+=Query(1,pos[fx],pos[x]);
rtn-=(Get_C(1,pos[fa[fx]])==Get_C(1,pos[fx]));
x=fa[fx],fx=top[x];
}
if(deep[x]<deep[y]) swap(x,y);
rtn+=Query(1,pos[y],pos[x]);
printf("%d\n",rtn);
}
void Solve()
{
while(m--)
{
char ch[1];
scanf("%s",ch);
static int x,y;
x=read(),y=read();
if(ch[0]=='C') Solve_Change(x,y,read());
else Solve_Query(x,y);
}
}
int main()
{
n=read(),m=read();
for(int i=1;i<=n;i++) a[i]=read();
for(int i=1;i<n;i++)
{
static int x,y;
x=read(),y=read();
Anode(x,y);
}
Dfs(1);dfs(1,1);Build(1,1,n);
Solve();
return 0;
}