zoukankan      html  css  js  c++  java
  • Super Mario HDU

    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.

    InputThe 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.)OutputFor 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
    题解:和查询区间第k小差不多,只不过修改了下询问函数,主要坑点就是特判下特殊情况。
    #include<iostream>
    #include<cstring>
    #include<string>
    #include<stdio.h>
    #include<algorithm>
    using namespace std;
    const int maxn=100010;
    int lson[maxn*20],rson[maxn*20],sum[maxn*20],root[maxn],a[maxn];
    int cnt,lsh[maxn];
    void build(int &rt,int l,int r)
    {
        rt=++cnt;
        if(l==r)return ;
        int mid=(l+r)/2;
        build(lson[rt],l,mid);
        build(rson[rt],mid+1,r);
    }
    int update(int last,int l,int r,int pos)
    {
        int now=++cnt;
        lson[now]=lson[last],rson[now]=rson[last],sum[now]=sum[last]+1;
        if(l==r)return now;
        int mid=(l+r)/2;
        if(pos<=mid)lson[now]=update(lson[now],l,mid,pos);
        else rson[now]=update(rson[now],mid+1,r,pos);
        return now;
    }
    int querysum(int L,int R,int l,int r,int pos)
    {
        int mid=(l+r)/2;
        int ans=0;
        if(l==r){
            return sum[R]-sum[L];
        }
        if(pos<=mid){
            ans+=querysum(lson[L],lson[R],l,mid,pos);
        }
        else{
            ans+=sum[lson[R]]-sum[lson[L]];//左区间的全部符合题意,直接加上
            ans+=querysum(rson[L],rson[R],mid+1,r,pos);
        }
        return ans;
    }
    int main()
    {
        ios::sync_with_stdio(0);
        int T,top=1;
        cin>>T;
        while(T--){
            memset(lson,0,sizeof(lson));
            memset(rson,0,sizeof(rson));
            memset(sum,0,sizeof(rson));
            memset(root,0,sizeof(root));
            cnt=0;
            int n,m;
            cin>>n>>m;
            for(int i=1;i<=n;i++){
                cin>>a[i];
                lsh[i]=a[i];
            }
            sort(lsh+1,lsh+1+n);
            int len=unique(lsh+1,lsh+1+n)-lsh-1;
            build(root[0],1,len);
            for(int i=1;i<=n;i++){
                int pos=lower_bound(lsh+1,lsh+1+len,a[i])-lsh;
                root[i]=update(root[i-1],1,len,pos);
            }
            cout<<"Case "<<top++<<":"<<endl;
            for(int i=1;i<=m;i++){
                int L,R,k;
                cin>>L>>R>>k;
                L++,R++;
                int pos=upper_bound(lsh+1,lsh+1+len,k)-lsh-1;
                if(pos==0){//如果pos=0,不能进去querysum函数,否则会得到错误的结果
                    cout<<0<<endl;
                    continue;
                }
                cout<<querysum(root[L-1],root[R],1,len,pos)<<endl;
            }
        }
        return 0;
    }
  • 相关阅读:
    sigaction函数解析
    实战Nginx与PHP(FastCGI)的安装、配置与优化
    Linux下Nginx+PHP 简单安装配置
    Nginx安装配置PHP(FastCGI)环境的教程
    Linux上配置Nginx+PHP5(FastCGI)
    @JoinTable和@JoinColumn
    Spring Data JPA 之 一对一,一对多,多对多 关系映射
    MyChrome制作Chrome浏览器便携版
    注解@CrossOrigin解决跨域问题
    MySQL查看表结构及查看建表语句
  • 原文地址:https://www.cnblogs.com/cherish-lin/p/10982121.html
Copyright © 2011-2022 走看看