bzoj 3110 [Zjoi2013]K大数查询 整体二分

Posted copperoxide

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了bzoj 3110 [Zjoi2013]K大数查询 整体二分相关的知识,希望对你有一定的参考价值。

题面

题目传送门

解法

树套树比较苟,考虑整体二分

因为要求第K大,那么我们在二分的时候把(v)大于(mid)的放在右边并修改

修改直接用线段树区间加区间求和即可

时间复杂度:(O(m log^2 n))

代码

#include <bits/stdc++.h>
#define int long long
#define N 50010
using namespace std;
template <typename node> void chkmax(node &x, node y) {x = max(x, y);}
template <typename node> void chkmin(node &x, node y) {x = min(x, y);}
template <typename node> void read(node &x) {
    x = 0; int f = 1; char c = getchar();
    while (!isdigit(c)) {if (c == ‘-‘) f = -1; c = getchar();}
    while (isdigit(c)) x = x * 10 + c - ‘0‘, c = getchar(); x *= f;
}
struct Node {
    int op, l, r, v, id;
} a[N], tx[N], ty[N];
struct SegmentTree {
    struct Node {
        int l, r, sum, del;
    } t[N * 4];
    void build(int k, int l, int r) {
        t[k] = (Node) {l, r, 0, 0};
        if (l == r) return;
        int mid = (l + r) >> 1;
        build(k << 1, l, mid);
        build(k << 1 | 1, mid + 1, r);
    }
    void pushdown(int k) {
        int x = t[k].del; t[k].del = 0;
        int lc = k << 1, rc = k << 1 | 1;
        t[lc].del += x, t[rc].del += x;
        t[lc].sum += (t[lc].r - t[lc].l + 1) * x;
        t[rc].sum += (t[rc].r - t[rc].l + 1) * x; 
    }
    void modify(int k, int L, int R, int v) {
        int l = t[k].l, r = t[k].r;
        if (L <= l && r <= R) {
            t[k].del += v, t[k].sum += (r - l + 1) * v;
            return;
        }
        if (t[k].del) pushdown(k);
        int mid = (l + r) >> 1;
        if (L <= mid && mid < R)
            modify(k << 1, L, mid, v), modify(k << 1 | 1, mid + 1, R, v);
        if (R <= mid) modify(k << 1, L, R, v);
        if (L > mid) modify(k << 1 | 1, L, R, v);
        t[k].sum = t[k << 1].sum + t[k << 1 | 1].sum;
    }
    int query(int k, int L, int R) {
        int l = t[k].l, r = t[k].r;
        if (L <= l && r <= R) return t[k].sum;
        if (t[k].del) pushdown(k);
        int mid = (l + r) >> 1;
        if (R <= mid) return query(k << 1, L, R);
        if (L > mid) return query(k << 1 | 1, L, R);
        return query(k << 1, L, mid) + query(k << 1 | 1, mid + 1, R);
    }
} T;
int ans[N];
void solve(int l, int r, int L, int R) {
    if (l > r) return;
    if (L == R) {
        for (int i = l; i <= r; i++)
            if (a[i].op == 2) ans[a[i].id] = L;
        return;
    }
    int mid = (L + R) >> 1, fx = 0, fy = 0, lx = 0, ly = 0;
    for (int i = l; i <= r; i++)
        if (a[i].op == 1) {
            if (a[i].v > mid) T.modify(1, a[i].l, a[i].r, 1), ty[++ly] = a[i];
                else tx[++lx] = a[i];
        } else {
            int tmp = T.query(1, a[i].l, a[i].r);
            if (a[i].v > tmp) a[i].v -= tmp, tx[++lx] = a[i], fx = 1;
                else ty[++ly] = a[i], fy = 1;
        }
    for (int i = l; i <= r; i++)
        if (a[i].op == 1 && a[i].v > mid) T.modify(1, a[i].l, a[i].r, -1);
    for (int i = 1; i <= lx; i++) a[i + l - 1] = tx[i];
    for (int i = 1; i <= ly; i++) a[i + l + lx - 1] = ty[i];
    if (fx) solve(l, l + lx - 1, L, mid);
    if (fy) solve(l + lx, r, mid + 1, R);
}
main() {
    int n, m, q = 0; read(n), read(m);
    for (int i = 1; i <= m; i++) {
        read(a[i].op), read(a[i].l), read(a[i].r), read(a[i].v);
        if (a[i].op == 2) a[i].id = ++q;
    }
    T.build(1, 1, n), solve(1, m, 1, n);
    for (int i = 1; i <= q; i++) cout << ans[i] << "
";
    return 0;
}

以上是关于bzoj 3110 [Zjoi2013]K大数查询 整体二分的主要内容,如果未能解决你的问题,请参考以下文章

BZOJ3110[Zjoi2013]K大数查询 树套树

bzoj3110: [Zjoi2013]K大数查询

bzoj3110: [Zjoi2013]K大数查询

bzoj:3110: [Zjoi2013]K大数查询

Bzoj3110: [Zjoi2013]K大数查询

BZOJ 3110 [Zjoi2013]K大数查询