hdu 4417 Super Mario 树状数组+离线操作

Super Mario

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 652    Accepted Submission(s): 355


Problem 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

 

  这个题目是第37届ACM杭州赛区网络预选赛的1008题.比赛的时候一下子没有想到这个方法,于是去做1005题目了,做完了以后心情突然很好,然后这个题就突然来了灵感,于是马上按照思路实现,中间出现过小失误,最终AC.当然这个题也可以用线段树,但是线段树编码复杂,消耗空间大,而且效率不如树状数组,所以我个人觉得能用数状数组就不要用线段树.

  题目的主要思路是把输入数据读入保存做离线处理,具体操作是,把初始话数据按照高度从高到低排序,然后把问题也按照高度从高到低排序,当然要给数据加个ID方便后面恢复.然后用循环把比当前问题高度小或者相等的初始数据按照初始位置插入,然后计数即可。

下面上代码。

 

#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=100010;
struct tnode
{
    int val;
    int id;
}data[maxn];
int n;
struct qnode
{
    int l,r;
    int h;
    int id;
}q[maxn];
int a[maxn];
int myans[maxn];
void add(int i)
{
    for (;i<=n;i+=i&(-i))
        a[i]++;
}
int getsum(int i)
{
    int ans=0;
    for (;i>0;i-=i&(-i))
        ans+=a[i];
    return ans;
}

int cmp2(qnode a,qnode b)
{
    return a.h<b.h;
}
int cmp(tnode a,tnode b)
{
    return a.val<b.val;
}
int main()
{
    int t;
    int m;
    int i,j;
    int tmp;
    int cas=1;
    scanf("%d",&t);

    while(t--)
    {
        scanf("%d%d",&n,&m);
        memset(a,0,sizeof(a));
        for (i=1;i<=n;i++)
        {
            scanf("%d",&data[i].val);
            data[i].id=i;
        }
        
        for (i=1;i<=m;i++)
        {
            scanf("%d%d%d",&q[i].l,&q[i].r,&q[i].h);
            q[i].id=i;
        }
        sort(data+1,data+1+n,cmp);
        sort(q+1,q+1+m,cmp2);
        for (i=1,j=1;i<=m;i++)
        {
           for (;j<=n&&data[j].val<=q[i].h;j++)
                 add(data[j].id);
           myans[q[i].id]=getsum(q[i].r+1)-getsum(q[i].l);
        }
        printf("Case %d:\n",cas++);
        for (i=1;i<=m;i++)
            printf("%d\n",myans[i]);
    }

    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值