题解:P3380 【模板】树套树

· · 题解

在遇到 ACM 类型的大型开卷比赛时,遇到平衡树只是题中一个挂件,但又不是那么挂件导致不能使用 set 和 unordered_map 平替时,老老实实写平衡树(即使手上有 Splay 和 FHQ Treap 的模板)常常会耗时(动辄 200 行以上)费力导致花的时间太多,bug 也难以调试——尤其是如上所述,平衡树只是一个挂件的情形,主体部分需要调试的那就更多了。但是好在市面上的大部分 ACM 模板都含有平板电视(pb_ds),我们也就可以轻松用这种数据结构迅速 AC 掉这类型的题。本题就是例子之一。

树套树又叫二逼平衡树,通用的做法即是线段树套平衡树,其他做法即是用小常数来分块处理。既然是平板电视做法,那我们当然使用的方法就是前者。

pb_ds 中的 ordered_set 相比 STL 中的 set 多了两个函数:

order_of_key(x)      // 查询有多少个元素小于 x
find_by_order(k)     // 查询排名第 k 的元素

本题中我们仅需用到前者。

因为本题时限很久(6.00s),所以不用减小常数与剪枝。如果需要减少用时,我们可以离散化(查询第 k 小时不用在 [0,1e8] 上二分,而是在出现过的值上二分)外加快读。因为时限够久,这里不再赘述这种做法。

以下为代码:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

const int INF = 0x7fffffff;

typedef tree<
    pair<int, int>,
    null_type,
    less<pair<int, int> >,
    rb_tree_tag,
    tree_order_statistics_node_update
> ost;

const int N = 200001;
const int V = 1e8; 

int n, m, a[N];
ost tr[N << 2];
vector<int> v;

void get(int p, int l, int r, int L, int R) {
    if (L <= l && r <= R) {
        v.push_back(p);
        return;
    }
    int mid = (l + r) >> 1;
    if (L <= mid)   get(p << 1, l, mid, L, R);
    if (R > mid)    get(p << 1 | 1, mid + 1, r, L, R);
}

void upd(int p, int l, int r, int x, int old_y, int new_y, bool is_init) {
    if (!is_init) {
        tr[p].erase({old_y, x});
    }
    tr[p].insert({new_y, x});
    if (l == r) return;
    int mid = (l + r) >> 1;
    if (x <= mid)   upd(p << 1, l, mid, x, old_y, new_y, is_init);
    else upd(p << 1 | 1, mid + 1, r, x, old_y, new_y, is_init);
}

int lt(int x) {
    int s = 0;
    for(int p: v)   s += tr[p].order_of_key({x, -INF}); 
    return s;
}

int le(int x) {
    int s = 0;
    for(int p: v)   s += tr[p].order_of_key({x, INF});
    return s;
}

int kth(int k) {
    int l = -V, r = V, ans = 0;
    while (l <= r) {
        int mid = (l + r) >> 1;
        if (le(mid) >= k)   ans = mid, r = mid - 1;
        else l = mid + 1;
    }
    return ans;
}

int pre(int x) {
    int ans = -INF;
    for(int p: v) {
        auto it = tr[p].lower_bound({x, -INF});
        if (it != tr[p].begin()) {
            it--;
            ans = max(ans, it->first);
        }
    }
    return ans;
}

int nxt(int x) {
    int ans = INF;
    for(int p: v) {
        auto it = tr[p].upper_bound({x, INF});
        if (it != tr[p].end())
            ans = min(ans, it->first);
    }
    return ans;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> m;
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
        upd(1, 1, n, i, 0, a[i], true); 
    }

    while (m--) {
        int op, l, r, k, x;
        cin >> op;
        if (op == 3) {
            cin >> x >> k;
            upd(1, 1, n, x, a[x], k, false);
            a[x] = k;
        }
        else {
            cin >> l >> r >> k;
            v.clear();
            get(1, 1, n, l, r);

            if (op == 1)        cout << lt(k) + 1 << '\n';
            else if (op == 2)   cout << kth(k) << '\n';
            else if (op == 4)   cout << pre(k) << '\n';
            else                cout << nxt(k) << '\n';
        }
    }
    return 0;
}