屑萌新求助左偏树
查看原帖
屑萌新求助左偏树
239895
Yusani_huh楼主2021/4/13 23:42

第 n 次调不出 bug 了...

感觉自己代码能力下降了

#include<bits/stdc++.h>
using namespace std;
int n,m,h[1000003];
bool f[1000003];
struct node{
	int nm,l,r,ds;
}t[1000003];
int fd(int a){  //并查集路径压缩
	if(h[a]!=a) h[a]=fd(h[a]);
	return h[a];
}
int hb(int x,int y){  //合并操作
	if(!x||!y) return x+y;
	if(t[x].nm>t[y].nm) swap(x,y);
	t[x].r=hb(t[x].r,y);
	if(t[t[x].l].ds<t[t[x].r].ds)
		swap(t[x].l,t[x].r);
	t[x].ds=t[t[x].r].ds+1;
	return x;
}
int main(){
	scanf("%d",&n);
	t[0].ds=-1;
	for(int i=1;i<=n;++i)
		scanf("%d",&t[i].nm),h[i]=i;
	scanf("%d",&m);
	char op=0;
	int x=0,y=0;
	for(int i=1;i<=m;++i){
		cin>>op;
		scanf("%d",&x);
		if(op=='M'){  //操作一,合并
			scanf("%d",&y);
			if(f[x]||f[y]) continue;
			int a=fd(x),b=fd(y);
			if(a!=b) h[x]=h[y]=hb(x,y);
		}
		else if(op=='K'){  //操作二,删除
			if(f[x]){
				printf("0\n");
				continue;
			}
			x=fd(x),f[x]=true;
			printf("%d\n",t[x].nm);
			h[x]=h[t[x].l]=h[t[x].r]
				=hb(t[x].l,t[x].r);
			t[x].l=t[x].r=t[x].ds=0;
		}
	}
	return 0;
}

不过更有可能是低级错误什么的,所以提前为浪费了各位时间而感到抱歉 /dk

2021/4/13 23:42
加载中...