bzoj2653 middle [主席树]

本文介绍了一种解决特定序列中最大中位数查询问题的高效算法。通过对权值建立线段树,并采用二分查找及区间查询最大子段和的方法,解决了给定序列上多个区间内的最大中位数求解问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Descrption:
一个长度为nn的序列a,设其排过序之后为bb,其中位数定义为b[n/2],其中a,ba,b00开始标号,除法取下整。
给你一个长度为n的序列s。
回答Q个这样的询问:ss的左端点在[a,b]之间,右端点在[c,d][c,d]之间的子序列中,最大的中位数。
其中a<b<c<da<b<c<d


Solution:
对权值建线段树,每个位置把小于当前值的位置赋为-1,其余为1,那么二分答案,区间查询最大子段和即可。


#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
struct node {
    int sum, l, r, lc, rc;
    node() {}
    node(int _sum, int _l, int _r, int _lc, int _rc) : sum(_sum), l(_l), r(_r), lc(_lc), rc(_rc) {}
    node friend operator + (const node &a, const node &b) {
        return node(a.sum + b.sum, max(a.l, a.sum + b.l), max(b.r, b.sum + a.r), 0, 0);
    }
} t[maxn * 30];
struct data {
    int x, id;
    bool friend operator < (const data &a, const data &b) {
        return a.x < b.x;
    }
} a[maxn];
int n, m, ans, cnt;
int rt[maxn], q[5];
void build(int l, int r, int &x) {
    x = ++cnt;
    t[x].sum = t[x].l = t[x].r = r - l + 1;
    if(l == r) {
        return;
    }
    int mid = (l + r) >> 1;
    build(l, mid, t[x].lc);
    build(mid + 1, r, t[x].rc);
}
void update(int l, int r, int &x, int last, int p, int d) {
    x = ++cnt;
    t[x] = t[last];
    if(l == r) {
        t[x].sum = t[x].l = t[x].r = -1;
        return;
    }
    int mid = (l + r) >> 1;
    if(p <= mid) {
        update(l, mid, t[x].lc, t[last].lc, p, d);
    } else {
        update(mid + 1, r, t[x].rc, t[last].rc, p, d);
    }
    int lc = t[x].lc, rc = t[x].rc;
    t[x].sum = t[lc].sum + t[rc].sum;
    t[x].l = max(t[lc].l, t[lc].sum + t[rc].l);
    t[x].r = max(t[rc].r, t[rc].sum + t[lc].r);
}
node query(int l, int r, int x, int a, int b) {
    if(l > b || r < a) {
        return t[0];
    }
    if(l >= a && r <= b) {
        return t[x];
    }
    int mid = (l + r) >> 1;
    node T = query(l, mid, t[x].lc, a, b) + query(mid + 1, r, t[x].rc, a, b);
    return T;
}
int main() {
    scanf("%d", &n);
    t[0].l = t[0].r = -1e9;
    for(int i = 1; i <= n; ++i) {
        scanf("%d", &a[i].x);
        a[i].id = i;
    }
    sort(a + 1, a + n + 1);
    build(1, n, rt[1]);
    for(int i = 2; i <= n; ++i) {
        update(1, n, rt[i], rt[i - 1], a[i - 1].id, -1);
    }
    scanf("%d", &m);
    while(m--) {
        for(int i = 0; i < 4; ++i) {
            scanf("%d", &q[i]);
            q[i] = (q[i] + ans) % n + 1;
        }
        sort(q, q + 4);
        int l = 1, r = n + 1;
        while(r - l > 1) {
            int mid = (l + r) >> 1, d;
            int a = query(1, n, rt[mid], q[0], q[1]).r;
            int b = query(1, n, rt[mid], q[1] + 1, q[2] - 1).sum;
            int c = query(1, n, rt[mid], q[2], q[3]).l;
            if(a + b + c >= 0) {
                l = mid;
            } else {
                r = mid;
            }
        }
        printf("%d\n", ans = a[l].x);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值