跳到正文

题解

题解 P3206 【[HNOI2010]城市建设】

KiDDOwithTopTree约 10 分钟
原文
文章信息与版本

文章信息

发布
2021年5月22日
收录
2026年8月24日
更新
2026年11月15日
篇幅
约 10 分钟
获赞
2
题目
P3206

版本

  1. v1当前

题目在这:传送门。

根据题意,我们可以知道其实是要求出一个最小生成树。

因为有了边权的修改,其实这是一个动态最小生成树。

因为边权修改不好看,我们可以想想怎么转换:

假设我们有一条从 xx 到 yy 边权为 valval 的边,然后把边权改为 val′val^\prime。那么我们可以把它看成是把从 xx 到 yy 边权为 valval 的边删去,再加入一条从 xx 到 yy 边权为 val′val^\prime 的边。这样我们就把修改边权转为了加边和删边。

如果是只加边的话其实就是 LCT\text{LCT} 求最小生成树的模板,但是多了一个删边的操作就不能这样求最小生成树了。

怎么办呢?线段树分治,你值得拥有。

注意到此题可以离线,那么就可以考虑线段树分治。我们建立一棵线段树来维护时间轴,然后把边从加入到删除这一段时间当做边存在的时间,接着把这段存在的时间插入进线段树里,最后遍历一遍线段树计算答案即可。

时复为 O(mlog⁡qlog⁡n)O(m \log q \log n),需要晚上吸氧。本人用这份代码跑了最劣解。

放上代码,一些易错点和计算答案方法都记录在代码注释里了。

