hdu4417 树状数组(求指定区间比指定数小的数的个数)

http://acm.hdu.edu.cn/showproblem.php?pid=4417

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
/**
hdu4417  树状数组(求指定区间比指定数小的数的个数)
http://blog.csdn.net/wmn_wmn/article/details/8034181
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn=300005;

int C[maxn],ans[maxn];
int n,m;

int lowbit(int x)
{
    return x&(-x);
}

int sum(int x)
{
    int ret=0;
    while(x>0)
    {
        ret+=C[x];
        x-=lowbit(x);
    }
    return ret;
}

void add(int x,int d)
{
    while(x<=n)
    {
        C[x]+=d;
        x+=lowbit(x);
    }
}

struct node
{
    int id,num;
    bool operator < (const node &other) const
    {
        return num < other.num;
    }
}a[maxn];
struct note
{
    int l,r,id,value;
    bool operator < (const note &other)const
    {
        return value <other.value;
    }
}b[maxn];

int main()
{
    int T,tt=0;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
        {
            int x;
            scanf("%d",&a[i].num);
            a[i].id=i+1;
        }
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&b[i].l,&b[i].r,&b[i].value);
            b[i].l++;
            b[i].r++;
            b[i].id=i+1;
        }
        sort(a,a+n);
        sort(b,b+m);
        memset(C,0,sizeof(C));
        int k=0;
        for(int i=0;i<m;i++)
        {
            while(k<n&&a[k].num<=b[i].value)
            {
                add(a[k].id,1);
                k++;
            }
            ans[b[i].id]=sum(b[i].r)-sum(b[i].l-1);
        }
        printf("Case %d:\n",++tt);
        for(int i=1;i<=m;i++)
        {
            printf("%d\n",ans[i]);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值