全 WA
找不到错了
到点睡觉了明早再来看
求指错
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#define maxn 1001000
#define ls x<<1
#define rs x<<1|1
using namespace std;
int n,m;
int cnt,siz[maxn],tot,a[maxn],head[maxn];
int dfn[maxn],top[maxn],dep[maxn],fa[maxn],son[maxn],lazy[maxn],pre[maxn];
struct node{int l,r,tot;}col[maxn];
struct edge{int fr,to,nxt;}e[maxn];
void add(int fr,int to){e[++tot].fr=fr;e[tot].to=to;e[tot].nxt=head[fr];head[fr]=tot;}
int read() {
int s=0,w=1;char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=(s<<1)+(s<<3)+ch-'0',ch=getchar();
return s*w;
}
namespace Seg{
void pushup(int x){
col[x].l=col[ls].l;col[x].r=col[rs].r;
col[x].tot=col[ls].tot+col[rs].tot;
if(col[ls].r==col[rs].l) col[x].tot--;
}
void pushdown(int x){
if(lazy[x]){
lazy[ls]=lazy[rs]=lazy[x];
col[ls].tot=col[rs].tot=1;
col[ls].l=col[ls].r=col[rs].r=col[rs].l=lazy[x];
lazy[x]=0;
}
}
void build(int x,int l,int r){
if(l==r){col[x].tot=1;col[x].l=col[x].r=a[pre[l]];return;}
int mid=l+r>>1;
build(ls,l,mid);build(rs,mid+1,r);
pushup(x);
}
void Update(int x,int l,int r,int val,int L,int R){
if(L<=l&&R>=r){col[x].l=col[x].r=lazy[x]=val;col[x].tot=1;return;}
int mid=l+r>>1;
pushdown(x);
if(L<=mid) Update(ls,l,mid,val,L,R);
if(R>=mid+1) Update(rs,mid+1,r,val,L,R);
pushup(x);
}
int Getcol(int x,int l,int r,int rt){
if(l==r) return col[x].l;
int mid=l+r>>1;
pushdown(x);
if(rt<=mid) return Getcol(ls,l,mid,rt);
else return Getcol(rs,mid+1,r,rt);
}
int Query(int x,int l,int r,int L,int R){
int ans=0;bool flag=0;
if(L<=l&&R>=r) return col[x].tot;
int mid=l+r>>1;
pushdown(x);
if(L<=mid){ans+=Query(ls,l,mid,l,R);flag=1;}
if(R>=mid+1){ans+=Query(rs,mid+1,r,L,R);if(flag==1){if(col[ls].r==col[rs].l) ans--;}}
return ans;
}
}
namespace Cut{
void dfs1(int x,int Fa){
fa[x]=Fa;dep[x]=dep[Fa]+1;siz[x]=1;
for(int i=head[x];i;i=e[i].nxt){
int to=e[i].to;
if(to==fa[x]) continue;
dfs1(to,x);
siz[x]+=siz[to];
if(siz[son[x]]<siz[to]) son[x]=to;
}
}
void dfs2(int x,int tp){
top[x]=tp;dfn[x]=++cnt;pre[cnt]=x;
if(son[x]) dfs2(son[x],tp);
for(int i=head[x];i;i=e[i].nxt){
int to=e[i].to;
if(to==fa[x]||to==son[x]) continue;
dfs2(to,to);
}
}
void change(int x,int y,int val){
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]]) swap(x,y);
Seg::Update(1,1,n,val,dfn[top[x]],dfn[x]);
x=fa[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
Seg::Update(1,1,n,val,dfn[x],dfn[y]);
}
int ask(int x,int y){
int ans=0;
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]]) swap(x,y);
ans+=Seg::Query(1,1,n,dfn[top[x]],dfn[x]);
if(Seg::Getcol(1,1,n,dfn[top[x]])==Seg::Getcol(1,1,n,dfn[fa[top[x]]])) ans--;
x=fa[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
ans+=Seg::Query(1,1,n,dfn[x],dfn[y]);
return ans;
}
}
int main(){
n=read();m=read();
for(int i=1;i<=n;i++) a[i]=read();
for(int i=1,u,v;i<n;i++){u=read();v=read();add(u,v);add(v,u);}
Cut::dfs1(1,0);Cut::dfs2(1,1);Seg::build(1,1,n);
for(int i=1,as,bs,cs;i<=m;i++){
char opt;cin>>opt;as=read();bs=read();
if(opt=='C'){cs=read();Cut::change(as,bs,cs);}
if(opt=='Q')printf("%d\n",Cut::ask(as,bs));
}
return 0;
}