HDU 4417 Super Mario (树状数组 + 离线)

本文介绍了一种使用树状数组进行区间查询的优化方法,通过离线处理查询请求并结合数据结构,实现高效查询区间内满足条件元素的数量。

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

题意:

给你一堆数, m 次询问, 每次询问问你区间里  小于等于x 的数有多少个?

思路:

很容易往 数据结构 + 离线 上想。

一开始写了一发发现想偏了。然后就没然后了。。

==========================

先把询问存下来, 按照h 排序, 在给那一堆数存下来, 排序。

这样枚举询问, 在写个  a数组的指针 , 只要比当前h小, 就往后挪就行, 挪的时候, 给那个位置 + 1.

这样就是一个简单的区间和了。

树状数组就成了。(基于上次那个分治的教训, 能用树状数组就别用线段树了)



#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 100000 + 10;

int T, n, m;
long long c[maxn];


inline lowbit(int x){
    return x&-x;
}
int sum(int x){
    int ans = 0;
    while(x > 0){
        ans += c[x];
        x -= lowbit(x);
    }
    return ans;
}
void add(int x, int v){
    while(x <= n){
        c[x] += v;
        x += lowbit(x);
    }
}

struct Node{
    int val, id;
    void read(int i){
        id = i;
        scanf("%d",&val);
    }
}a[maxn];
bool cmp_Node(Node a, Node b ){
    return a.val < b.val;
}

struct node{
    int l,r,h;
    int id;
    void read(int i){
        id = i;
        scanf("%d %d %d",&l, &r, &h);
    }
}p[maxn];
int ans[maxn];
bool cmp(node a, node b){
    return a.h < b.h;
}

int main(){
    scanf("%d",&T);int ks = 0;
    while(T--){
        memset(c,0,sizeof c);
        scanf("%d %d",&n, &m);
        for (int i = 0; i < n; ++i){
            a[i].read(i+1);

        }
        for (int i = 0; i < m; ++i){
            p[i].read(i);
        }
        sort(p, p + m, cmp);
        sort(a, a + n, cmp_Node);

        int j = 0;
        for (int i = 0; i < m; ++i){
            while(j < n && a[j].val <= p[i].h){
                add(a[j].id, 1);
                ++j;
            }
            ans[ p[i].id ] = sum(p[i].r + 1) - sum(p[i].l);
        }

        printf("Case %d:\n", ++ks);
        for (int i = 0; i < m; ++i){
            printf("%d\n", ans[i]);
        }
    }


    return 0;
}


Super Mario

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


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
 

Source
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   6113  6112  6111  6110  6109 
 

Statistic |  Submit |  Discuss |  Note

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值