Super Mario (分块做法,适合初学的我们)

题目直通车
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.
题目大意:

马里奥是一个举世闻名的管道工,他的跳跃能力让我们钦佩。在一条长度为n的道路上,在每个整数点i的位置都有一个高度为hi的障碍物。现在的问题是:假设马里奥可以跳跃的最高高度为H,在道路的[L,R] 区间内他可以跳跃过的障碍物有多少个(不要考虑他被挡住)
(大意来自https://blog.csdn.net/qq_41289920/article/details/99710618)

#include<iostream>
#include<math.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
const int N=1e5+10;
 int a[N];
 int b[N];
 int l[N],r[N];
int biao[N];
int tot;
int n,m;
int sizes;

void built()
{   sizes=sqrt(n);
    tot=n/sizes;
    if(n%sizes) tot++;
    for(int i=1;i<=tot;i++)
        l[i]=(i-1)*sizes+1,r[i]=i*sizes;
    r[tot]=n;
for(int i=1;i<=n;i++)
    biao[i]=(i+sizes-1)/sizes;
for(int i=1;i<=n;i++)
    {scanf("%d",&a[i]);
     b[i]=a[i];
    }
  for(int i=1;i<=n;i++)
        sort(b+l[biao[i]],b+1+r[biao[i]]);

}
int get(int x,int y, int h)
{int ans=0;
 if(biao[x]==biao[y])
      {for(int i=x;i<=y;i++)
          {if(h>=a[i])
            ans++;

          }
      }
  else
  {int t1=sizes*biao[x];
   int t2=sizes*(biao[y]-1)+1;
   for(int i=x;i<=t1;i++)
   {if(h>=a[i])
      {ans++;

      }
   }
   for(int i=t2;i<=y;i++)
   {if(h>=a[i])
       {ans++;

       }
   }
for(int i=biao[x]+1;i<biao[y];i++)
        ans+=upper_bound(b+l[i],b+r[i]+1,h)-b-l[i];

  }
 return ans;
}


int main()
{int t;
 cin>>t;
 int time=0;
 while(t--)
    {scanf("%d%d",&n,&m);
     memset(a,0,sizeof(a));
     memset(b,0,sizeof(b));


     built();
     int l,r;
        int h;

     cout<<"Case "<<++time<<":"<<endl;
     for(int f=1;f<=m;f++)
        {
        scanf("%d%d%d",&l,&r,&h);
         printf("%d\n",get(l+1,r+1,h));

        }


    }

}

如有疑问,请下方留言

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值