左偏树(可并堆)

洛谷3377

#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define ls tr[x].Son[0]
#define rs tr[x].Son[1]
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define pb push_back
#define mk make_pair
using namespace std;

typedef long long LL;
typedef pair<int,int> PII;

const int N=500010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;

int n,m;
struct Node
{
    int dis,val,rot;
    int Son[2];
}tr[N];

int find(int x)
{
    return x==tr[x].rot? x:tr[x].rot=find(tr[x].rot);
}

int merge(int x,int y)
{
    if(!x||!y) return x+y;
    if(tr[x].val>tr[y].val||(tr[x].val==tr[y].val&&x>y)) swap(x,y);
    rs=merge(rs,y);
    if(tr[ls].dis<tr[rs].dis) swap(ls,rs);
    tr[ls].rot=tr[rs].rot=tr[x].rot=x;
    tr[x].dis=tr[rs].dis+1;
    return x;
}

void pop(int x)
{
    tr[x].val=-1;
    tr[ls].rot=ls,tr[rs].rot=rs;
    tr[x].rot=merge(ls,rs);
}

int main()
{
//	ios::sync_with_stdio(false);
//	cin.tie(0);

    scanf("%d%d",&n,&m); tr[0].dis=-1;
    for(int i=1;i<=n;i++) scanf("%d",&tr[i].val),tr[i].rot=i;
    while(m--)
    {
        int op,x,y; scanf("%d%d",&op,&x);
        if(op==1)
        {
            scanf("%d",&y);
            if(tr[x].val==-1||tr[y].val==-1) continue;
            int px=find(x),py=find(y);
            if(px!=py) tr[px].rot=tr[py].rot=merge(px,py);
        }
        else
        {
            if(tr[x].val==-1) puts("-1");
            else printf("%d\n",tr[find(x)].val),pop(find(x));
        }
    }




	return 0;
}
/*

*/









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值