treap平衡树20分求助
查看原帖
treap平衡树20分求助
341672
zjutwyy楼主2021/8/26 16:31

大概写法和y总的板子一样,但是一直20分qwq

我试过直接复制y总原板子+改查询rank返回值也过不去

#include <bits/stdc++.h>

using namespace std;

const int N = 4e6 + 10;
const int INF = 0x3f3f3f3f;

int n, m;
struct node
{
    int l, r;
    int key, val;
    int cnt, size;
} tr[N];

int root, idx;

void pushup(int p)
{
    tr[p].size = tr[tr[p].l].size + tr[tr[p].r].size + tr[p].cnt;
}

int add_node(int key)
{
    tr[++idx].key = key;
    tr[idx].val = rand();
    tr[idx].cnt = 1;
    tr[idx].size = 1;
    return idx;
}

void lspin(int& p) //leftspin
{
    int q = tr[p].r;
    tr[p].r = tr[q].l;
    tr[q].l = p;
    p = q;
    pushup(tr[p].l);
    pushup(p);
}

void rspin(int& p) //rightspin
{
    int q = tr[p].l;
    tr[p].l = tr[q].r;
    tr[q].r = p;
    p = q;
    pushup(tr[p].r);
    pushup(p);
}

void build()
{
    tr[0] = {0, 0, 0, 0, 0, 0};
    //两个哨兵
    add_node(-INF);
    add_node(INF);
    root = 1, tr[1].r = 2;
    pushup(root);
    if(tr[1].val < tr[2].val)
        lspin(root);
}

void insert(int& p, int key)
{
    if(!p)
        p = add_node(key);
    else if(tr[p].key == key)
        tr[p].cnt++;
    else if(tr[p].key > key)
    {
        insert(tr[p].l, key);
        if(tr[tr[p].l].val > tr[p].val)
            rspin(p);
    }
    else
    {
        insert(tr[p].r, key);
        if(tr[tr[p].r].val > tr[p].val)
            lspin(p);
    }
    pushup(p);
}

void remove(int& p, int key)
{
    if(!p)
        return ;
    if(tr[p].key == key)
    {
        if(tr[p].cnt > 1) //个数多于1,直接减
            tr[p].cnt--;
        else if(tr[p].l || tr[p].r) //有左右子树且个数为1,要考虑如何把子树接到父亲节点
        {
            if(!tr[p].r || tr[tr[p].l].val > tr[tr[p].r].val)
            {
                rspin(p);
                remove(tr[p].r, key);
            }
            else
            {
                lspin(p);
                remove(tr[p].l, key);
            }
        }
        else //没有左右子树且个数为1,直接置零
            p = 0;
    }
    else if(tr[p].key > key)
        remove(tr[p].l, key);
    else
        remove(tr[p].r, key);
    pushup(p);
}

int get_rank_by_key(int p, int key)
{
    //不存在这个key,不同情况返回不同值
    if(!p)
    {
        return 1;
    }

    if(tr[p].key == key)
        return tr[tr[p].l].size + 1;
    if(tr[p].key > key)
        return get_rank_by_key(tr[p].l, key);
    else
        return tr[tr[p].l].size + tr[p].cnt + get_rank_by_key(tr[p].r, key);
}

int get_key_by_rank(int p, int rnk)
{
    if(!p)
        return 0;
    if(tr[tr[p].l].size >= rnk)
        return get_key_by_rank(tr[p].l, rnk);
    if(tr[tr[p].l].size + tr[p].cnt >= rnk)
        return tr[p].key;
    return get_key_by_rank(tr[p].r, rnk - tr[tr[p].l].size - tr[p].cnt);
}

int get_prev(int p, int key)
{
    if(!p) //找不到
        return -INF;
    if(tr[p].key >= key)
        return get_prev(tr[p].l, key);
    return max(tr[p].key, get_prev(tr[p].r, key));
}

int get_next(int p, int key)
{
    if(!p)
        return INF;
    if(tr[p].key <= key)
        return get_next(tr[p].r, key);
    return min(tr[p].key, get_next(tr[p].l, key));
}

int main()
{
    build();
    scanf("%d%d", &n, &m);
    while(n--)
    {
        int x;
        scanf("%d", &x);
        insert(root, x);
    };
    int last = 0;
    int res = 0;
    while(m--)
    {
        int op, x;
        scanf("%d%d", &op, &x);
        x ^= last;
        switch(op)
        {
        case 1:
            insert(root, x);
            break;
        case 2:
            remove(root, x);
            break;
        case 3:
            last = get_rank_by_key(root, x) - 1;
            res ^= last;
            break;
        case 4:
            last = get_key_by_rank(root, x + 1);
            res ^= last;
            break;
        case 5:
            last = get_prev(root, x);
            res ^= last;
            break;
        case 6:
            last = get_next(root, x);
            res ^= last;
            break;
        }
    }
    printf("%d\n", res);
    return 0;
}
2021/8/26 16:31
加载中...