zoukankan      html  css  js  c++  java
  • HDU 4417 Super Mario (树状数组/线段树)

    Super Mario

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

    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
    题意:给你一个序列,以及m个询问,每次查询[l,r]里面小于等于h的数的个数
    分析:离线保存序列和询问,排序后将序列从小到大插入,然后对于询问,用树状数组或者线段树查询区间和就行了。
    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    
    const int MAXN = 1E5+100;
    struct Query
    {
        int l,r;
        int h;
        int id;
    }Q[MAXN];
    pair<int,int>p[MAXN];
    int F[MAXN];
    int n,m;
    void update(int x,int val)
    {
        while(x<=n)
        {
            F[x]+=val;
            x+=x&-x;
        }
    }
    int query(int x)
    {
        int res=0;
        while(x>0)
        {
            res+=F[x];
            x-=x&-x;
        }
        return res;
    }
    bool cmp(const Query& aa,const Query& bb)
    {
        return aa.h<bb.h;
    }
    int ans[MAXN];
    
    int main()
    {
        int T;
        int iCase=0;
        scanf("%d",&T);
        while(T--)
        {
            iCase++;
            scanf("%d%d",&n,&m);
            memset(F,0,sizeof(F));
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&p[i].first);
                p[i].second=i;
            }
            for(int i=1;i<=m;i++)
            {
                scanf("%d%d%d",&Q[i].l,&Q[i].r,&Q[i].h);
                Q[i].id=i,Q[i].l++,Q[i].r++;
            }
            sort(Q+1,Q+m+1,cmp);
            sort(p+1,p+n+1);
            int i=1,j=1;
            while(j<=m)
            {
                while(i<=n)
                {
                    if(p[i].first>Q[j].h) break;
                    update(p[i].second,1);
                    i++;
                }
                while(j<=m)
                {
                    if(i<=n&&Q[j].h>=p[i].first) break;
                    ans[Q[j].id]=query(Q[j].r)-query(Q[j].l-1);
                    j++;
                }
            }
            printf("Case %d:
    ",iCase);
            for(int i=1;i<=m;i++) printf("%d
    ",ans[i]);
        }
        return 0;
    }
  • 相关阅读:
    通过修改注册表建立Windows自定义协议
    cmd命令大全
    Sql Server 2012 存储过程的调试
    C#.Net环境下的缓存技术
    WCF引用方式之IIS方式寄宿服务
    关于WCF引用方式之WCF服务寄宿控制台
    tcp_tw_recycle和tcp_timestamps导致connect失败问题
    TCP短连接TIME_WAIT问题解决方法大全
    打开tcp_tw_recycle引起的一个问题
    TCP三次握手,什么情况下client会回复reset
  • 原文地址:https://www.cnblogs.com/wangdongkai/p/5745119.html
Copyright © 2011-2022 走看看