C++
#include<iostream>
#include<vector>
using namespace std;
const int N=2e4+10,M=5e4+10;
struct node{
	int x,y,z;
	int pos;
	int t;
	node(){
		t=1;
	}
};
struct node1{
	int x,y,z;
	int pos;
};
struct node2{
	int x,y,z;
	int val;
};
struct segment_tree{
	vector<node1> v;
};
struct splay{
	int fa,son[2];
	int maxn,val;
	int pos;
	int rev;
	int x,y;
};
node a[M];//边数
segment_tree smt[4*M];//四倍空间
splay spl[N+2*M];//空间为点数加边数加修改次数
node2 sta[N+2*M];//这个空间我不知道…反正写了这个能对
int top;
long long ans=0;
inline bool check_root(int pos){
	return spl[spl[pos].fa].son[0]!=pos&&spl[spl[pos].fa].son[1]!=pos;
}
inline int relation(int pos,int fa){
	return spl[fa].son[1]==pos;
}
inline void connect(int pos,int fa,int son){
	spl[pos].fa=fa,spl[fa].son[son]=pos;
}
inline void pushup(int pos){
	int ls=spl[pos].son[0],rs=spl[pos].son[1];
	spl[pos].pos=pos,spl[pos].maxn=spl[pos].val;
	if(spl[ls].maxn>spl[pos].maxn){
		spl[pos].maxn=spl[ls].maxn;
		spl[pos].pos=spl[ls].pos;
	}
	if(spl[rs].maxn>spl[pos].maxn){
		spl[pos].maxn=spl[rs].maxn;
		spl[pos].pos=spl[rs].pos;
	}
}
inline void pushdown(int pos){
	if(!spl[pos].rev) return ;
	int ls=spl[pos].son[0],rs=spl[pos].son[1];
	swap(spl[pos].son[0],spl[pos].son[1]);
	spl[ls].rev^=1,spl[rs].rev^=1,spl[pos].rev=0;
}
inline void pushall(int pos){
	if(!check_root(pos)) pushall(spl[pos].fa);
	pushdown(pos);
}
void rotate(int pos){
	int fa=spl[pos].fa,gf=spl[fa].fa;
	int t1=relation(pos,fa),t2=relation(fa,gf);
	connect(spl[pos].son[t1^1],fa,t1);
	connect(fa,pos,t1^1);
	spl[pos].fa=gf;
	if(spl[gf].son[t2]==fa)
		spl[gf].son[t2]=pos;
	pushup(fa),pushup(pos);
}
void splaying(int pos){
	pushall(pos);
	while(!check_root(pos)){
		int fa=spl[pos].fa,gf=spl[fa].fa;
		if(!check_root(fa))
			relation(pos,fa)^relation(fa,gf)?rotate(pos):rotate(fa);
		rotate(pos);
	}
}
void access(int pos){
	int son=0;
	while(pos){
		splaying(pos);
		spl[pos].son[1]=son;
		pushup(pos);
		son=pos,pos=spl[pos].fa;
	}
}
void make_root(int pos){
	access(pos);
	splaying(pos);
	spl[pos].rev^=1;
}
int find_root(int pos){
	access(pos);
	splaying(pos);
	pushdown(pos);
	while(spl[pos].son[0]){
		pos=spl[pos].son[0];
		pushdown(pos);
	}
	splaying(pos);
	return pos;
}
void split(int x,int y){
	make_root(x);
	access(y);
	splaying(y);
}
void link(int x,int y){
	make_root(x);
	spl[x].fa=y;
}
void cut(int x,int y){
	make_root(x);
	access(y);
	splaying(y);
	spl[y].son[0]=spl[x].fa=0;
	pushup(y);
}
bool check(int x,int y){
	make_root(x);
	return find_root(y)==x;
}
//以上为LCT
void ins(int pos,node1 val,int vl,int vr,int l,int r){
	if(vl<=l&&vr>=r){
		smt[pos].v.push_back(val);
		return ;
	}
	int mid=l+r>>1;
	if(vl<=mid) ins(pos<<1,val,vl,vr,l,mid);
	if(vr>mid) ins(pos<<1|1,val,vl,vr,mid+1,r);
}
void solve(int pos,int l,int r){
	int last=top;
	node1 val;
	node2 tmp;
	int len=smt[pos].v.size();
	int x,y,z,p;
	int xx,yy,q;
	for(int i=0;i<len;i++){
		val=smt[pos].v[i];
		x=val.x,y=val.y,z=val.z,p=val.pos;
		if(check(x,y)){//判断是否联通
			splaying(y);
			if(spl[y].maxn<z) continue;//既不link又不cut
			q=spl[y].pos;
			xx=spl[q].x,yy=spl[q].y;
			cut(xx,q),cut(yy,q);//断边
			tmp.x=xx,tmp.y=yy,tmp.z=q,tmp.val=spl[q].val;//记录操作
			sta[++top]=tmp;
			ans-=tmp.val;//把边的贡献删除
		}
		link(x,p),link(p,y);//连边
		tmp.x=x,tmp.y=y,tmp.z=p,tmp.val=-z;//记录操作
		sta[++top]=tmp;
		ans-=tmp.val;//负负得正,加上贡献
	}
	if(l==r){
		if(l!=0)//注意判断,l=0时即为初始状态,此时不用输出
			cout<<ans<<'\n';
	}
	else{//左右分治
		int mid=l+r>>1;
		solve(pos<<1,l,mid);
		solve(pos<<1|1,mid+1,r);
	}
	while(last<top){//撤销操作
		tmp=sta[top--];
		if(tmp.val<0) cut(tmp.x,tmp.z),cut(tmp.y,tmp.z);
		else link(tmp.x,tmp.z),link(tmp.z,tmp.y);
		ans+=tmp.val;
	}
}
//以上为线段树分治部分
int main(){
	int n,m,k;//k为题目的q
	cin>>n>>m>>k;
	for(int i=1;i<=n;i++) spl[i].pos=i;
	int t=n;
//a[i].t初始为0
	for(int i=1;i<=m;i++){
		a[i].pos=++t;
		cin>>a[i].x>>a[i].y>>a[i].z;
		spl[t].x=a[i].x,spl[t].y=a[i].y;//记录下这条边连接的两个点
		spl[t].pos=t,spl[t].maxn=spl[t].val=a[i].z;
	}
	node1 val;
	for(int i=1;i<=k;i++){
		int x,y;
		cin>>x>>y;
		val.pos=a[x].pos;
		a[x].pos=++t;
		val.x=a[x].x,val.y=a[x].y,val.z=a[x].z;
		ins(1,val,a[x].t,i-1,0,k);//上一条边存在的时间没有到i
		a[x].t=i,a[x].z=y;
		spl[t].x=a[x].x,spl[t].y=a[x].y;//记录下这条边连接的两个点
		spl[t].pos=t,spl[t].maxn=spl[t].val=a[x].z;
	}
	for(int i=1;i<=m;i++){//边最后没有删除,那么就算到k时结束
		val.x=a[i].x,val.y=a[i].y,val.z=a[i].z;
		val.pos=a[i].pos;
		ins(1,val,a[i].t,k,0,k);
	}
	solve(1,0,k);//计算结果
}
原文
存档评论3 条抓取于