可並堆模板

可並堆模板

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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章