Leftist Heap Visualization
#include<bits/stdc++.h>
#define ll long long
#define N 100005
using namespace std;
int n,m,op,x,y;
int ls[N],rs[N],dis[N],v[N];
int fa[N];
int Find(int x)
{
return fa[x]==x?x:fa[x]=Find(fa[x]);
}
int Merge(int x,int y)
{
if(!x||!y) return x+y;
if(v[x]==v[y]?x>y:v[x]>v[y]) swap(x,y);
rs[x]=Merge(rs[x],y);
if(dis[ls[x]]<dis[rs[x]]) swap(ls[x],rs[x]);
dis[x]=dis[rs[x]]+1;
return x;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++) scanf("%d",v+i);
for(int i=1; i<=n; i++) fa[i]=i;
dis[0]=-1;
while(m--)
{
scanf("%d%d",&op,&x);
if(op==1)
{
scanf("%d",&y);
if(!v[x]||!v[y]) continue;
x=Find(x),y=Find(y);
if(x!=y)fa[x]=fa[y]=Merge(x,y);
}
else
{
if(!v[x])
{
printf("-1\n");
continue;
}
x=Find(x);
printf("%d\n",v[x]);
v[x]=0;
fa[ls[x]]=fa[rs[x]]=fa[x]=Merge(ls[x],rs[x]);
}
}
return 0;
}