CE求助
查看原帖
CE求助
900910
lsrsrl楼主2024/11/24 10:18
#include <bits/stdc++.h>

using namespace std;

int n, root, cnt, opt, x;
struct Node {
    int L, R, size, val, num;
    Node (int l, int r, int s, int v) : L(l), R(r), size(s), val(v), num ( 1 ) {}
    Node () {}
} t[100010];
inline void upd(int root) {
    t[root].size = t[t[root].L].size + t[t[root].R].size + t[root].num;
}
int rank(int x, int root) {
    if (root) {
        if (x < t[root].val)
        	return rank(x, t[root].L);
        if (x > t[root].val)
            return rank(x, t[root].R) + t[t[root].L].size + t[root].num;
        return t[t[root].L].size + t[root].num;
    }
    return 1;
}
int kth(int x, int root) {
    if (x <= t[t[root].L].size)
        return kth(x, t[root].L);
    if (x <= t[t[root].L].size + t[root].num)
        return t[root].val;
    return kth(x - t[t[root].L].size - t[root].num, t[root].R);
}
void insert(int x, int &root) {
    if (x < t[root].val)
        if (!t[root].L)
            t[t[root].L = ++cnt] = Node (0, 0, 1, x);
        else
            insert(x, t[root].L);
    else if (x > t[root].val)
        if (!t[root].R)
            t[t[root].R = ++cnt] = Node (0, 0, 1, x);
        else
            insert(x, t[root].R);
    else
        t[root].num++;
    upd(root);
}
int main() {
    cin >> n;
    int num = 0;
    t[root = ++cnt] = Node (0, 0, 1, INT_MAX);
    while (n--) {
        cin >> opt >> x;
        num++;
        if (opt == 1) cout << rank(x, root)<< endl;
        else if (opt == 2) cout << kth(x, root)<< endl;
        else if (opt == 3) cout << kth(rank(x, root) - 1, root)<< endl;
        else if (opt == 4) cout << kth(rank(x + 1, root), root)<< endl;
        else num--, insert(x, root);
    }
    return 0;
}
2024/11/24 10:18
加载中...