跳到正文

题解

P3313 solution

4C45约 9 分钟
原文
文章信息与版本

文章信息

发布
2026年8月18日
收录
2026年8月18日
更新
2026年11月21日
篇幅
约 9 分钟
获赞
2
题目
P3313

版本

  1. v2当前
  2. v1

upd:补了一点说明。

走了一圈题解区怎么都是动态开点。

显然暴力可以对每个 CC 开一个线段树来维护。但是这样空间会爆炸。我们尝试在同一棵线段树上完成所有操作。

单独考虑 Cu=xC_u=x 的 uu 组成的树。考虑一个 CC 操作实际上意味着什么。只有当切换 CuC_u 为 xx 时点 uu 才会被计入树 xx 贡献的计算。

把一次 CC 操作拆成前后两部分:将 CuC_u 改为 00,将 CuC_u 改为 yy。也就是说,我们在 CuC_u 树上去除了点 uu 对应的 wuw_u 权值(可以看作设为 00),然后在 yy 树上设置 wuw_u 权值。

两棵树之间是没有关系的,所以先把题目里面的更改操作跑一遍,记录什么时候某棵树上进行了一次询问或将某个点的权值改为 yy,按时间顺序把操作记录到每棵树的操作列表,依次处理每棵树。由于 11 次 CC 操作只能拆出 22 个赋值操作,再加上起始时加入树和结束时退出树的赋值,最终的操作数量是 O(n+q)\mathcal O(n+q) 的。一次跳树链求答案复杂度是 O(log⁡2n)\mathcal O(\log^2 n),赋值操作的复杂度是 O(log⁡n)\mathcal O(\log n),最终复杂度是 O(nlog⁡n+qlog⁡2n)\mathcal O(n\log n+q\log^2 n)。

