题目大意:C a b c 将a到b的路径上的点权覆盖为c
Q a b 查询a到b的路径上有几段颜色
bzoj2243染色
树链剖分 写的不熟 注意细节
#include<iostream>
#include<cstdio>
#define N 100005
using namespace std;
struct E {int to,nxt;}edge[N*2];
struct T {int l,r,lc,rc,numc,cov;}t[N*4];
int n,m,cnt=0;
int sz[N],top[N],fa[N],son[N],w[N],val[N],dep[N],f[N];
int idx[N],tot=1;
void addedge(int from,int to){
edge[tot].to=to;edge[tot].nxt=idx[from];idx[from]=tot++;
}
void dfs(int x,int Fa){
fa[x]=Fa;
for(int t=idx[x];t;t=edge[t].nxt){
E e=edge[t];
if(e.to==Fa) continue;
dep[e.to]=dep[x]+1;
dfs(e.to,x);
sz[x]+=sz[e.to];
if(sz[son[x]]<sz[e.to]) son[x]=e.to;
}
sz[x]++;
}
void dfs2(int x,int Top){
top[x]=Top;w[x]=++cnt;f[cnt]=x;
if(son[x]) dfs2(son[x],Top);
for(int t=idx[x];t;t=edge[t].nxt){
E e=edge[t];
if(e.to!=fa[x] && e.to!=son[x]) dfs2(e.to,e.to);
}
}
T merge(T a,T b){
if(a.cov==-2) return b;
if(b.cov==-2) return a;
T tmp;
tmp.lc=a.lc;tmp.rc=b.rc;tmp.cov=-1;
tmp.numc=a.numc+b.numc-(a.rc==b.lc);
tmp.l=a.l;tmp.r=b.r;
return tmp;
}
void pushup(int k){
t[k]=merge(t[k<<1],t[k<<1|1]);
}
void pushdown(int k){
if(t[k].cov==-1) return;
t[k<<1].cov=t[k<<1|1].cov=t[k<<1].lc=t[k<<1|1].lc=t[k<<1].rc=t[k<<1|1].rc=t[k].cov;
t[k<<1].numc=t[k<<1|1].numc=1;
}
void build(int k,int l,int r){
if(l==r){
t[k].l=t[k].r=l;
t[k].lc=t[k].rc=val[f[l]];
t[k].numc=1;t[k].cov=-1;return;
}
int mid=(l+r)>>1;
build(k<<1,l,mid);build(k<<1|1,mid+1,r);pushup(k);
}
void cover(int k,int a,int b,int c){
int l=t[k].l,r=t[k].r;
if(b<l || r<a) return;
if(a<=l && r<=b){
t[k].lc=t[k].rc=t[k].cov=c;t[k].numc=1;
return;
}
pushdown(k);
cover(k<<1,a,b,c);cover(k<<1|1,a,b,c);
pushup(k);
}
T query(int k,int a,int b){
int l=t[k].l,r=t[k].r;
if(b<l || r<a) return t[0];
if(a<=l && r<=b) return t[k];
pushdown(k);
return merge(query(k<<1,a,b),query(k<<1|1,a,b));
}
int preq(int x,int y){
int fx=top[x],fy=top[y];
int lastx=-1,lasty=-1,ans=0;T tt;
while(fx!=fy){
if(dep[fx]<dep[fy]){
tt=query(1,w[fy],w[y]);
ans+=tt.numc-(tt.rc==lasty);
lasty=tt.lc;
y=fa[fy];fy=top[y];
}
else{
tt=query(1,w[fx],w[x]);
ans+=tt.numc-(tt.rc==lastx);
lastx=tt.lc;
x=fa[fx];fx=top[x];
}
}
if(w[x]<w[y]){
tt=query(1,w[x],w[y]);
ans+=tt.numc-(tt.lc==lastx)-(tt.rc==lasty);
}
else{
tt=query(1,w[y],w[x]);
ans+=tt.numc-(tt.rc==lastx)-(tt.lc==lasty);
}
return ans;
}
void prec(int x,int y,int z){
int fx=top[x],fy=top[y];
while(fx!=fy){
if(dep[fx]<dep[fy]) swap(fx,fy),swap(x,y);
cover(1,w[fx],w[x],z);
x=fa[fx];fx=top[x];
}
cover(1,min(w[x],w[y]),max(w[x],w[y]),z);
}
int main(){
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
int x,y,z;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",&val[i]);
for(int i=1;i<n;i++){
scanf("%d%d",&x,&y);
addedge(x,y);addedge(y,x);
}
dfs(1,-1);dfs2(1,1);
build(1,1,n);t[0].cov=-2;
// for(int i=1;i<=24;i++) printf("%d %d %d %d %d %d %d\n",i,t[i].l,t[i].r,t[i].lc,t[i].rc,t[i].numc,t[i].cov);
for(int i=1;i<=m;i++){
char s[10];scanf("%s",s);
if(s[0]=='C') scanf("%d%d%d\n",&x,&y,&z),prec(x,y,z);
if(s[0]=='Q') scanf("%d%d\n",&x,&y),printf("%d\n",preq(x,y));
}
// printf("son fa top w\n");
// for(int i=1;i<=n;i++) printf("%d %d %d %d\n",son[i],fa[i],top[i],w[i]);
return 0;
}