ds求解
查看原帖
ds求解
823830
mcturtle楼主2024/7/21 14:19
//请ds求解,全部copy深初,#0全A,新hackTLE
#include <iostream>
using namespace std;
int n, root, cnt, opt, x;
struct node
{
    int left, right, size, value, num;
    node(int l, int r, int s, int v)
        : left(l), right(r), size(s), value(v), num(1) {}
    node() {}
} t[100010];
inline void update(int root)
{
    t[root].size = t[t[root].left].size + t[t[root].right].size + t[root].num;
}
inline int rk(int x, int root)
{
    if (root)
    {
        if (x < t[root].value)
            return rk(x, t[root].left);
        if (x > t[root].value)
            return rk(x, t[root].right) + t[t[root].left].size + t[root].num;
        return t[t[root].left].size + t[root].num;
    }
    return 1;
}
inline int kth(int x, int root)
{
    if (x <= t[t[root].left].size)
        return kth(x, t[root].left);
    if (x <= t[t[root].left].size + t[root].num)
        return t[root].value;
    return kth(x - t[t[root].left].size - t[root].num, t[root].right);
}
inline void insert(int x, int &root)
{
    if (x < t[root].value)
        if (!t[root].left)
            t[t[root].left = ++cnt] = node(0, 0, 1, x);
        else
            insert(x, t[root].left);
    else if (x > t[root].value)
        if (!t[root].right)
            t[t[root].right = ++cnt] = node(0, 0, 1, x);
        else
            insert(x, t[root].right);
    else
        t[root].num++;
    update(root);
}
signed main()
{
    cin >> n;
    int num = 0;
    t[root = ++cnt] = node(0, 0, 1, 2147483647);
    while (n--)
    {
        cin >> opt >> x;
        num++;
        if (opt == 1)
            cout << rk(x, root) << endl;
        else if (opt == 2)
            cout << kth(x, root) << endl;
        else if (opt == 3)
            cout << kth(rk(x, root) - 1, root) << endl;
        else if (opt == 4)
            cout << kth(rk(x + 1, root), root) << endl;
        else
            num--, insert(x, root);
    }
    return 0;
}

qaq

2024/7/21 14:19
加载中...