本文主要是介绍左偏树【待施工】,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int fa[N],ls[N],rs[N],dist[N],val[N],id[N];
bool del[N];
int n,m,cnt;
int get(int x)
{
if(x == fa[x])return x;
return fa[x] = get(fa[x]);
}
struct leftist
{
int id,val;
bool operator<(leftist x)const{return val == x.val?id<x.id:val < x.val;}
}t[N];
int mer(int x,int y)
{
if(!x||!y)return x|y;
if(t[y]<t[x])swap(x,y);
rs[x] = mer(rs[x],y);
if(dist[ls[x]] < dist[rs[x]])swap(ls[x],rs[x]);
dist[x] = dist[rs[x]] + 1;
return x;
}
int main()
{
cin >> n >> m;
dist[0] = -1;
for(int i = 1 ; i <= n ; i ++ )
{
scanf("%d",&t[i].val);
fa[i] = i;
t[i].id = i;
}
for(int i = 1 ; i <= m ; i ++ )
{
int x,y,op;
scanf("%d%d",&op,&x);
if(op == 1)
{
scanf("%d",&y);
if(del[x]||del[y])continue;
x = get(x),y = get(y);
if(x != y)fa[x] = fa[y] = mer(x,y);
}
else {
if(del[x]){puts("-1");continue;}
x = get(x);
printf("%d\n",t[x].val);
del[x] = 1;
fa[ls[x]] = fa[rs[x]] = fa[x] = mer(ls[x],rs[x]);
ls[x] = rs[x] = dist[x] = 0;
}
}
}
这篇关于左偏树【待施工】的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!