跳到正文

题解

P12693 BZOJ3589 动态树

Harekaze约 7 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 7 分钟
题目
P12693

版本

  1. v2当前
  2. v1

1. 题目分析

对于事件零,很好维护,暂且搁在一边。

重点还是事件一,难点在于如何将多个区间的重复部分去除。不难发现,我们可以树链剖分将整棵树分成一段一段的区间。对于多个区间,区间合并即可。也就是按左端点排序,将多个重复的区间连成一段,具体可参考这道题:区间覆盖(加强版)。当然区间覆盖在[CSP-S 2024] 超速检测中也有所涉及。

复杂度是 O(Qklog⁡2n)O(Qk\log^2n) 的,瓶颈在于区间合并操作。

2. 代码

C++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+5,P=1<<31;
ll siz[N],son[N],top[N];
ll idx,cnt,h[N],p[N],dep[N],dfn[N];
struct node{ll l,r,w,lz;};
struct edge{ll u,v,nxt;}e[2*N];
inline void add(ll u,ll v){e[++idx]={u,v,h[u]},h[u]=idx;}
struct SEG
{
	ll n;
	vector<node>t;
	SEG(ll x):n(x),t(x*4+5){}
	inline void pushup(ll x){t[x].w=(t[x*2].w+t[x*2+1].w)%P;}
	inline void pushdown(ll x)
	{
		t[x*2].w=(t[x*2].w+(t[x*2].r-t[x*2].l+1)*t[x].lz)%P;
		t[x*2+1].w=(t[x*2+1].w+(t[x*2+1].r-t[x*2+1].l+1)*t[x].lz)%P;
		t[x*2].lz=(t[x*2].lz+t[x].lz)%P,t[x*2+1].lz=(t[x*2+1].lz+t[x].lz)%P;
		t[x].lz=0;
	}
	void build(ll x,ll l,ll r)
	{
		t[x].l=l;
		t[x].r=r;
		t[x].w=t[x].lz=0;
		if(l==r)return;
		ll mid=(l+r)>>1;
		build(x*2,l,mid);
		build(x*2+1,mid+1,r);
		pushup(x);
	}
	void update(ll x,ll l,ll r,ll k)
	{
		if(t[x].r<l||t[x].l>r)return;
		if(t[x].l>=l&&t[x].r<=r)
		{
			t[x].lz=(t[x].lz+k)%P;
			t[x].w=(t[x].w+(t[x].r-t[x].l+1)*k)%P;
			return;
		}
		if(t[x].lz)pushdown(x);
		ll mid=(t[x].l+t[x].r)>>1;
		if(l<=mid)update(x*2,l,r,k);
		if(r>mid)update(x*2+1,l,r,k);
		pushup(x);
	}
	ll query(ll x,ll l,ll r)
	{
		if(t[x].r<l||t[x].l>r)return 0;
		if(t[x].l>=l&&t[x].r<=r)return t[x].w;
		if(t[x].lz)pushdown(x);
		ll ans=0,mid=(t[x].l+t[x].r)>>1;
		if(l<=mid)ans=(ans+query(x*2,l,r))%P;
		if(r>mid)ans=(ans+query(x*2+1,l,r))%P;
		return ans;
	}
};
void dfs1(ll u,ll fa)
{
	p[u]=fa,dep[u]=dep[fa]+1,siz[u]=1;
	for(ll i=h[u];i!=-1;i=e[i].nxt)
	{
		ll v=e[i].v;
		if(v==fa)continue;
		dfs1(v,u);
		siz[u]+=siz[v],p[v]=u;
		if(!son[u]||siz[son[u]]<siz[v])son[u]=v;
	}
}
void dfs2(ll u,ll t)
{
	top[u]=t,dfn[u]=++cnt;
	if(!son[u])return;
	dfs2(son[u],t);
	for(ll i=h[u];i!=-1;i=e[i].nxt)
	{
		ll v=e[i].v;
		if(v!=p[u]&&v!=son[u])dfs2(v,v);
	}
}
void update(SEG &t,ll u,ll v,ll k)
{
	while(top[u]!=top[v])
	{
		if(dep[top[u]]<dep[top[v]])swap(u,v);
		t.update(1,dfn[top[u]],dfn[u],k);
		u=p[top[u]];
	}
	if(dep[u]>dep[v])swap(u,v);
	t.update(1,dfn[u],dfn[v],k);
}
//需要合并的区间
vector<pair<ll,ll>>query(ll u,ll v)
{
	vector<pair<ll,ll>>res;
	while(top[u]!=top[v])
	{
		if(dep[top[u]]<dep[top[v]])swap(u,v);
		res.emplace_back(dfn[top[u]],dfn[u]);
		u=p[top[u]];
	}
	if(dep[u]>dep[v])swap(u,v);
	res.emplace_back(dfn[u],dfn[v]);
	return res;
}
int main()
{
	ll n,q;
	scanf("%lld",&n);
	memset(h,-1,sizeof(h));
	for(ll i=1;i<n;i++)
	{
		ll u,v;
		scanf("%lld%lld",&u,&v);
		add(u,v),add(v,u);
	}
	SEG t(n);
	t.build(1,1,n);
	dfs1(1,0),dfs2(1,1);
	scanf("%lld",&q);
	while(q--)
	{
		ll op,u,v,k;
		scanf("%lld",&op);
		if(op==1)
		{
			scanf("%lld",&k);
			vector<pair<ll,ll>>MAS;
			while(k--)
			{
				scanf("%lld%lld",&u,&v);
				if(dep[u]<dep[v])swap(u,v);
				auto res=query(u,v);
				MAS.insert(MAS.end(),res.begin(),res.end());
			}
			//以下为区间合并操作
			//pair按第一关键字排序,也就是左端点排序
			sort(MAS.begin(),MAS.end());
			ll l=0,r=0,ans=0;
			for(auto x:MAS)
			{
				if(x.first>r)//不能连在一起就新开一段
				{
					if(l)ans+=t.query(1,l,r);
					l=x.first,r=x.second;
				}
				else r=max(r,x.second);//连在一起
			}
			if(l)ans+=t.query(1,l,r);
			printf("%lld\n",ans%P);
		}
		else scanf("%lld%lld",&u,&v),t.update(1,dfn[u],dfn[u]+siz[u]-1,v);
	}
	return 0;
}
原文