https://www.lydsy.com/JudgeOnline/problem.php?id=2243
树链剖分加区间合并 每个节点维护最左边和最右边的颜色以及当前区间的总段数 最难处理的地方在于一条链会被剖分成很多小段 需要将衔接部分处理好
#include <bits/stdc++.h>
using namespace std;
struct node1
{
int v;
int next;
};
struct node2
{
int l;
int r;
int left;
int right;
int val;
int laz;
};
node1 edge[200010];
node2 tree[400010];
int clr[100010],first[100010],fa[100010],deep[100010],sum[100010],son[100010],top[100010],mp1[100010],mp2[100010];
int n,q,num,ans,pre,lc,rc;
void addedge(int u,int v)
{
edge[num].v=v;
edge[num].next=first[u];
first[u]=num++;
}
void dfsI(int cur)
{
int i,v;
sum[cur]=1,son[cur]=-1;
for(i=first[cur];i!=-1;i=edge[i].next)
{
v=edge[i].v;
if(v!=fa[cur])
{
fa[v]=cur,deep[v]=deep[cur]+1;
dfsI(v);
sum[cur]+=sum[v];
if(son[cur]==-1||sum[son[cur]]<sum[v])
{
son[cur]=v;
}
}
}
}
void dfsII(int cur,int tp)
{
int i,v;
num++;
top[cur]=tp,mp1[cur]=num,mp2[num]=cur;
if(son[cur]==-1) return;
dfsII(son[cur],tp);
for(i=first[cur];i!=-1;i=edge[i].next)
{
v=edge[i].v;
if(v!=fa[cur]&&v!=son[cur])
{
dfsII(v,v);
}
}
}
void pushup(int cur)
{
tree[cur].left=tree[2*cur].left,tree[cur].right=tree[2*cur+1].right;
tree[cur].val=tree[2*cur].val+tree[2*cur+1].val;
if(tree[2*cur].right==tree[2*cur+1].left) tree[cur].val--;
}
void pushdown(int cur)
{
if(tree[cur].laz!=-1)
{
tree[2*cur].left=tree[2*cur].right=tree[cur].laz;
tree[2*cur].val=1;
tree[2*cur].laz=tree[cur].laz;
tree[2*cur+1].left=tree[2*cur+1].right=tree[cur].laz;
tree[2*cur+1].val=1;
tree[2*cur+1].laz=tree[cur].laz;
tree[cur].laz=-1;
}
}
void build(int l,int r,int cur)
{
int m;
tree[cur].l=l;
tree[cur].r=r;
tree[cur].left=0;
tree[cur].right=0;
tree[cur].val=0;
tree[cur].laz=-1;
if(l==r)
{
tree[cur].left=clr[mp2[l]];
tree[cur].right=clr[mp2[l]];
tree[cur].val=1;
return;
}
m=(l+r)/2;
build(l,m,2*cur);
build(m+1,r,2*cur+1);
pushup(cur);
}
void updateII(int pl,int pr,int c,int cur)
{
if(pl<=tree[cur].l&&tree[cur].r<=pr)
{
tree[cur].left=tree[cur].right=c;
tree[cur].val=1;
tree[cur].laz=c;
return;
}
pushdown(cur);
if(pl<=tree[2*cur].r) updateII(pl,pr,c,2*cur);
if(pr>=tree[2*cur+1].l) updateII(pl,pr,c,2*cur+1);
pushup(cur);
}
void updateI(int u,int v,int c)
{
while(top[u]!=top[v])
{
if(deep[top[u]]<deep[top[v]]) swap(u,v);
updateII(mp1[top[u]],mp1[u],c,1);
u=fa[top[u]];
}
if(deep[u]<deep[v]) swap(u,v);
updateII(mp1[v],mp1[u],c,1);
}
int getlca(int u,int v)
{
while(top[u]!=top[v])
{
if(deep[top[u]]<deep[top[v]]) swap(u,v);
u=fa[top[u]];
}
if(deep[u]<deep[v]) return u;
else return v;
}
void queryII(int pl,int pr,int cur)
{
if(pl<=tree[cur].l&&tree[cur].r<=pr)
{
ans+=tree[cur].val;
if(tree[cur].left==rc) ans--;
if(lc==-1) lc=tree[cur].left;
rc=tree[cur].right;
return;
}
pushdown(cur);
if(pl<=tree[2*cur].r) queryII(pl,pr,2*cur);
if(pr>=tree[2*cur+1].l) queryII(pl,pr,2*cur+1);
}
void queryI(int u,int v)
{
int lca;
ans=0,lca=getlca(u,v);
pre=-1;
while(top[u]!=top[lca])
{
lc=-1,rc=-1;
queryII(mp1[top[u]],mp1[u],1);
u=fa[top[u]];
if(rc==pre) ans--;
pre=lc;
}
lc=-1,rc=-1;
queryII(mp1[lca],mp1[u],1);
if(rc==pre) ans--;
pre=-1;
while(top[v]!=top[lca])
{
lc=-1,rc=-1;
queryII(mp1[top[v]],mp1[v],1);
v=fa[top[v]];
if(rc==pre) ans--;
pre=lc;
}
lc=-1,rc=-1;
queryII(mp1[lca],mp1[v],1);
if(rc==pre) ans--;
ans--;
}
int main()
{
int n,q,u,v,c,i;
char op[10];
while(scanf("%d%d",&n,&q)!=EOF)
{
for(i=1;i<=n;i++)
{
scanf("%d",&clr[i]);
}
memset(first,-1,sizeof(first));
num=0;
for(i=1;i<=n-1;i++)
{
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
fa[1]=-1,deep[1]=1;
dfsI(1);
num=0;
dfsII(1,1);
build(1,n,1);
while(q--)
{
scanf("%s",op);
if(op[0]=='C')
{
scanf("%d%d%d",&u,&v,&c);
updateI(u,v,c);
}
else
{
scanf("%d%d",&u,&v);
queryI(u,v);
printf("%d\n",ans);
}
}
}
return 0;
}