Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9501 Accepted Submission(s): 4017
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.)
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
2012 ACM/ICPC Asia Regional Hangzhou Online
题目连接:HDU4417 Super Mario
题解:求静态区间[l,r]中小于等于k的数的数量,主席树模板题。注意,题目中区间从0开始编号
AC的C++代码:
#include
#include
using namespace std;
const int N=100010;
int a[N],b[N],rt[N*20],ls[N*20],rs[N*20],sum[N*20];
int id;
void build(int &o,int l,int r)
{
o=++id;
sum[o]=0;
if(l==r) return;
int m=(l+r)>>1;
build(ls[o],l,m);
build(rs[o],m+1,r);
}
void update(int &o,int l,int r,int last,int p)
{
o=++id;
ls[o]=ls[last];
rs[o]=rs[last];
sum[o]=sum[last]+1;
if(l==r) return;
int m=(l+r)>>1;
if(p<=m)
update(ls[o],l,m,ls[last],p);
else
update(rs[o],m+1,r,rs[last],p);
}
//查询区间[L,R]内小于等于k的个数
int query(int s,int e,int l,int r,int k)
{
if(b[r]<=k)
return sum[e]-sum[s];
if(l==r)
return 0;
int m=(l+r)>>1;
int sum=0;
if(b[m+1]<=k)
sum+=query(rs[s],rs[e],m+1,r,k);
sum+=query(ls[s],ls[e],l,m,k);
return sum;
}
int main()
{
int T,n,q,l,r,k;
scanf("%d",&T);
for(int t=1;t<=T;t++){
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b+1,b+1+n);
int size=unique(b+1,b+1+n)-(b+1);
id=0;
build(rt[0],1,size);
for(int i=1;i<=n;i++){
a[i]=lower_bound(b+1,b+1+size,a[i])-b;
update(rt[i],1,size,rt[i-1],a[i]);
}
printf("Case %d:\n",t);
while(q--){
scanf("%d%d%d",&l,&r,&k);
l++,r++; //题中是从0开始编号的,因此使它加一从1开始编号
int num=query(rt[l-1],rt[r],1,size,k);
printf("%d\n",num);
}
}
return 0;
}