P3313
C++
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ir(i,a,b) for(int i=b;i>=a;i--)
#define db double
#define ld long double
#define YES cout<<"YES\n"
#define Yes cout<<"Yes\n"
#define NO cout<<"NO\n"
#define No cout<<"No\n"
#define re return
#define len(str) (str.length())
#define inr(L,R,l,r) (l<=L and R<=r)
#define ofr(L,R,l,r) (L>r or l>R)
#define lowbit(x) (x&(-x))
#define tn2 tuple<node*,node*>
#define tn3 tuple<node*,node*,node*>
#define mt make_tuple
#define np nullptr
#define ioo cin.tie(0)->sync_with_stdio(0);cout.tie(0)->sync_with_stdio(0);
#define popc __builtin_popcount
using namespace std;
const int maxn=1e5+114;
struct Node
{
	int w,mx;
	Node()
	{
		w=mx=0;
	}
} t[maxn<<2];
struct Opt
{
	int typ,x,y,qid;
	Opt(int tt,int xx,int yy,int qq): typ(tt),x(xx),y(yy),qid(qq){}
};
vector<Opt> opt[maxn];
int w[maxn],c[maxn],n,q;
int head[maxn],nxt[maxn*2],to[maxn*2],tot;
bool isq[maxn];int ans[maxn];
int dfn[maxn],dfl[maxn],wc[maxn],siz[maxn],dep[maxn],top[maxn],fu[maxn],ts;
bool vis[maxn];
void add(int u,int v)
{
	nxt[++tot]=head[u];head[u]=tot;
	to[tot]=v;
}
inline void dfs(int u,int fa)
{
	siz[u]=1;dep[u]=dep[fa]+1;fu[u]=fa;
	for(int i=head[u],v;i;i=nxt[i])
	{
		v=to[i];
		if(v==fa) continue;
		dfs(v,u);
		siz[u]+=siz[v];
		if(siz[v]>siz[wc[u]]) wc[u]=v;
	}
}
inline void chain(int u,int Top)
{
	top[u]=Top;dfn[u]=++ts;dfl[ts]=u;
	if(wc[u]) chain(wc[u],Top);
	for(int i=head[u],v;i;i=nxt[i])
	{
		v=to[i];
		if(v==fu[u] or v==wc[u]) continue;
		chain(v,v);
	}
}
Node pushup(Node a,Node b)
{
	Node c;
	c.w=a.w+b.w;
	c.mx=max(a.mx,b.mx);
	re c;
}
inline void update(int u,int L,int R,int k,int v)
{
	if(L==R)
	{
		t[u].w=t[u].mx=v;
	}else{
		int M=L+R>>1;
		if(k<=M) update(2*u,L,M,k,v);
		else update(2*u+1,M+1,R,k,v);
		t[u]=pushup(t[2*u],t[2*u+1]);
	}
}
inline int qmax(int u,int L,int R,int l,int r)
{
	if(inr(L,R,l,r)) re t[u].mx;
	else 
	{
		int M=L+R>>1,ans=0;
		if(l<=M) ans=max(ans,qmax(2*u,L,M,l,r));
		if(r>M) ans=max(ans,qmax(2*u+1,M+1,R,l,r));
		re ans;
	}
}
inline int qsum(int u,int L,int R,int l,int r)
{
	if(inr(L,R,l,r)) re t[u].w;
	else{
		int M=L+R>>1,ans=0;
		if(l<=M) ans=ans+qsum(2*u,L,M,l,r);
		if(r>M) ans=ans+qsum(2*u+1,M+1,R,l,r);
		re ans;
	}
}
inline int chainsum(int u,int v)
{
	int ans=0;
	while(top[u]!=top[v])
	{
		if(dep[top[u]]>dep[top[v]]) ans=ans+qsum(1,1,n,dfn[top[u]],dfn[u]),u=fu[top[u]];
		else ans=ans+qsum(1,1,n,dfn[top[v]],dfn[v]),v=fu[top[v]];
	}
	ans=ans+qsum(1,1,n,min(dfn[u],dfn[v]),max(dfn[u],dfn[v]));
	re ans;
}
inline int chainmax(int u,int v)
{
	int ans=0;
	while(top[u]!=top[v])
	{
		if(dep[top[u]]>dep[top[v]]) ans=max(ans,qmax(1,1,n,dfn[top[u]],dfn[u])),u=fu[top[u]];
		else ans=max(ans,qmax(1,1,n,dfn[top[v]],dfn[v])),v=fu[top[v]];
	}
	ans=max(ans,qmax(1,1,n,min(dfn[u],dfn[v]),max(dfn[u],dfn[v])));
	re ans;
}
int main()
{
	ioo;
	cin>>n>>q;
	rep(i,1,n) 
	{
		cin>>w[i]>>c[i];
		opt[c[i]].push_back(Opt(1,i,w[i],0));
	}
	rep(i,1,n-1)
	{
		int u,v;
		cin>>u>>v;
		add(u,v);add(v,u);
	}
	rep(i,1,q)
	{
		char p,q;int x,y;
		cin>>p>>q>>x>>y;
		if(q=='C')
		{
			opt[c[x]].push_back(Opt(1,x,0,i));
			c[x]=y;
			opt[y].push_back(Opt(1,x,w[x],i));
		}else if(q=='W')
		{
			opt[c[x]].push_back(Opt(1,x,y,i));
			w[x]=y;
		}else if(q=='S')
		{
			isq[i]=1;
			opt[c[x]].push_back(Opt(2,x,y,i));
		}else
		{
			isq[i]=1;
			opt[c[x]].push_back(Opt(3,x,y,i));
		}
	}
	rep(i,1,n) opt[c[i]].push_back(Opt(1,i,0,n+1));
	dfs(1,0);
	chain(1,1);
	rep(i,1,100000)
	{
		if(opt[i].empty()) continue;
		for(auto o:opt[i])
		{
			int typ,x,y,qid;
			typ=o.typ;x=o.x;y=o.y;qid=o.qid;
			if(typ==1)
			{
				update(1,1,n,dfn[x],y);
			}else if(typ==2)
			{
				ans[qid]=chainsum(x,y);
			}else{
				ans[qid]=chainmax(x,y);
			}
		}
	}
	rep(i,1,q) if(isq[i]) cout<<ans[i]<<'\n';
}
原文