HDU 4417 (主席树)

Super Mario

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4493    Accepted Submission(s): 2080


Problem Description
Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory. Now the poor princess is in trouble again and Mario needs to save his lover. We regard the road to the boss’s castle as a line (the length is n), on every integer point i there is a brick on height hi. Now the question is how many bricks in [L, R] Mario can hit if the maximal height he can jump is H.
 

Input
The first line follows an integer T, the number of test data.
For each test data:
The first line contains two integers n, m (1 <= n <=10^5, 1 <= m <= 10^5), n is the length of the road, m is the number of queries.
Next line contains n integers, the height of each brick, the range is [0, 1000000000].
Next m lines, each line contains three integers L, R,H.( 0 <= L <= R < n 0 <= H <= 1000000000.)
 

Output
For each case, output "Case X: " (X is the case number starting from 1) followed by m lines, each line contains an integer. The ith integer is the number of bricks Mario can hit for the ith query.
 

Sample Input
  
  
1 10 10 0 5 2 7 5 4 3 8 7 7 2 8 6 3 5 0 1 3 1 1 9 4 0 1 0 3 5 5 5 5 1 4 6 3 1 5 7 5 7 3
 

Sample Output
  
  
Case 1: 4 0 0 3 1 2 0 1 5 1
 


题意:求区间小于等于k的数的个数.

因为[l,r]中小于等于k的数等于[l,n]-[r+1,n]的个数,所以建号主席树后就只需要在

l版本的线段树查询小于等于k的个数减去r+1版本中查询小于等于k的个数.

#include <bits/stdc++.h>
using namespace std;
#define maxn 211111

struct node {
    int l, r;
    int num;
}tree[maxn<<4];
int n, m, T[maxn], cnt, tot;
int a[maxn];
vector <int> num;
map <int, int> gg;
struct Q {
    int l, r, num;
}op[maxn];

void lisan () {
    sort (num.begin (), num.end ());
    int cnt = 0;
    for (int i = 0; i < n+m; i++) {
        if (!i || num[i] != num[i-1]) {
            gg[num[i]] = ++cnt;
        }
    }
    for (int i = 1; i <= n; i++) {
        a[i] = gg[a[i]];
    }
    for (int i = 1; i <= m; i++) {
        op[i].num = gg[op[i].num];
    }
    tot = cnt;
}

int build_tree (int l, int r) {
    int root = cnt++;
    tree[root].num = 0;
    int mid = (l+r)>>1;
    if (l != r) {
        tree[root].l = build_tree (l, mid);
        tree[root].r = build_tree (mid+1, r);
    }
    return root;
}

int update (int root, int pos, int val) {
    int new_root = cnt++; int tmp = new_root;
    tree[new_root].num = tree[root].num + val;
    int l = 1, r = tot;
    while (l < r) {
        int mid = (l+r) >> 1;
        if (pos <= mid) {
            tree[new_root].l = cnt++;
            tree[new_root].r = tree[root].r;
            new_root = tree[new_root].l;
            root = tree[root].l;
            r = mid;
        }
        else {
            tree[new_root].l = tree[root].l;
            tree[new_root].r = cnt++;
            new_root = tree[new_root].r;
            root = tree[root].r;
            l = mid+1;
        }
        tree[new_root].num = tree[root].num + val;
    }
    return tmp;
}

int query (int root, int pos) {
    int ans = 0;
    int l = 1, r = tot;
    while (l < r) {
        int mid = (l+r)>>1;
        if (pos < mid) {
            root = tree[root].l;
            r = mid;
        }
        else {
            ans += tree[tree[root].l].num;
            root = tree[root].r;
            l = mid+1;
        }
    }
    return ans;
}

int main () {
    int t, kase = 0;
    scanf ("%d", &t);
    while (t--) {
        scanf ("%d%d", &n, &m);
        printf ("Case %d:\n", ++kase);
        gg.clear (); num.clear (); cnt = 0;
        for (int i = 1; i <= n; i++) {
            scanf ("%d", &a[i]);
            num.push_back (a[i]);
        }
        for (int i = 1; i <= m; i++) {
            scanf ("%d%d%d", &op[i].l, &op[i].r, &op[i].num);
            op[i].l++, op[i].r++;
            num.push_back (op[i].num);
        }
        lisan ();
        T[n+1] = build_tree (1, tot);
        for (int i = n; i >= 1; i--) {
            T[i] = update (T[i+1], a[i], 1);
        }
        for (int i = 1; i <= m; i++) {
            printf ("%d\n", query (T[op[i].l], op[i].num) -
                    query (T[op[i].r+1], op[i].num));
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值