hdu4417 Super Mario

Super Mario

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


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
2012 ACM/ICPC Asia Regional Hangzhou Online 
简单的线段树
#include <iostream>
#include <stdio.h>
#include <algorithm>
#define MAXN 100500
using namespace std;
struct node {
    int x,id;
    bool operator <(node b)const{return x<b.x;}
}no[MAXN];
struct que{
    int l,r,h,id;
    bool operator <(que b)const{return h<b.h;}
}qu[MAXN];
int l[MAXN*20],ans[MAXN];
bool cmp1(node a,node b){return a<b;}
bool cmp2(que a,que b){return a<b;}
void build(int num,int s,int e)
{
    l[num]=0;
    if(s>=e)
    return ;
    int mid=(s+e)>>1;
    build(num<<1,s,mid);
    build(num<<1|1,mid+1,e);
}
int update(int num,int s,int e,int a)
{
    if(s>=e)
    {
        l[num]++;
        return 1;
    }
    int mid;
    mid=(s+e)>>1;
    if(a<=mid)update(num<<1,s,mid,a);
    else if(a>mid)update(num<<1|1,mid+1,e,a);
    l[num]=l[num<<1]+l[num<<1|1];
}
int query(int num,int s,int e,int a,int b)
{
    if(a<=s&&b>=e)
    {
        return l[num];
    }
    int mid=(s+e)>>1,re=0;
    if(a<=mid)re+=query(num<<1,s,mid,a,b);
    if(b>mid)re+=query(num<<1|1,mid+1,e,a,b);
    return re;
}
int main()
{
    int n,m,tcase,i,j,t=1,tempa,tempb;
    scanf("%d",&tcase);
    while(tcase--)
    {
        scanf("%d%d",&n,&m);
        build(1,1,n);
        for(i=1;i<=n;i++)
        {
            scanf("%d",&no[i].x);
            no[i].id=i;
        }
        sort(no+1,no+1+n,cmp1);
        for(i=1;i<=m;i++)
        {
            scanf("%d%d%d",&tempa,&tempb,&qu[i].h);
           qu[i].l=tempa+1; qu[i].r=tempb+1;qu[i].id=i;
        }
        sort(qu+1,qu+1+m,cmp2);
         for(j=1,i=1;j<=m;j++)
         {
            while(i<=n&&no[i].x<=qu[j].h)
            {
                update(1,1,n,no[i].id);
                i++;
            }
            ans[qu[j].id]=query(1,1,n,qu[j].l,qu[j].r);
         }
        printf("Case %d:\n",t++);
        for(i=1;i<=m;i++)
        printf("%d\n",ans[i]);
    }
    return 0;
}


你可能感兴趣的:(hdu4417 Super Mario)