可并堆模板

可并堆模板

luogu P3377 示范


斜堆
#include <cstdio>
#include <cstring>
#define R register
#define Null b
struct Data
{
    int key;
    Data *lson, *rson;
} b[100010], *root[100010];
int tot;
int Pre[100010];
bool vis[100010];
int Find(R int Now){ return Pre[Now] == Now ? Now : Pre[Now] = Find(Pre[Now]); }
void Swap(R Data *&A, R Data *&B){ R Data *t = A; A = B; B = t; }
Data *Merge(R Data *A, R Data *B)
{
    if(A == Null || B == Null) return A == Null ? B : A;
    if(A->key > B->key || (A->key == B->key && A - b > B - b)) Swap(A, B);
    A->rson = Merge(A->rson, B);
    Swap(A->rson, A->lson);
    return A;
}
void Pop(R Data *&A){ A = Merge(A->lson, A->rson); }
int main()
{
    b[0] = (Data) {0, Null, Null};
    R int N, M;
    scanf("%d %d", &N, &M);
    for(R int i = 1; i <= N; i++)
    {
        R int t;
        scanf("%d", &t);
        b[++tot] = (Data) {t, Null, Null};
        root[i] = b + tot;
        Pre[i] = i;
    }
    root[0] = Null;
    while(M--)
    {
        R int opt, x, y;
        scanf("%d", &opt);
        if(opt == 1)
        {
            scanf("%d %d", &x, &y);
            R int X = Find(x), Y = Find(y);
            if(X != Y && !vis[x] && !vis[y]) 
            {
                Pre[Y] = X;
                root[X] = Merge(root[X], root[Y]);
            }
        }
        else
        {
            scanf("%d", &x);
            if(vis[x]) puts("-1");
            else 
            {
                R int X = Find(x);
                printf("%d\n", root[X]->key);
                vis[root[X] - b] = 1;
                Pop(root[X]);
            }
        }
    }
    return 0;
}

左偏树

两者的区别仅在于左偏树记录了距离,根据距离交换;而斜堆是每次都交换,均摊为O(log n),单次最坏为O(n)。

#include <cstdio>
#include <cstring>
#define R register
#define Null b
struct Data
{
    int dis, key;
    Data *lson, *rson;
} b[100010], *root[100010];
int tot;
int Pre[100010];
bool vis[100010];
int Find(R int Now){ return Pre[Now] == Now ? Now : Pre[Now] = Find(Pre[Now]); }
void Swap(R Data *&A, R Data *&B){ R Data *t = A; A = B; B = t; }
Data *Merge(R Data *A, R Data *B)
{
    if(A == Null || B == Null) return A == Null ? B : A;
    if(A->key > B->key || (A->key == B->key && A - b > B - b)) Swap(A, B);
    A->rson = Merge(A->rson, B);
    if(A->rson->dis > A->lson->dis) Swap(A->rson, A->lson);
    if(A->rson == Null) A->dis = 0;
    else A->dis = A->rson->dis + 1;
    return A;
}
void Pop(R Data *&A){ A = Merge(A->lson, A->rson); }
int main()
{
    b[0] = (Data) {-1, 0, Null, Null};
    R int N, M;
    scanf("%d %d", &N, &M);
    for(R int i = 1; i <= N; i++)
    {
        R int t;
        scanf("%d", &t);
        b[++tot] = (Data) {0, t, Null, Null};
        root[i] = b + tot;
        Pre[i] = i;
    }
    root[0] = Null;
    while(M--)
    {
        R int opt, x, y;
        scanf("%d", &opt);
        if(opt == 1)
        {
            scanf("%d %d", &x, &y);
            R int X = Find(x), Y = Find(y);
            if(X != Y && !vis[x] && !vis[y]) 
            {
                Pre[Y] = X;
                root[X] = Merge(root[X], root[Y]);
            }
        }
        else
        {
            scanf("%d", &x);
            if(vis[x]) puts("-1");
            else 
            {
                R int X = Find(x);
                printf("%d\n", root[X]->key);
                vis[root[X] - b] = 1;
                Pop(root[X]);
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值