求调左偏树WAon #12
查看原帖
求调左偏树WAon #12
532109
freedom_wang楼主2024/3/16 15:56

rt,悬2关

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define rop(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
#define por(i,a,b) for(int i=a;i>b;i--)
#define endl '\n'
#define pii pair<int,int>
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef double lf;
typedef long double ld;
using namespace std;
int n,m,op,x,y;
struct node{
	int lc,rc,s,fa,dis;
	bool tf;
}t[100005];
int merge(int x,int y){
	if(!x||!y)
		return x|y;
	if(t[y].s<t[x].s)
		swap(x,y);
	t[x].rc=merge(t[x].rc,y);
//	t[t[x].rc].fa=x;
	if(t[t[x].lc].dis<t[t[x].rc].dis)
		swap(t[x].lc,t[x].rc);
	t[x].dis=t[t[x].rc].dis+1;
	return x;
}
int find(int x){
	return(t[x].fa==x)?x:t[x].fa=find(t[x].fa);
}
signed main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n>>m;
	t[0].dis=-1;
	rep(i,1,n){
		cin>>t[i].s;
		t[i].fa=i;
	}
	while(m--){
		cin>>op>>x;
        if(op==1){
            cin>>y;
            if(t[x].tf||t[y].tf)
				continue;
            x=find(x);
			y=find(y);
			if(x!=y)
				t[x].fa=t[y].fa=merge(x,y);
        }
        if(op==2){
            if(t[x].tf){
				cout<<"-1\n";
				continue;
			}
            x=find(x);
            cout<<t[x].s<<endl;
            t[x].tf=1;
            t[t[x].lc].fa=t[t[x].rc].fa=t[x].fa=merge(t[x].lc,t[x].rc);
            t[x].lc=t[x].rc=t[x].dis=0;
        }
	}
	return 0;
}
/*

*/

2024/3/16 15:56
加载中...