HDU4417-Super Mario

Super Mario

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


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

题意:给出n个数,问区间内小于等于k的有几个数

解题思路:由于数据范围较大,所以不能离散化处理,但由于数字的个数是不大的,可以通过排序进行重新编号,然后在建可持久化线段树


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <set>

using namespace std;

#define LL long long
int n,m,k;
int a[100009],x[100009];
int s[100009],L[100009*20],R[100009*20],sum[100009*20];
int tot;

void build(int now,int l,int r,int p)
{
    sum[++tot]=sum[now]+1;
    if(l==r) L[tot]=R[tot]=0;
    else
    {
        int mid=(l+r)>>1;
        L[tot]=L[now];R[tot]=R[now];
        if(mid>=p) L[tot]=tot+1;
        else R[tot]=tot+1;
        if(p<=mid) build(L[now],l,mid,p);
        else build(R[now],mid+1,r,p);
    }
}

int query(int u,int v,int l,int r,int p)
{
    if(p<l||p>r) return 0;
    if(l==r) return sum[u]-sum[v];
    else
    {
        int mid=(l+r)>>1;
        if(p>mid) return sum[L[u]]-sum[L[v]]+query(R[u],R[v],mid+1,r,p);
        else return query(L[u],L[v],l,mid,p);
    }
}

int main()
{
    int t,cas=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&n,&k);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]),x[i]=a[i];
        sort(a+1,a+n+1);
        m=unique(a+1,a+n+1)-a;
        memset(s,0,sizeof s);
        L[0]=R[0]=sum[0]=tot=0;
        for(int i=1;i<=n;i++)
        {
            x[i]=lower_bound(a+1,a+m,x[i])-a;
            s[i]=tot+1;
            build(s[i-1],1,m,x[i]);
        }
        int ll,rr,kk;
        printf("Case %d:\n",++cas);
        for(int i=0;i<k;i++)
        {
            scanf("%d %d %d",&ll,&rr,&kk);
            kk=upper_bound(a+1,a+m,kk)-a-1;
            printf("%d\n",query(s[rr+1],s[ll],1,m,kk));
        }
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值