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;
    }
    
  • 相关阅读:
    【BZOJ3166】ALO(主席树)
    【UOJ#188】Sanrd(min_25筛)
    伯努利数
    【51Nod1258】序列求和V4(FFT)
    【BZOJ5306】[HAOI2018]染色(NTT)
    【BZOJ4943】【NOI2017】蚯蚓排队(哈希)
    【BZOJ4912】天才黑客(最短路,虚树)
    【BZOJ5333】荣誉称号(动态规划)
    NOI2018前的每日记录
    【BZOJ1088】扫雷(递推)
  • 原文地址:https://www.cnblogs.com/WABoss/p/6027496.html
Copyright © 2011-2022 走看看