算法整理 & 复习:Treap

文章目录




一、普通平衡树


P3369 【模板】普通平衡树

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
using namespace std;
#define MAXN 100005

struct Treapnode
{
    int l, r;
    int val, key;
    int size;
}tree[MAXN];

int cnt, root;

int newNode(int k)
{
    tree[++cnt].val = k;
    tree[cnt].key = rand();
    tree[cnt].size = 1;
    return cnt;
}

void update(int now)
{
    tree[now].size = tree[tree[now].r].size + tree[tree[now].l].size + 1;
}

void split(int now, int val, int &x, int &y)//按大小分裂
{
    if (!now) x = y = 0;
    else
    {
        if (tree[now].val <= val)
        {
            x = now;
            split(tree[now].r, val, tree[now].r, y);
        }
        else
        {
            y = now;
            split(tree[now].l, val, x, tree[now].l);
        }
        update(now);
    }
}

int merge(int x, int y)
{
    if (!x || !y) return x + y;
    if (tree[x].key > tree[y].key)
    {
        tree[x].r = merge(tree[x].r, y);
        update(x);
        return x;
    }
    else
    {
        tree[y].l = merge(x, tree[y].l);
        update(y);
        return y;
    }
}

int x, y, z;

void ins(int val)
{
    split(root, val, x, y);
    root = merge(merge(x, newNode(val)), y);
}

void del(int val)
{
    split(root, val, x, z);
    split(x, val-1, x, y);
    y = merge(tree[y].l, tree[y].r);
    root = merge(merge(x, y), z);
}

int Rank(int val)
{
    split(root, val-1, x, y);
    int rk = tree[x].size+1;
    root = merge(x, y);
    return rk;
}

int kth(int k)
{
    int now = root;
    while (now)
    {
        if (tree[tree[now].l].size+1 == k) break;
        else if (tree[tree[now].l].size >= k)
        {
            now = tree[now].l;
        }
        else
        {
            k -= tree[tree[now].l].size+1;
            now = tree[now].r;
        }
    }
    return tree[now].val;
}

int pre(int val)
{
    split(root, val-1, x, y);
    int now = x;
    while (tree[now].r)
    {
        now = tree[now].r;
    }
    int ans = tree[now].val;
    root = merge(x, y);
    return ans;
}

int next(int val)
{
    split(root, val, x, y);
    int now = y;
    while (tree[now].l)
    {
        now = tree[now].l;
    }
    int ans = tree[now].val;
    root = merge(x, y);
    return ans;
}

int main(void)
{
    srand(time(NULL));
    int n;
    cin >> n;
    for(int i=1;i<=n;i++)
    {
        int a,b;
        cin >> a >> b;
        switch (a)
        {
            case 1:
                ins(b);
                break;
            case 2:
                del(b);
                break;
            case 3:
                cout << Rank(b) << endl;
                break;
            case 4:
                cout << kth(b) << endl;
                break;
            case 5:
                cout << pre(b) << endl;
                break;
            case 6:
                cout << next(b) << endl;
                break;
        }
    }
    return 0;
}



二、不储存索引的平衡树


int merge(int x, int y)
{
    if (!x || !y) return x + y;
    if (rand()&1)//可以用rand()%2等方法实现无key treap,但是性能玄学。
    {
        tree[x].r = merge(tree[x].r, y);
        update(x);
        return x;
    }
    else
    {
        tree[y].l = merge(x, tree[y].l);
        update(y);
        return y;
    }
}



三、文艺平衡树


P3391 【模板】文艺平衡树

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
#define MAXN 100005

int n, m, root, cnt;

struct node{
    int l, r;
    int val, key;
    int size;
    bool tag;
}tree[MAXN];

int newnode(int val)
{
    tree[++cnt].val = val;
    tree[cnt].key = rand();
    tree[cnt].size = 1;
    return cnt;
}

void update(int now){
    tree[now].size = tree[tree[now].l].size + tree[tree[now].r].size + 1;
}

void down(int now)
{
    swap(tree[now].l, tree[now].r);
    tree[tree[now].l].tag ^= 1;
    tree[tree[now].r].tag ^= 1;
    tree[now].tag = 0;
}

void split(int now, int siz, int &x, int &y)
{
    if (!now) x = y = 0;
    else
    {
        if (tree[now].tag) down(now);
        if (tree[tree[now].l].size < siz)
        {
            x = now;
            split(tree[now].r, siz-tree[tree[now].l].size-1, tree[now].r, y);
        }
        else
        {
            y = now;
            split(tree[now].l, siz, x, tree[now].l);
        }
        update(now);
    }
}

int merge(int x, int y)
{
    if (!x || !y) return x + y;
    if (tree[x].key < tree[y].key)
    {
        if (tree[x].tag) down(x);
        tree[x].r = merge(tree[x].r, y);
        update(x);
        return x;
    }
    else
    {
        if (tree[y].tag) down(y);
        tree[y].l = merge(x, tree[y].l);
        update(y);
        return y;
    }
}

void reverse(int l, int r)
{
    int x, y, z;
    split(root, l-1, x, y);
    split(y, r-l+1, y, z);
    tree[y].tag ^= 1;
    root = merge(merge(x, y), z);
}

void ldr(int now)
{
    if (!now) return;
    if (tree[now].tag) down(now);
    ldr(tree[now].l);
    cout << tree[now].val << " ";
    ldr(tree[now].r);
}

int main(void)
{
    srand(time(NULL));
    cin >> n >> m;
    for (int i=1;i<=n;i++)
    {
        root = merge(root, newnode(i));
    }
    for (int i=1;i<=m;i++)
    {
        int l, r;
        cin >> l >> r;
        reverse(l, r);
    }
    ldr(root);
}





返回
上一篇:P2596 [ZJOI2006]书架(fhq treap)


下一篇:[模板] 平衡树之Treap