HDU - 4417 Super Mario 主席树

题目链接:https://vjudge.net/problem/HDU-4417#author=0
题意:多次询问区间小于等于k的数目。
思路:考虑用主席树,离散化要记得将k的值也添入,其余就是主席树常规操作了。

#include
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define ls rt << 1
#define rs rt << 1|1
#define po pop_back
#define pb push_back
#define mk make_pair
#define lson l, mid, ls
#define rson mid + 1, r, rs
#define pll pair
#define pii pair
#define ull unsigned long long
#define pdd pair
const int mod = 1e9 + 7;
const int maxn = 2e5 + 10;
const int inf = 0x3f3f3f3f;
const ll linf = 0x3f3f3f3f3f3f3f3f;
int sum[maxn << 5], root[maxn], Ls[maxn << 5], Rs[maxn << 5]; //sum记录前缀和数量,root记录根范围
int a[maxn], b[maxn], vis[maxn];
int n, m, tot = 0, len;

int getid(int val) //返回离散化下标
{
    return lower_bound(b + 1, b + len + 1, val) - b;
}
int build(int l, int r) //不用build root[0] = 0
{
    int x = ++tot; //新增结点
    sum[x] = 0;
    if(l == r) return x; //叶子结点返回编号
    int mid = (l + r) >> 1;
    Ls[x] = build(l, mid);
    Rs[x] = build(mid + 1, r);
    return x;
}
int update(int k, int l, int r, int rt) //插入元素k
{
    int x = ++tot;
    Ls[x] = Ls[rt], Rs[x] = Rs[rt], sum[x] = sum[rt] + 1; //复制信息
    if(l == r) return x;
    int mid = (l + r) >> 1; //接下来要更新要增加的链
    if(k <= mid) Ls[x] = update(k, l, mid, Ls[x]);
    else Rs[x] = update(k, mid + 1, r, Rs[x]);
    return x;
}
int query_kth(int u, int v, int l, int r, int k) //查询第k大
{
    int mid = (l + r) >> 1, x = sum[Ls[v]] - sum[Ls[u]]; //通过前缀和得到当前左区间的数目
    if(l == r) return l;
    if(k <= x) return query_kth(Ls[u], Ls[v], l, mid, k); //如果左边够就去左边
    else return query_kth(Rs[u], Rs[v], mid + 1, r, k - x);
}
int query_num(int u, int v, int l, int r, int ql, int qr) //查询区间个数
{
    if(ql <= l && qr >= r) return sum[v] - sum[u];
    int mid = (l + r) >> 1, res = 0;
    if(ql <= mid) res += query_num(Ls[u], Ls[v], l, mid, ql, qr);
    if(qr > mid) res += query_num(Rs[u], Rs[v], mid + 1, r, ql, qr);
    return res;
}
int query_kind(int u, int v, int l, int r, int k) //统计区间大于等于k的数目
{
    if(l == r) return sum[v] - sum[u]; //l这个点在这个区间出现次数
    int mid = (l + r) >> 1;
    //如果在左边,那么就遍历左边再加上右边全部的数量
    if(k <= mid) return query_kind(Ls[u], Ls[v], l, mid, k) + sum[Rs[v]] - sum[Rs[u]];
    else return query_kind(Rs[u], Rs[v], mid + 1, r, k);
}
int L[maxn], R[maxn], K[maxn];
void init()
{
    tot = 0;
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; ++i)
    {
        scanf("%d", &a[i]);
        b[i] = a[i];
    }
    for(int i = 1; i <= m; ++i)
    {
        scanf("%d%d%d", &L[i], &R[i], &K[i]);
        L[i]++, R[i]++;
        b[n + i] = K[i];
    }
    sort(b + 1, b + n + m + 1);
    len = unique(b + 1, b + n + m + 1) - b - 1;
    build(1, len);
    for(int i = 1; i <= n; ++i)
        root[i] = update(getid(a[i]), 1, len, root[i - 1]);
}


int main()
{
    int t, t1 = 1;
    scanf("%d", &t);
    while(t--)
    {
        init();
        printf("Case %d:\n", t1++);
        for(int i = 1; i <= m; ++i)
        {
            printf("%d\n", R[i] - L[i] + 1 - query_kind(root[L[i] - 1], root[R[i]], 1, len, getid(K[i]) + 1));
        }
    }
    return 0;
}

 

你可能感兴趣的:(HDU,主席树)