社区讨论

何意味

P3377【模板】可并堆 1参与者 39已保存回复 44

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
43 条
当前快照
1 份
快照标识符
@mkqmuki3
此快照首次捕获于
2026/01/23 16:42
4 周前
此快照最后确认于
2026/01/23 22:00
4 周前
查看原帖
00 号节点的 dist\operatorname{dist} 置为 1-1 的这一行删掉,为啥还能过?
CPP
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
struct left_heap{
	struct nd{
		int val,id;
		int ls,rs,d;
		int fa,e;
	}f[N];
	void init(int n,int a[]){
		for(int i=1;i<=n;i++){
			f[i].val=a[i];
			f[i].id=i;
			f[i].d=0;
			f[i].fa=i;
		}
		f[0].d=-1;
	}
	int mer(int x,int y){
		if(!x) return y;
		if(!y) return x;
		if(f[x].val>f[y].val||(f[x].val==f[y].val&&f[x].id>f[y].id)) swap(x,y);
		f[x].rs=mer(f[x].rs,y);
		if(f[f[x].ls].d<f[f[x].rs].d) swap(f[x].ls,f[x].rs);
		f[x].d=f[f[x].rs].d+1;
		return x;
	}
	int getfa(int x){
		if(x==f[x].fa) return x;
		return f[x].fa=getfa(f[x].fa);
	}
	void upd(int x,int y){
		if(f[x].e||f[y].e) return;
		x=getfa(x);
		y=getfa(y);
		if(x==y) return;
		f[x].fa=f[y].fa=mer(x,y);
	}
	void rep(int x){
		if(f[x].e){
			cout<<"-1\n";
			return;
		}
		x=getfa(x);
		cout<<f[x].val<<"\n";
		int y=mer(f[x].ls,f[x].rs);
		if(f[x].ls) f[f[x].ls].fa=y;
		if(f[x].rs) f[f[x].rs].fa=y;
		f[x].fa=f[y].fa=y;
		f[x].e=1;
	}
}t;
int n,a[N],q;
signed main(){
	ios::sync_with_stdio(0),cin.tie(0);
	cin>>n>>q;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	t.init(n,a);
	while(q--){
		int op,x,y;
		cin>>op;
		if(op==1){
			cin>>x>>y;
			t.upd(x,y);
		}
		else{
			cin>>x;
			t.rep(x);
		}
	}
}

回复

44 条回复,欢迎继续交流。

正在加载回复...