HDU 4417 Super Mario

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
 

主席树
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=100005;
int n,m,T,a[maxn],b[maxn],c[maxn];
int L[maxn*20],R[maxn*20],sum[maxn*20],tot,frist[maxn];

bool cmp(const int &x,const int &y)
{
    return a[x]<a[y];
}

void insert(int now,int l,int r,int u)
{
    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 (u<=mid) L[tot]=tot+1; else R[tot]=tot+1;
        if (u<=mid) insert(L[now],l,mid,u);
        else insert(R[now],mid+1,r,u);
    }
}

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

int main()
{
    int tt=0;
    scanf("%d",&T);
    while (T--)
    {
        scanf("%d%d",&n,&m);
        for (int i=1;i<=n;i++) scanf("%d",&a[b[i]=i]);
        sort(b+1,b+n+1,cmp);
        sort(a+1,a+n+1);
        for (int i=1;i<=n;i++) c[b[i]]=i;
        memset(frist,0,sizeof(frist));
        frist[0]=L[0]=R[0]=sum[0]=tot=0;
        for (int i=1;i<=n;i++) 
        {
            frist[i]=tot+1;
            insert(frist[i-1],1,n,c[i]);
        }
        printf("Case %d:\n",++tt);
        while (m--)
        {
            int x,y,k;
            scanf("%d%d%d",&x,&y,&k);
            k=upper_bound(a+1,a+n+1,k)-a-1;
            printf("%d\n",query(frist[y+1],frist[x],1,n,k));
        }
    }
    return 0;
}


你可能感兴趣的:(HDU)