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;
    }
    
  • 相关阅读:
    __x__(10)0906第三天__字符实体(转义字符)
    __x__(9)0906第三天__常见的标签
    __x__(8)0906第三天__乱码问题
    广工校赛——小明在工作
    广工校赛——01串也疯狂之光棍也有伴
    POJ2488——DFS——A Knight's Journey
    josephus问题
    链表(排序和删除)
    Buy Tickets
    敌兵布阵
  • 原文地址:https://www.cnblogs.com/WABoss/p/6027496.html
Copyright © 2011-2022 走看看