rt。
经过几轮调试,样例已过。
但一个点都跑不过去。
#include<cstdio>
#include<algorithm>
#define maxn 1000010
#define Inf 0x3f3f3f3f
using namespace std;
inline int read(){
register int d=0,f=1;char ch = getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
d = (d<<1)+(d<<3)+(ch^48);
ch = getchar();
}
return d*f;
}
void writes(int x){
if(x>9) writes(x/10);
putchar(x%10+48);
}
inline void write(int x){
if(x<0) putchar('-'),writes(-x);
else writes(x);
putchar('\n');
}
int edgeCnt;
struct Edge{
int to,nxt;
}edge[maxn];
int head[maxn];
int fa[maxn];
int siz[maxn];
int w[maxn],wt[maxn];
int son[maxn];
int id[maxn];
int top[maxn];
int dep[maxn];
int col[maxn];
int ncol[maxn];
int n,m,r,cnt;
struct Node{
int lc,rc,val,tag;
}tree[maxn<<4];
void add(int u,int v){
edge[++edgeCnt].to=v;
edge[edgeCnt].nxt=head[u];
head[u]=edgeCnt;
}
void pushdown(int x){
if(tree[x].tag){
tree[x<<1].lc=tree[x].tag;
tree[x<<1].rc=tree[x].tag;
tree[x<<1].tag=tree[x].tag;
tree[x<<1].val=1;
tree[x<<1|1].lc=tree[x].tag;
tree[x<<1|1].rc=tree[x].tag;
tree[x<<1|1].tag=tree[x].tag;
tree[x<<1|1].val=1;
tree[x].tag=0;
}
}
void build(int rt,int l,int r){
if(l==r){
tree[rt].lc=tree[rt].rc=ncol[l];
tree[rt].val=1;
tree[rt].tag=0;
return ;
}
int mid=(l+r)>>1;
build(rt<<1,l,mid);
build(rt<<1|1,mid+1,r);
tree[rt].lc=tree[rt<<1].lc,tree[rt].rc=tree[rt<<1|1].rc;
tree[rt].val=tree[rt<<1].val+tree[rt<<1|1].val-(tree[rt<<1].rc==tree[rt<<1|1].lc);
}
void update(int rt,int l,int r,int ql,int qr,int col){
if(ql<=l&&r<=qr){
tree[rt].val=1;
tree[rt].lc=col;
tree[rt].rc=col;
tree[rt].tag=col;
return ;
}
int mid=(l+r)>>1;
pushdown(rt);
if(ql<=mid) update(rt<<1,l,mid,ql,qr,col);
if(mid<qr) update(rt<<1|1,mid+1,r,ql,qr,col);
tree[rt].lc=tree[rt<<1].lc,tree[rt].rc=tree[rt<<1|1].rc;
tree[rt].val=tree[rt<<1].val+tree[rt<<1|1].val-(tree[rt<<1].rc==tree[rt<<1|1].lc);
}
int L,R;
int query(int rt,int l,int r,int ql,int qr){
if(ql==l) L=tree[rt].lc;
if(qr==r) R=tree[rt].rc;
if(ql<=l&&r<=qr){
return tree[rt].val;
}
int mid=(l+r)>>1,ans=0;
pushdown(rt);
if(ql<=mid) ans+=query(rt<<1,l,mid,ql,qr);
if(mid<qr) ans+=query(rt<<1|1,mid+1,r,ql,qr);
return ans;
}
void Update(int x,int y,int col){
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]]) swap(x,y);
update(1,1,n,id[top[x]],id[x],col);
x=fa[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
update(1,1,n,id[x],id[y],col);
}
int Query(int x,int y){
int res=0,ans1=0,ans2=0;
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]]) swap(x,y),swap(ans1,ans2);
res+=query(1,1,n,id[top[x]],id[x]);
if(R==ans1) res--;
x=fa[top[x]];
ans1=L;
}
if(dep[x]>dep[y]) swap(x,y),swap(ans1,ans2);
res+=query(1,1,n,id[x],id[y]);
if(ans1==R) res--;
if(ans2==L) res--;
return res;
}
void dfs1(int x,int Fa,int deep){
dep[x]=deep;
siz[x]=1;
fa[x]=Fa;
int maxx=-Inf;
for(int i=head[x];i;i=edge[i].nxt){
int y=edge[i].to;
if(y==Fa) continue;
dfs1(y,x,deep+1);
siz[x]+=siz[y];
if(siz[y]>maxx) maxx=siz[y],son[x]=y;
}
}
void dfs2(int x,int tops){
top[x]=tops;
id[x]=++cnt;
ncol[id[x]]=col[x];
if(!son[x]) return ;
dfs2(son[x],tops);
for(int i=head[x];i;i=edge[i].nxt){
int y=edge[i].to;
if(y==fa[x]||y==son[x]) continue;
dfs2(y,y);
}
}
char op[maxn];
signed main(){
n=read(),m=read();
for(int i=1;i<=n;i++) col[i]=read();
for(int i=1;i<n;i++){
int u=read(),v=read();
add(u,v);add(v,u);
}
dfs1(1,0,1);
dfs2(1,1);
build(1,1,n);
while(m--){
scanf("%s",op);
if(op[0]=='C'){
int a=read(),b=read(),c=read();
Update(a,b,c);
}
else{
int a=read(),b=read();
write(Query(a,b));
}
}
return 0;
}
/*
1
/ \
2 3
/|\
4 5 6
*/