Super Mario
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4382 Accepted Submission(s): 2033
Problem Description
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.)
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
Source
题意:找出区间内小于等于H的数的个数。
思路:把点按高度从小到大排序,把查询按高度从小到大排序,每次插入小于等于当前查询高度的点,再求出sum(R)-sum(L-1)。
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define maxn 100010
using namespace std;
int bit[maxn], ask[maxn];
int n, m;
struct point{
int h, los;
}a[maxn];
struct node{
int l, r, h;
int id;
}b[maxn];
bool cmp1(node &a, node &b){
return a.h<b.h;
}
bool cmp2(point &a, point &b){
return a.h<b.h;
}
void add(int i, int x){
while(i <= n){
bit[i] += x;
i += i&-i;
}
}
int sum(int i){
int s = 0;
while(i > 0){
s += bit[i];
i -= i&-i;
}
return s;
}
int main()
{
int i, j, T, cas;
scanf("%d", &T);
cas = 1;
while(T--){
memset(bit, 0, sizeof bit);
scanf("%d %d", &n, &m);
for(i = 0;i < n;i++){
scanf("%d", &a[i].h);
a[i].los = i;
}
for(i = 0;i < m;i++){
scanf("%d %d %d", &b[i].l, &b[i].r, &b[i].h);
b[i].id = i;
}
sort(a, a+n, cmp2);
sort(b, b+m, cmp1);
j = 0;
for(i = 0;i < m;i++){
int tmp = b[i].h;
while(j<n&&a[j].h<=tmp){
add(a[j].los+1, 1);
j++;
}
ask[b[i].id] = sum(b[i].r+1)-sum(b[i].l);
}
printf("Case %d:\n", cas++);
for(i = 0;i < m;i++)
printf("%d\n", ask[i]);
}
}