咕了很久的左偏树,具体思想待咕咕
#include<bits/stdc++.h> using namespace std; int n,m,f[100010],flg[100010],a,b,opt,lc[100010],rc[100010],d[100010]; struct node{int val,id;}num[100010]; int find(int x){return f[x]==x?x:f[x]=find(f[x]);} int merge(int x,int y) { if(!x||!y)return x+y; if(num[x].val>num[y].val||(num[x].val==num[y].val&&num[x].id>num[y].id)) swap(x,y); rc[x]=merge(rc[x],y); if(d[lc[x]]<d[lc[y]])swap(lc[x],lc[y]); d[x]=d[rc[x]]+1; return x; } int main() { cin>>n>>m; for(int i=1;i<=n;i++){f[i]=i;cin>>num[i].val;num[i].id=i;} while(m--) { cin>>opt>>a; if(opt==1) { cin>>b; if(flg[a]||flg[b])continue; int x=find(a),y=find(b); if(x!=y)f[x]=f[y]=merge(x,y); } else{ if(flg[a]){cout<<-1<<endl;continue;} int x=find(a); cout<<num[x].val<<endl; flg[x]=1; f[lc[x]]=f[rc[x]]=f[x]=merge(lc[x],rc[x]); lc[x]=rc[x]=d[x]=0; } } }