[HDU4417]Super Mario
[HDU4417]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
思路
我们可以用upper_bound-1
来查找第一个小于等于h的u元素,我们将这个位置记为k。那么[1,k]都是可用的高度(跳得过)。套个权值线段树就好了。也就是可持久化线段树和主席树。
貌似有线段树离线做法?懒得找了。。。。
代码
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define maxn (int)(1e5+1000)
using namespace std;
int
sum[maxn*21],rs[maxn*21],ls[maxn*21],root[maxn],len,a[maxn],sorted[maxn];
int n,m,idx;
int getid(int x){
return lower_bound(sorted+1,sorted+1+len,x)-sorted;
}
int build(int l,int r){
int mid=(l+r)>>1;
int now=++idx;
if(l==r)return now;
ls[now]=build(l,mid);
rs[now]=build(mid+1,r);
return now;
}
int insert(int num,int l,int r,int mark){
int now=++idx;
ls[now]=ls[mark];rs[now]=rs[mark];sum[now]=sum[mark]+1;
if(l==r)return now;
int mid=(l+r)>>1;
if(num<=mid){
ls[now]=insert(num,l,mid,ls[mark]);
}
else{
rs[now]=insert(num,mid+1,r,rs[mark]);
}
return now;
}
int query(int ln,int rn,int l,int r,int h){
if(h<l)return 0;
if(r<=h){
return sum[rn]-sum[ln];
}
int ans=0;
int mid=(l+r)>>1;
ans+=query(ls[ln],ls[rn],l,mid,h)+query(rs[ln],rs[rn],mid+1,r,h);
return ans;
}
void solve(){
memset(sum,0,sizeof(sum));
memset(rs,0,sizeof(rs));
memset(ls,0,sizeof(ls));
memset(root,0,sizeof(root));
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);sorted[i]=a[i];
}
sort(sorted+1,sorted+1+n);
len=unique(sorted+1,sorted+1+n)-sorted-1;
root[0]=build(1,len);
for(int i=1;i<=n;i++){
root[i]=insert(getid(a[i]),1,len,root[i-1]);
}
for(int i=1;i<=m;i++){
int l,r,h;scanf("%d%d%d",&l,&r,&h);l++;r++;
int tmp=upper_bound(sorted+1,sorted+1+len,h)-sorted-1;
printf("%d\n",query(root[l-1],root[r],1,len,tmp));
}
return;
}
int main(){
////freopen("in","r",stdin);
int t;scanf("%d",&t);int cnt=0;
while(t--){
printf("Case %d:\n",++cnt);
solve();
}
return 0;
}