C - 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个数,询问区间[l,r]内小于h的数的个数。

思路:分块,先将每个块的数值排序,如果查询到完整块就upper_bound二分找到小于h的个数。如果是不完整块就暴力。

 

代码:


//Full of love and hope for life

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <queue>
#define inf 0x3f3f3f3f
//https://paste.ubuntu.com/

using namespace std;

typedef long long ll;
const int maxn=100010;

int a,b;
int L[maxn],R[maxn];
int belong[maxn];
int num;
int n[maxn],m[maxn]; // 存分块排好序的n值

void creat(){
    int block=(int)sqrt(a);
    num=a/block;
    if(a%block){
        num++;
    }
    for(int i=1;i<=num;i++){
        L[i]=(i-1)*block+1;
        R[i]=i*block;
    }
    R[num]=a;
    for(int i=1;i<=a;i++){
        belong[i]=(i-1)/block+1;
    }
}

int query(int l,int r,int v){
     int sum=0;
     if(belong[l]==belong[r]){
        for(int i=l;i<=r;i++){
            if(n[i]<=v){
                sum++;
            }
        }
        return sum;
     }
     for(int i=l;i<=R[belong[l]];i++){
        if(n[i]<=v){
            sum++;
        }
     }
     for(int i=belong[l]+1;i<belong[r];i++){
        sum+=upper_bound(m+L[i],m+R[i]+1,v)-(m+L[i]);
     }
     for(int i=L[belong[r]];i<=r;i++){
        if(n[i]<=v){
            sum++;
        }
     }
     return sum;
}

int main(){
    ios::sync_with_stdio(false);
    int T,k=0;
    cin >> T;
    while(T--){
        k++;
        cin >> a >> b;
        creat();
        for(int i=1;i<=a;i++){
            cin >> n[i];
            m[i]=n[i];
        }
        for(int i=1;i<=num;i++){
            sort(m+L[i],m+R[i]+1);
        }
        cout << "Case " << k << ":" << endl;
        int x,y,v;
        while(b--){
            cin >> x >> y >> v;
            cout << query(x+1,y+1,v) << endl;
        }
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZZ --瑞 hopeACMer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值