hdu4417 Super Mario

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

正解:主席树

建立从1-i的线段树,每次查询时比较r与l-1之差,如果mid<=H则往左查询,否则往右查询,并加上sum[ls[y]]-sum[ls[x]],查询到叶子时也把两者之差加上。

 //It is made by wfj_2048~
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define inf 1<<30
#define il inline
#define RG register
#define ll long long
#define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout) using namespace std; int sum[],ls[],rs[],root[],num[],a[],hashh[],n,m,sz,tot; il int gi(){
RG int x=,q=; RG char ch=getchar(); while ((ch<'' || ch>'') && ch!='-') ch=getchar();
if (ch=='-') q=,ch=getchar(); while (ch>='' && ch<='') x=x*+ch-,ch=getchar(); return q ? -x : x;
} il void insert(RG int x,RG int &y,RG int l,RG int r,RG int v){
y=++sz,sum[y]=sum[x]+,ls[y]=ls[x],rs[y]=rs[x]; if (l==r) return; RG int mid=(l+r)>>;
if (v<=mid) insert(ls[x],ls[y],l,mid,v); else insert(rs[x],rs[y],mid+,r,v);
} il int query(RG int x,RG int y,RG int l,RG int r,RG int v){
if (l==r) return sum[y]-sum[x]; RG int mid=(l+r)>>;
if (v<=mid) return query(ls[x],ls[y],l,mid,v);
else return sum[ls[y]]-sum[ls[x]]+query(rs[x],rs[y],mid+,r,v);
} il void work(){
n=gi(),m=gi(); for (RG int i=;i<=n;++i) num[i]=a[i]=gi(); sort(num+,num+n+);
hashh[++tot]=num[]; for (RG int i=;i<=n;++i) if (num[i]!=num[i-]) hashh[++tot]=num[i];
for (RG int i=;i<=n;++i) insert(root[i-],root[i],,tot,lower_bound(hashh+,hashh+tot+,a[i])-hashh);
for (RG int i=;i<=m;++i){
RG int l=gi()+,r=gi()+,h=gi(),H=upper_bound(hashh+,hashh+tot+,h)-hashh-;
if (!H) printf("0\n"); else printf("%d\n",query(root[l-],root[r],,tot,H));
}
return;
} int main(){
File("hdu4417");
RG int T=gi();
for (RG int i=;i<=T;++i){ sz=tot=; printf("Case %d:\n",i); work(); }
return ;
}
上一篇:responsive web design


下一篇:Qt编写带频谱的音乐播放器