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
题目大意:查询区间内小于等于马里奥高度的数字的个数
lower_bound和upper_bound
在算法章节,我们经常需要使用分治,C++函数就为我们准备了 lower_bound和upper_bound,用于二分查找
头文件:algorithm
注意:需要一个非降序列!
lower_bound
lower_bound返回值是一个迭代器,返回指向比key大(或等)的第一个值的位置
用法:
lower_bound(头地址,尾地址,key);
key为查找值当未查找到key时,将输出地址:查找尾地址+1,值:key;
upper_bound
upper_bound返回值是一个迭代器,返回指向比key大的第一个值的位置
用法:
upper_bound(头地址,尾地址,key);
key为查找值当未查找到key时,将输出地址:查找尾地址+1,值:key;
概括:把每个块内的数字排成升序序列,然后在查找的时候就可以通过块的长度和找到的位置来确定数字的个数
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
const double pi = acos(-1.0);
const ll mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + 5;
ll belong[maxn], l[maxn], r[maxn], num;
ll a[maxn], b[maxn];
int n, m, ans;
void build()
{
int block = sqrt(n);
num = n / block;
if(n%num != 0) num++;
for(int i=1;i<=num;i++)
{
l[i] = (i - 1) * block + 1;
r[i] = i * block;
}
r[num] = n;
for(int i=1;i<=n;i++)
{
belong[i] = (i - 1) / block + 1;
}
}
int query(int x, int y, int z)
{
ans = 0;
if(belong[x] == belong[y])
{
for(int i=x;i<=y;i++)
{
if(a[i] <= z) ans++;
}
return ans;
}
else
{
for(int i=x;i<=r[belong[x]];i++)
{
if(a[i] <= z) ans++;
}
for(int i=belong[x] + 1;i<belong[y];i++)
{
int pos = upper_bound(b + l[i], b + r[i] + 1, z) - b - l[i];
ans += pos;
}
for(int i=l[belong[y]];i<=y;i++)
{
if(a[i] <= z) ans++;
}
return ans;
}
}
int main()
{
ios::sync_with_stdio(false);
int t;
cin >> t;
int T = 1;
while(t--)
{
cin >> n >> m;
for(int i=1;i<=n;i++)
{
cin >> a[i];
b[i] = a[i];
}
build();
for(int i=1;i<=num;i++)
{
sort(b + l[i], b + r[i] + 1);
}
cout << "Case " << T++ << ":" << endl;
while(m--)
{
int x, y, z;
cin >> x >> y >> z;
x++;
y++;
cout << query(x, y, z) << endl;
}
}
return 0;
}