HDU 4417 Super Mario 线段树

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

题意:
求在区间[L,R]上小于H的个数。

思路:

可以分别对所给的区间高度和查询的H进行排序。并分别记录他们的编号。

然后通过编号来建线段树。

然后将查询的H进行遍历, 并同时将比H小的插入线段树中,查完所有比H小的元素之后, 更新线段树。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
const int maxn=100005;
int tree[maxn<<2];
//查询结构体
struct query
{
    int s;
    int e;
    int num; //大小
    int no; //编号
};
struct data
{
    int num;  //大小
    int no;  //编号
};
data a[maxn];
query q[maxn];
int t;
int n,m;
int total;
int anss[maxn];
int compare (query a,query b)
{
    return a.num<b.num;
}
int compare1 (data a,data b)
{
    return a.num<b.num;
}
void pushup (int re)
{
    tree[re]=tree[re<<1]+tree[re<<1|1];
}
void build (int l,int r,int re)
{
    tree[re]=0;
    if(l==r)
        return;
    int mid=(l+r)>>1;
    build (l,mid,re<<1);
    build (mid+1,r,re<<1|1);
}
int query (int left,int right,int l,int r,int re)
{
    if(l>=left&&r<=right)
        return tree[re];
    int ans=0;
    int mid=(l+r)>>1;
    if(mid>=left)
        ans+=query (left,right,l,mid,re<<1);
    if(mid<right)
        ans+=query (left,right,mid+1,r,re<<1|1);
    return ans;
}
void update (int l,int r,int re,int loc)
{
    if(l==r)
    {
        tree[re]++;
        return ;
    }
    int mid=(l+r)>>1;
    if(mid>=loc)
        update (l,mid,re<<1,loc);
    else
        update (mid+1,r,re<<1|1,loc);
    pushup (re);
}
int main()
{
    scanf("%d",&t);
    for (int k=1;k<=t;k++)
    {
        scanf("%d%d",&n,&m);
        build (1,n,1);
        for (int i=1;i<=n;i++)
        {
            scanf("%d",&a[i].num);
            a[i].no=i;
        }
        for (int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&q[i].s,&q[i].e,&q[i].num);
            q[i].no=i;
        }
        sort (a+1,a+n+1,compare1);
        sort (q+1,q+1+m,compare);
        for (int i=1,j=1;j<=m;j++)
        {
            //将比H小的高度插入线段树
            while (q[j].num>=a[i].num&&i<=n)
            {
                update (1,n,1,a[i].no);
                i++;
            }
            //查询在区间[L,R]上的个数
            anss[q[j].no]=query(q[j].s+1,q[j].e+1,1,n,1);
        }
        printf("Case %d:\n",k);
        for (int i=1;i<=m;i++)
            printf("%d\n",anss[i]);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值