题目
思路
树剖啊,有一个好处,就是样例过了,这玩意就A了……
这里的线段树需要维护3个东西:
最左边的点的颜色,最右边的点的颜色,颜色段数。
我们发现在统计段数的合并时,需要考虑左右2个儿子拼起来,中间的缝隙两端(即左儿子的右边和右儿子的左边)是否相同,相同段数要-1。
同理,区间查询也需要此操作。
但是,树剖的树上路径查询具有完(丧)美(心)无(病)瑕(狂)的好性质:
路径上的点在线段树上的区间不连续!
导致我们需要把重链顶端及其父节点的颜色比较,相同则结果-1.
code:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
//When I wrote this code,God and I unterstood what was I doing
inline long long read()
{
long long ret,c,f=1;
while (((c=getchar())> '9'||c< '0')&&c!='-');
if (c=='-') f=-1,ret=0;
else ret=c-'0';
while ((c=getchar())>='0'&&c<='9') ret=ret*10+c-'0';
return ret*f;
}
int xdtreel[400020],xdtreer[400020],xdtrees[400020],lazy[400020],a[100005],b[100005],fa[100005],top[100005],zson[100005],dep[100005],id[100005],siz[100005];
int head[100005],nxt[200010],to[200010],n,m,r,tot,x,y,z;
void dfs1(int x,int f)
{
dep[x]=dep[f]+1;
fa[x]=f;
siz[x]=1;
for (int i=head[x];i;i=nxt[i])
{
if (to[i]==f||to[i]==x) continue;
dfs1(to[i],x);
siz[x]+=siz[to[i]];
if (siz[zson[x]]<siz[to[i]]) zson[x]=to[i];
}
return;
}
void dfs2(int x,int f)
{
id[x]=++tot;
b[id[x]]=a[x];
top[x]=f;
if (zson[x]==0) return;
dfs2(zson[x],f);
for (int i=head[x];i;i=nxt[i])
{
if (id[to[i]]) continue;
dfs2(to[i],to[i]);
}
return;
}
void build(int l,int r,int id)
{
if (l==r)
{
xdtreel[id]=xdtreer[id]=b[l];
xdtrees[id]=1;
return;
}
build(l,(l+r)>>1,id*2);
build(((l+r)>>1)+1,r,id*2+1);
xdtrees[id]=xdtrees[id*2]+xdtrees[id*2+1];
xdtreel[id]=xdtreel[id*2],xdtreer[id]=xdtreer[id*2+1];
if (xdtreer[id*2]==xdtreel[id*2+1]) xdtrees[id]--;
return;
}
void down(int x)
{
if (lazy[x])
{
lazy[x*2]=lazy[x*2+1]=lazy[x];
xdtreel[x*2]=xdtreer[x*2]=xdtreel[x*2+1]=xdtreer[x*2+1]=lazy[x];
xdtrees[x*2]=xdtrees[x*2+1]=1;
lazy[x]=0;
}
return;
}
void qjgx(int l,int r,int id,int L,int R,int x)
{
if (l>=L&&R>=r)
{
xdtreel[id]=xdtreer[id]=x;
xdtrees[id]=1;
lazy[id]=x;
return;
}
down(id);
int mid=(l+r)>>1;
if (L<=mid) qjgx(l,mid,id*2,L,R,x);
if (R>mid) qjgx(mid+1,r,id*2+1,L,R,x);
xdtrees[id]=xdtrees[id*2]+xdtrees[id*2+1];
xdtreel[id]=xdtreel[id*2],xdtreer[id]=xdtreer[id*2+1];
if (xdtreer[id*2]==xdtreel[id*2+1]) xdtrees[id]--;
return;
}
int qjcxx(int l,int r,int id,int L)
{
if (l==r&&r==L)
{
return xdtreel[id];
}
down(id);
int mid=(l+r)>>1,ans=0;
if (L<=mid)
{
ans=qjcxx(l,mid,id*2,L);
}
if (L>mid) ans=qjcxx(mid+1,r,id*2+1,L);
return ans;
}
int qjcx(int l,int r,int id,int L,int R)
{
if (l>=L&&R>=r)
{
return xdtrees[id];
}
down(id);
int mid=(l+r)>>1,ans=0,rr=0;
if (L<=mid)
{
ans=ans+qjcx(l,mid,id*2,L,R);
rr=xdtreer[id*2];
}
if (R>mid)
{
ans=ans+qjcx(mid+1,r,id*2+1,L,R);
if (rr==xdtreel[id*2+1]) ans--;
}
return ans;
}
void treeadd(int x,int y,int z)
{
while (top[x]!=top[y])
{
if (dep[top[x]]<dep[top[y]]) swap(x,y);
qjgx(1,n,1,id[top[x]],id[x],z);
x=fa[top[x]];
}
if (dep[x]<dep[y]) swap(x,y);
qjgx(1,n,1,id[y],id[x],z);
return;
}
int treesum(int x,int y)
{
int ans=0;
while (top[x]!=top[y])
{
if (dep[top[x]]<dep[top[y]]) swap(x,y);
ans=ans+qjcx(1,n,1,id[top[x]],id[x]);
if (qjcxx(1,n,1,id[fa[top[x]]])==qjcxx(1,n,1,id[top[x]])) ans--;
x=fa[top[x]];
}
if (dep[x]<dep[y]) swap(x,y);
ans=ans+qjcx(1,n,1,id[y],id[x]);
return ans;
}
int main()
{
n=read(),m=read();
for (int i=1;i<=n;i++) a[i]=read();
for (int i=1;i<n;i++)
{
x=read(),y=read();
to[++tot]=y;
nxt[tot]=head[x];
head[x]=tot;
to[++tot]=x;
nxt[tot]=head[y];
head[y]=tot;
}
r++;
dfs1(r,0);
tot=0;
dfs2(r,r);
build(1,n,1);
char opt;
while (m--)
{
cin>>opt;
if (opt=='C')
{
x=read(),y=read(),z=read();
treeadd(x,y,z);
}
if (opt=='Q')
{
x=read(),y=read();
cout<<treesum(x,y)<<endl;
}
}
return 0;
}
//Now,only God know