zoukankan      html  css  js  c++  java
  • HDU4417 Super Mario(主席树)

    题目

    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:
    ",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
    ",query(0,nn-1,root[a],root[b+1]));
            }
        }
        return 0;
    }
    
  • 相关阅读:
    Sqlite官方的查询优化文档
    VC++动态链接库(DLL)编程深入浅出(三)转
    用Python查询手机号码归属地
    Delphi使用迅雷的开放下载引擎下载
    Android基础之一
    VC++动态链接库深入浅出(转)
    在Python脚本中使用Delphi控件
    Python与其他语言结合的参数转换函数PyArg_ParseTuple()
    设计模式之模板方法模式(Template)
    设计模式之简单工厂模式(Simple Factory)
  • 原文地址:https://www.cnblogs.com/WABoss/p/6027496.html
Copyright © 2011-2022 走看看