FHQ MLE是什么问题呀?

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

cjwdyzxfblzs @ 2023-04-16 16:48:23

#include <bits/stdc++.h>

using namespace std;

#define int long long

const int N = 1e6;
mt19937 rnd(233);

int read(){
    register int x=0;
    register bool f=0;
    register char c=getchar();
    while(c<'0'||c>'9'){
        if(c=='-') f=1;
        c=getchar();
    }
    while(c>='0'&&c<='9'){
        x=(x<<3)+(x<<1)+c-48;
        c=getchar();
    }
    return f?-x:x;
}

struct FHQ
{   
    int l, r;
    int key, val;
    int sizes;
}tr[N];
int root, idx;
int x, y, z;

int get_node(int key)
{
    tr[ ++ idx].key = key;
    tr[idx].val = rnd();
    tr[idx].sizes = 1;
    return idx;
}
void push_up(int u)
{
    tr[u].sizes = tr[tr[u].l].sizes + tr[tr[u].r].sizes + 1;
    return;
}
void split(int p, int key, int &x, int &y)
{
    if (!p)
    {
        x = y = 0;
        return;
    }
    else
    {
        if (tr[p].key <= key)
        {
            x = p;
            split(tr[p].r, key, tr[p].r, y);
        }
        else
        {
            y = p;
            split(tr[p].l, key, x, tr[p].l);
        }
        push_up(p);
    }
    return;
}
int merge(int x, int y)
{
    if (!x || !y) return x + y;
    if (tr[x].val <= tr[y].val)
    {
        tr[x].r = merge(tr[x].r, y);
        push_up(x);
        return x;
    }
    else 
    {
        tr[y].l = merge(x, tr[y].l);
        push_up(y);
        return y;
    }
}
void insert(int key)
{
    split(root, key, x, y);
    root = merge(merge(x, get_node(key)), y);
    return;
}
void del(int key)
{
    split(root, key, x, z);
    split(root, key - 1, x, y);
    y = merge(tr[y].l, tr[y].r);
    root = merge(merge(x, y), z);
    return;
}
int get_rank(int key)
{
    split(root, key - 1, x, y);
    key = tr[x].sizes + 1;
    root = merge(x, y);
    return key;
}
int get_key(int rank)
{
    int p = root;
    while (p)
    {
        if (tr[tr[p].l].sizes + 1 == rank)
            break;
        else if (tr[tr[p].l].sizes >= rank)
            p = tr[p].l;
        else
        {
            rank -= tr[tr[p].l].sizes + 1;
            p = tr[p].r;
        }
    }
    return tr[p].key;
}
int get_pre(int key)
{
    split(root, key - 1, x, y);
    int p = x;
    while (tr[p].r) { p = tr[p].r;}
    key = tr[p].key;
    root = merge(x, y);
    return key;
}
int get_next(int key)
{
    split(root, key, x, y);
    int p = y;
    while (tr[p].l) { p = tr[p].l;}
    key = tr[p].key;
    root = merge(x, y);
    return key;
}

int n, m;
int last;
int ans;

signed main()
{
    n = read(), m = read();
    for (register int i = 1; i <= n; i ++ )
    {
        int a;
        a = read();
        insert(a);
    }
    for (register int i = 1; i <= m; i ++ )
    {
        int opt, x;
        opt = read(), x = read();
        if (opt == 1) insert(x ^ last);
        if (opt == 2) del(x ^ last);
        if (opt == 3) { last = get_rank(x ^ last); ans = ans ^ last;}
        if (opt == 4) { last = get_key(x ^ last); ans = ans ^ last;}
        if (opt == 5) { last = get_pre(x ^ last); ans = ans ^ last;}
        if (opt == 6) { last = get_next(x ^ last); ans = ans ^ last;}
    }
    cout << ans << "\n";

    return 0;
}

by cjwdyzxfblzs @ 2023-04-29 18:26:22

已经找到问题了,此贴终


by jia_hua_wu @ 2023-05-11 13:50:37

@sjzez__chess 我也遇到了这个问题,请问是怎么解决的


|