fhq-treap一半WA

P6136 【模板】普通平衡树(数据加强版)

char_cha_ch @ 2022-09-26 13:55:50

#include<stdio.h>
#define N 5000009
#include<stdlib.h>
#include<time.h>

int val[N], prio[N], siz[N], ls[N], rs[N], tot, rt;

inline void updata(int x)
{
    siz[x] = siz[ls[x]] + siz[rs[x]] + 1;
}

inline int New(int x)
{
    val[++ tot] = x, siz[tot] = 1, prio[tot] = rand();
    return tot;
}

void split(int cur, int key, int& x, int& y)
{
    if (!cur)
    {
        x = y = 0;
        return;
    }
    if (val[cur] <= key)
        x = cur, split(rs[cur], key, rs[cur], y);
    else
        y = cur, split(ls[cur], key, x, ls[cur]);
    updata(cur);
}

int merge(int x, int y)
{
    if (!x || !y)
        return x + y;
    if (prio[x] < prio[y])
    {
        rs[x] = merge(rs[x], y);
        updata(x);
        return x;
    }
    else
    {
        ls[y] = merge(x, ls[y]);
        updata(y);
        return y;
    }
}

inline void insert(int key)
{
    int x = 0, y = 0;
    split(rt, key - 1, x, y);
    rt = merge(merge(x, New(key)), y);
}

inline void del(int key)
{
    int x = 0, y = 0, z = 0;
    split(rt, key, x, z);
    split(x, key - 1, x, y);
    y = merge(ls[y], rs[y]);
    rt = merge(merge(x, y), z);
}

inline int rank(int key)
{
    int x = 0, y = 0, ret;
    split(rt, key - 1, x, y);
    ret = siz[x] + 1;
    rt = merge(x, y);
    return ret;
}

inline int xrank(int key)
{
    int cur = rt;
    while (1)
    {
        int lsiz = siz[ls[cur]] + 1;
        if (key == lsiz)
            return val[cur];
        if (key <= lsiz)
            cur = ls[cur];
        else
            key -= lsiz, cur = rs[cur];
    }
}

inline int pre(int key)
{
    int x = 0, y = 0, cur, ret;
    split(rt, key - 1, x, y);
    cur = x;
    while (rs[cur])
        cur = rs[cur];
    ret = val[cur];
    rt = merge(x, y);
    return ret;
}

inline int nxt(int key)
{
    int x = 0, y = 0, cur, ret;
    split(rt, key, x, y);
    cur = y;
    while (ls[cur])
        cur = ls[cur];
    ret = val[cur];
    rt = merge(x, y);
    return ret;
}

int main()
{
    srand(time(0));
    int n, m, opt, x, lastans = 0, ans = 0;
    scanf("%d%d", &n, &m);
    while (n --)
        scanf("%d", &x), insert(x);
    while (m --)
    {
        scanf("%d%d", &opt, &x);
        x ^= lastans;
        if (opt == 1)
            insert(x);
        if (opt == 2)
            del(x);
        if (opt == 3)
            lastans = rank(x);
        if (opt == 4)
            lastans = xrank(x);
        if (opt == 5)
            lastans = pre(x);
        if (opt == 6)
            lastans = nxt(x);
        ans ^= lastans;
    }
    printf("%d", ans);

    return 0;
}

by Zvelig1205 @ 2022-09-26 14:18:53

@kirihara233 真的只是 WA 一半吗?您连样例都没过……

不是查询的操作ans不需要异或lastans


by char_cha_ch @ 2022-09-26 22:22:16

@Zvelig1205 是 WA 一半,总感觉异或出了问题,之前写 Splay 也是一样的


by char_cha_ch @ 2022-09-26 23:05:40

@Zvelig1205 改对了,就是异或错误


|