MLE on #1(hack 数据) 求调
查看原帖
MLE on #1(hack 数据) 求调
392157
Little_CartG2_leaf楼主2024/5/1 11:19

rt,以下为代码

#include<bits/stdc++.h>
using namespace std;
const int N=1000024,M=2000024;
int t,n,m,tot,ans,op,x,y;
int a[N],dep[N],fa[N],rs[N],ls[N];
bool dead[N];
int findfa(int x){
	if(x==fa[x]){
		return x;
	}
	return fa[x]=findfa(fa[x]);
}
int merge(int x,int y){
	if(x==0||y==0){
		return x|y;
	}
	if(a[x]>a[y]||(a[x]==a[y]&&x>y)){
		swap(x,y);
	}
	rs[x]=merge(rs[x],y);
	if(dep[rs[x]]>dep[ls[x]]){
		swap(ls[x],rs[x]);
	}
	dep[x]=dep[rs[x]]+1;
	return x;
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	freopen("lft.in","r",stdin);
	freopen("lft.txt","w",stdout);
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		fa[i]=i;
		dep[i]=0;
	}
	while(m--){
		cin>>op>>x;
		if(op==1){
			cin>>y;
			if(dead[x]||dead[y]){
				continue;
			}
			x=findfa(x);
			y=findfa(y);
			fa[x]=fa[y]=merge(x,y);
		}
		else{
			if(dead[x]){
				cout<<"-1\n";
				continue;
			}
			x=findfa(x);
			cout<<a[x]<<"\n";
			dead[x]=1;
			fa[ls[x]]=fa[rs[x]]=fa[x]=merge(ls[x],rs[x]);
			ls[x]=rs[x]=dep[x]=0;
		}
	}
	return 0;
}

据我所知,我的代码在 hack 数据中出现了 x==rs[x] 的情况,但是又不知道是怎么出现的,所以到底是怎么绘事呢

2024/5/1 11:19
加载中...