码迷,mamicode.com
首页 > 其他好文 > 详细

HDU4417 Super Mario(主席树)

时间:2016-11-03 18:50:39      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:区间查询   ble   ping   nbsp   int   start   amp   string   代码   

题目

Source

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

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;
#define MAXN 100010

int tn,root[MAXN],lch[MAXN*17],rch[MAXN*17],sum[MAXN*17];
int x,y;
void update(int i,int j,int a,int &b){
    b=++tn;
    if(i==j){
        sum[b]=sum[a]+1;
        return;
    }
    lch[b]=lch[a]; rch[b]=rch[a];
    int mid=i+j>>1;
    if(x<=mid) update(i,mid,lch[a],lch[b]);
    else update(mid+1,j,rch[a],rch[b]);
    sum[b]=sum[lch[b]]+sum[rch[b]];
}
int query(int i,int j,int a,int b){
    if(x>y) return 0;
    if(x<=i && j<=y){
        return sum[b]-sum[a];
    }
    int mid=i+j>>1,ret=0;
    if(x<=mid) ret+=query(i,mid,lch[a],lch[b]);
    if(y>mid) ret+=query(mid+1,j,rch[a],rch[b]);
    return ret;
}

int num[MAXN],nn;
int a[MAXN];
int main(){
    int t,n,m;
    scanf("%d",&t);
    for(int cse=1; cse<=t; ++cse){
        printf("Case %d:\n",cse);
        scanf("%d%d",&n,&m);
        nn=0;
        for(int i=1; i<=n; ++i){
            scanf("%d",a+i);
            num[nn++]=a[i];
        }
        sort(num,num+nn);
        nn=unique(num,num+nn)-num;
        tn=0;
        for(int i=1; i<=n; ++i){
            x=lower_bound(num,num+nn,a[i])-num;
            update(0,nn-1,root[i-1],root[i]);
        }
        int a,b,c;
        while(m--){
            scanf("%d%d%d",&a,&b,&c);
            x=0; y=upper_bound(num,num+nn,c)-num-1;
            printf("%d\n",query(0,nn-1,root[a],root[b+1]));
        }
    }
    return 0;
}

 

HDU4417 Super Mario(主席树)

标签:区间查询   ble   ping   nbsp   int   start   amp   string   代码   

原文地址:http://www.cnblogs.com/WABoss/p/6027496.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!