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
AC代码
//#include<bits/stdc++.h>
#define _CRT_SBCURE_NO_DEPRECATE
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
//#define UP(i,x,y) for(int i=x;i<=y;i++)
//#define DOWN(i,x,y) for(int i=x;i>=y;i--)
//#define sd(x,y,z) scanf("%d%d%d", &x, &y, &z)
#define sd(x,y) scanf("%d%d", &x, &y)
#define sd(x) scanf("%d", &x)
//#define mp make_pair
#define pb push_back
using namespace std;
typedef long long ll;
#define MOD 1000000007
const int maxn = 101000;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
int n, m, t, _n;
int d[maxn];
struct node
{
ll val;
int pos;
};
node arr[maxn];
struct que
{
ll v;
int l, r;
int pos;
int ans;
};
que q[maxn];
int lowbit(int x)
{
return x&(-x);
}
void add(int x, int v)
{
while(x< maxn)
{
d[x] += v;
x += lowbit(x);
}
}
int query(int x)
{
int res = 0;
while(x)
{
res += d[x];
x -= lowbit(x);
}
return res;
}
bool cmp(node a, node b)
{
return a.val<b.val;
}
bool cmpa(que a, que b)
{
return a.v<b.v;
}
bool cmpb(que a, que b)
{
return a.pos<b.pos;
}
int main()
{
//freopen("out.txt", "w", stdout);
scanf("%d", &t);
int a = 1;
while(t--)
{
scanf("%d%d", &n,&m);
for(int i = 1; i <= n; i++)
{
scanf("%lld", &arr[i].val);
arr[i].pos = i;
}
sort(arr+1, arr+n+1, cmp);
for(int i = 1; i <= m; i++)
{
scanf("%d%d%lld", &q[i].l, &q[i].r, &q[i].v);
q[i].l++; q[i].r++;
q[i].pos = i;
}
sort(q+1,q+m+1,cmpa);
int tt = 1;
memset(d, 0, sizeof d);
for(int i = 1; i <= m; i++)
{
while (arr[tt].val <= q[i].v && tt <= n)
{
add(arr[tt++].pos, 1);
}
q[i].ans = query(q[i].r) - query(q[i].l-1);
}
sort(q+1,q+m+1,cmpb);
printf("Case %d:\n", a++);
for(int i = 1; i <= m; i++)
{
printf("%d\n", q[i].ans);
}
}
return 0;
}