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.

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

题意:
n个数,求所给区间内小于等于h的数的个数

思路:
分块,在每一块上排序,每整一块上利用upper_bound确定满足条件的元素个数,小块上用纯暴力即可
有关说明见代码注释

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t, i, j, k, n, m, temp, left, right, u, v, h;
    int a[100005], before[100005], div[100005];
    int re, x;
    scanf("%d", &t);
    for(j=1;j<=t;j++)
    {
        scanf("%d %d", &n, &m);
        temp = sqrt(n);
        for(i=0;i<n;i++)
        {
            scanf("%d", &a[i]);
            before[i] = a[i];    //把原来的数组存起来,不参与后面的块内排序,
                                 //用于之后不完整块上的求解
            div[i] = i/temp + 1;        //分块
        }
        k = n/temp;
        if(temp*temp<n) k++;      //k为总块数,这句判断的的是是否后面有小块
        for(i=0;i<k;i++)          //对每一块进行块内排序
        {
            left = i * temp;
            right = min(left + temp, n);
            sort(a + left, a + right);
        }
        printf("Case %d:\n", j);
        while(m--)
        {
            scanf("%d %d %d", &u, &v, &h);
            re = 0;
            if(div[u]==div[v])             //同一块,对原数组(before)暴力,得正解
            {
                for(i=u; i<=v; i++)
                {
                    if(before[i]<=h) re++;
                }
            }
            else
            {
                for(i=div[u]+1;i<=div[v]-1;i++)          //对于中间的整块,因之前排过序,
                                                  //此时能简单求得每块中求得满足条件的个数
                {
                    left = (i - 1) * temp;
                    right = min(left + temp, n);
                    x = upper_bound(a + left, a + right, h) - a;
                    //upper_bound(地址left,地址right,要比较的数字),得到的是第一个大于h的地址,
                    //再将结果减去数组a首地址得到的是第一个大于h的下标
                    re += (x - left);
                }
                for(i=u;div[i]==div[u];i++)     //暴力求两边小块
                {
                    if(before[i]<=h) re++;
                }
                for(i=v;div[i]==div[v];i--)
                {
                    if(before[i]<=h) re++;
                }
            }
            printf("%d\n", re);
        }
    }
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值