HDU 4417- Super Mario 分块+二分 (分块大法好)

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个数,数中有重复的。有m个询问,问的是[L,R] 区间内有多少个数小于等于h。

分析
快速查找——排序(快排),二分

分块大法好呀,昨天把莫队搞懂了,今天敲个分块,弄的很快

顿时觉得自己可以做离线的任何题了

一道主席树(可持久化线段树)直接分块解决

简单粗暴

#include<bits/stdc++.h>
using namespace std;
int n,m;
int init[100005];//最初的
int after[100005];//分块排好序的
int block[100005];//第i个属于第几块
int temp;
int l,r;
int main()
{
    int t;
    scanf("%d",&t);
    for(int cas=1; cas<=t; cas++)
    {
        scanf("%d%d",&n,&m);
        for(int i=0; i<n; i++)
        {
            scanf("%d",&init[i]);
            after[i]=init[i];
        }
        temp=sqrt(n);
        for(int i=0; i<n; i++)
        {
            block[i]=i/temp+1;//分块
        }
        int k;
        if(temp*temp==n)
            k=n/temp;
        else
            k=n/temp+1;//k是多少块
        //temp是标准块有几个元素
        for(int i=0; i<k; i++)
        {
            l=i*temp;
            r=min(l+temp,n);
            sort(after+l,after+r);//分块分别排序
        }
        int x,y,h;
        printf("Case %d:\n",cas);
        for(int q=0; q<m; q++)
        {
            scanf("%d%d%d",&x,&y,&h);
            l=block[x];
            r=block[y];
            int ans=0;
            if(l==r)//同一个块直接数
            {
                for(int i=x; i<=y; i++)
                    if(init[i]<=h)
                        ans++;
                printf("%d\n",ans);
                continue;
            }
            for(int i=l+1; i<=r-1; i++)//不同的块分成三部分前中后   此处为中间的整块
            {
                int ll=(i-1)*temp;
                int rr=min(ll+temp,n);
                int p=upper_bound(after+ll,after+rr,h)-after;
                ans+=(p-ll);
            }
            for(int i=x; i<min(l*temp,n); i++)//前块
            {
                if(init[i]<=h)
                    ans++;
            }
            for(int i=(r-1)*temp; i<=y; i++)//后块
            {
                if(init[i]<=h)
                    ans++;
            }
            printf("%d\n",ans);
        }

    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值