求助,好像就我最后一个点 TLE 了的
查看原帖
求助,好像就我最后一个点 TLE 了的
566935
timmark楼主2023/8/23 17:45
#include<bits/stdc++.h>
#define int long long 
#define v first
#define id second
using namespace std;
const int maxn=2e5+5;
int n,m,lc[maxn],dis[maxn],rt[maxn],rc[maxn],op,x,y;
bool del[maxn];
pair <int,int> a[maxn];
int merge(int x,int y){
	if(!x || !y) return x+y;
	if(a[x]>a[y]) swap(x,y);
	rc[x]=merge(rc[x],y);
	if(dis[rc[x]]>dis[lc[x]]) swap(dis[lc[x]],dis[rc[x]]);
	dis[x]=dis[rc[x]]+1;
	return x;
}int gf(int x){
	if(rt[x]==x) return x;
	else{
		rt[x]=gf(rt[x]);
		return rt[x];
	}
}signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	dis[0]=-1;
	cin >> n >> m ;
	for(int i=1;i<=n;i++) cin >> a[i].v ,a[i].id=i,rt[i]=i;
	while(m--){
		cin >> op >> x ;
		if(op==1){
			cin >> y ;
			if(del[x] || del[y]) continue;
			x=gf(x),y=gf(y);
			if(x!=y) rt[x]=rt[y]=merge(x,y);
		}else{
			if(del[x]){
				cout << "-1\n" ;
				continue;
			}x=gf(x);
			cout << a[x].first << "\n" ;
			del[x]=1;
			rt[lc[x]]=rt[rc[x]]=rt[x]=merge(lc[x],rc[x]);
			lc[x]=rc[x]=dis[x]=0;
		}
	}return 0;
}
2023/8/23 17:45
加载中...