[WC2013]糖果公园

时间:2023-03-09 19:10:49
[WC2013]糖果公园

Description

题库链接

给你一棵 $n$ 个节点,有 $m$种颜色的树。每个节点上有一个颜色。定义一条树上路径的价值为

$sum_c V_c(\sum_{i=1}^{tim_c}W_i)$

其中 $V,W$已经给出, $tim_c$ 表示路径上 $c$ 颜色的节点数。

现在给出 $q$ 个操作,让你实现:

  1. 修改节点颜色;
  2. 询问树上路径的价值。

$1\leq n,m,q\leq 100000$

转载自Navi_Awson

如果这题出在序列上而不是在树上,很容易用莫队求解。

考虑用类似的方法,我们将树分块,采用[SCOI 2005]王室联邦的方法。

对于树上分块的复杂度和块的大小的选取可以参见ljh的博客,这里摘出了一些:

设 $block_{num}$ 为块数, $block_{size}$ 为块的大小,则有 $block_{num}\times block_{size}=n$ ,在证明中我们假设 $n,q$ 同阶。
设块对 $(block_i,block_j)$ ,易知这样的块对不会超过 $block_{size}^2$ 个。
对于块对内的操作:我们考虑总复杂度,左端点共移动至多 $O(q\times block_{size})$ ,右端点亦是。时间共移动至多 $O(block_{num}^2\times q)$ 。故这一部分的复杂度为 $O(n\times(block_{size}+block_{num}^2))$ 。
对于块与块之间的操作,不超过 $block_{num}^2$ 次:左端第移动一次,最多 $O(n)$ ,右端点亦是如此。时间最多移动 $O(q)=O(n)$ 。故这一部分复杂度为 $O(block_{num}^2\times n)$。
故总复杂度为 $O(n\times(block_{size}+block_{num}^2))$。
可以证明当 $block_{size}=n^{\frac{2}{3}}$ 时, $block_{num}=n^{\frac{1}{3}}$ ,复杂度最优,为 $O(n^{\frac{5}{3}})$ 。

至于莫队的操作,就是将序列中移动左右端点变成在树上移动路径的两个端点。对于修改,我们同样是模拟时间倒流和消逝。

那么怎么去移动结点来保证提取出路径?

考虑我们树上的所有结点,实际上可以认为是 $0/1$ 状态——计入答案或者未计入答案。

考虑用类似于异或的思想来执行操作,比如:计入答案再从答案中去掉,等于异或了两次 $1$ ,就等于原来的数。假设这次的起点、终点为 $u,v$ ,上次为 $x,y$ ,那么可以对 $x$ 到 $u$ 的路径、 $v$ 到 $y$ 的路径进行一次取 $xor$ 操作。注意的是对 $lca$ 不做处理,这样就能保证每次操作之后图上打上标记的点只在 $(u,lca)$和 $(v,lca)$ 的路径上(不包括 $lca$ )。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long lol;
struct ZYYS
{
int x,y,p,id;
}p[],q[];
struct Node
{
int next,to;
}edge[];
int num,head[],block[],tot,size[],son[],block_size,block_num,cnt,s[];
int top[],dfn[],dep[],fa[],rev[],c[],n,m,Q,pre[],cntp,cntq;
int curl,curr,curp;
lol sum,v[],w[],ans[];
bool cmp(ZYYS a,ZYYS b)
{
if (block[a.x]==block[b.x])
{
if (block[a.y]==block[b.y]) return a.id<b.id;
else return block[a.y]<block[b.y];
}
else return block[a.x]<block[b.x];
}
void add(int u,int v)
{
num++;
edge[num].next=head[u];
head[u]=num;
edge[num].to=v;
}
void dfs1(int x,int pa)
{int i;
int bot=tot;
fa[x]=pa;
size[x]=;
dep[x]=dep[pa]+;
for (i=head[x];i;i=edge[i].next)
{
int v=edge[i].to;
if (v==pa) continue;
dfs1(v,x);
if (size[son[x]]<size[v]) son[x]=v;
size[x]+=size[v];
if (tot-bot>=block_size)
{
++cnt;
while (tot>bot)
{
block[s[tot--]]=cnt;
}
}
}
s[++tot]=x;
}
void dfs2(int x,int pa,int tp)
{int i;
top[x]=tp;
dfn[x]=++tot;
if (son[x]) dfs2(son[x],x,tp);
for (i=head[x];i;i=edge[i].next)
{
int v=edge[i].to;
if (v==pa||v==son[x]) continue;
dfs2(v,x,v);
}
}
int get_lca(int x,int y)
{
while (top[x]!=top[y])
{
if (dep[top[x]]<dep[top[y]]) swap(x,y);
x=fa[top[x]];
}
if (dep[x]<dep[y]) return x;
return y;
}
void reverse(int x)
{
if (rev[x]) sum-=v[c[x]]*w[s[c[x]]],s[c[x]]--;
else s[c[x]]++,sum+=v[c[x]]*w[s[c[x]]];
rev[x]^=;
}
void update(int x,int y)
{
if (rev[x]) reverse(x),c[x]=y,reverse(x);
else c[x]=y;
}
void move(int x,int y)
{
while (x!=y)
{
if (dep[x]<dep[y]) swap(x,y);
reverse(x);
x=fa[x];
}
}
int main()
{int i,uu,vv,opt,x,y;
cin>>n>>m>>Q;
for (i=;i<=m;i++)
scanf("%lld",&v[i]);
for (i=;i<=n;i++)
scanf("%lld",&w[i]);
block_size=pow(n,0.666667);
block_num=n/block_size;
for (i=;i<n;i++)
{
scanf("%d%d",&uu,&vv);
add(uu,vv);add(vv,uu);
}
for (i=;i<=n;i++)
{
scanf("%d",&c[i]);
pre[i]=c[i];
}
dfs1(,);
++cnt;
while (tot)
{
block[s[tot--]]=cnt;
}
tot=;
memset(s,,sizeof(s));
dfs2(,,);
for (i=;i<=Q;i++)
{
scanf("%d",&opt);
if (opt==)
{
scanf("%d%d",&x,&y);
p[++cntp]=(ZYYS){x,y,pre[x],};
pre[x]=y;
}
else
{
scanf("%d%d",&x,&y);
q[++cntq]=(ZYYS){x,y,cntp,cntq};
}
}
sort(q+,q+cntq+,cmp);
curl=;curr=;curp=;
for (i=;i<=cntq;i++)
{
while (curp<q[i].p) {curp++;update(p[curp].x,p[curp].y);}
while (curp>q[i].p) {update(p[curp].x,p[curp].p);curp--;}
move(curl,q[i].x);curl=q[i].x;
move(curr,q[i].y);curr=q[i].y;
int lca=get_lca(curl,curr);
reverse(lca);
ans[q[i].id]=sum;
reverse(lca);
}
for (i=;i<=cntq;i++)
{
printf("%lld\n",ans[i]);
}
}