Super Mario
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3292 Accepted Submission(s): 1538
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
题目大意:给你一个长度为n的数组,然后m次查询,每次询问问你l到r区间有多少个数是小于等于H的。
思路:直接写个主席树求第k大的数,然后二分在第1到第r-l+1大的数是否小于等于H,找到刚好小于等于H的那个数,然后答案就是那个那个小于等于那个数的个数的总和,主席树里面已经维护了那个数的总和,直接求取就行。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <queue>
typedef __int64 LL;
using namespace std;
const int maxn = 100005;
int n,m;
int T[maxn],a[maxn],b[maxn];
int lson[maxn * 20],rson[maxn * 20],c[maxn*20];
int tot,num;
void hase(int k)
{
sort(b,b+k);
num = unique(b,b+k) - b;
}
int get_hase(int now)
{
return (lower_bound(b,b+num,now) - b);
}
int build(int l,int r)
{
int newroot = tot++;
c[newroot] = 0;
if(l != r)
{
int mid = (l + r) >> 1;
lson[newroot] = build(l,mid);
rson[newroot] = build(mid + 1,r);
}
return newroot;
}
int ans;
int insert1(int root,int pos,int val)
{
int newroot = tot++;
int tmp = newroot;
c[newroot] = c[root] + val;
int l = 0,r = num - 1;
while(l < r)
{
int mid = (l + r) >> 1;
if(pos <= mid)
{
lson[newroot] = tot++;
rson[newroot] = rson[root];
root = lson[root];
newroot = lson[newroot];
r = mid;
}
else
{
lson[newroot] = lson[root];
rson[newroot] = tot++;
root = rson[root];
newroot = rson[newroot];
l = mid + 1;
}
c[newroot] = c[root] + val;
}
return tmp;
}
int qurry(int ll,int rr,int pos)
{
int rootl = T[ll];
int rootr = T[rr];
int l = 0,r = num - 1;
ans = 0;
while(l < r)
{
int mid = (l + r) >> 1;
int cou = c[lson[rootr]] - c[lson[rootl]];
if(cou < pos)
{
pos -= cou;
rootr = rson[rootr];
rootl = rson[rootl];
l = mid + 1;
ans += cou;
}
else
{
rootr = lson[rootr];
rootl = lson[rootl];
r = mid;
}
}
ans += c[rootr] - c[rootl];
return l;
}
int main()
{
int t;
scanf("%d",&t);
int ca = 1;
while(t--)
{
scanf("%d %d",&n,&m);
int i,j;
num = tot = 0;
for(i = 1; i <= n; i++)
{
scanf("%d",&a[i]);
b[num++] = a[i];
}
hase(num);
T[0] = build(0,num-1);
for(i = 1; i <= n; i++)
{
T[i] = insert1(T[i-1],get_hase(a[i]),1);
}
printf("Case %d:\n",ca++);
while(m--)
{
int q,w,h;
scanf("%d %d %d",&q,&w,&h);
q++,w++;
int l = 1,r = w - q + 2;
while(r - l > 1)
{
int mid = (l + r) >> 1;
int vv = qurry(q-1,w,mid);
int now = b[vv];
if(b[vv] <= h)
l = mid;
else
r = mid;
}
int vv = qurry(q-1,w,l);
if(b[vv] > h)
{
printf("0\n");
continue;
}
printf("%d\n",ans);
}
}
return 0;
}