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;
    }
  • 相关阅读:
    求一个二维数组的最大子矩阵
    在一整型数组中找到此数组中子数组和的最大值
    软件工程个人小项目:写一个程序,分析一个文本文件(英文文章)中各个词出现的频率,并且把频率最高的10个词打印出来
    Redis 为什么这么快?
    在netfarmerwork3.5版本的winform下执行string串中的代码
    c# 反射(Reflection)详解
    string,特殊的引用类型
    c#使用HashSet<T>集合去重
    c# .Net重试机制
    观察者模式
  • 原文地址:https://www.cnblogs.com/cherish-lin/p/10982121.html
Copyright © 2011-2022 走看看