70 pts 求助
查看原帖
70 pts 求助
1028403
封禁用户楼主2023/11/5 09:36

问题都出在最后俩点(。。。

不吸氧一个 MLE 一个 TLE,吸氧俩都 TLE(。。。

深入浅出书上的

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
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[N];
inline void update(int root) {
    t[root].size = t[t[root].left].size + t[t[root].right].size + t[root].num;
}
int rankk(int x, int root) {
    if (root) {
        if (x < t[root].value)
            return rankk(x, t[root].left);
        if (x > t[root].value)
            return rankk(x, t[root].right) + t[t[root].left].size + t[root].num;
        return t[t[root].left].size + t[root].num;
    }
    return 1;
}
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);
}
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);
}
int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> n;
    int num = 0;
    t[root = ++cnt] = Node(0, 0, 1, 2147483647);
    while (n--) {
        cin >> opt >> x;
        num++;
        if (opt == 1) cout << rankk(x, root) << endl;
        else if (opt == 2) cout << kth(x, root) << endl;
        else if (opt == 3) cout << kth(rankk(x, root) - 1, root) << endl;
        else if (opt == 4) cout << kth(rankk(x + 1, root), root) << endl;
        else num--, insert(x, root);
    }
    return 0;
}
2023/11/5 09:36
加载中...