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]],查询到叶子时也把两者之差加上。

 

 1 //It is made by wfj_2048~
 2 #include <algorithm>
 3 #include <iostream>
 4 #include <cstring>
 5 #include <cstdlib>
 6 #include <cstdio>
 7 #include <vector>
 8 #include <cmath>
 9 #include <queue>
10 #include <stack>
11 #include <map>
12 #include <set>
13 #define inf 1<<30
14 #define il inline
15 #define RG register
16 #define ll long long
17 #define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
18 
19 using namespace std;
20 
21 int sum[3000010],ls[3000010],rs[3000010],root[100010],num[100010],a[100010],hashh[100010],n,m,sz,tot;
22 
23 il int gi(){
24     RG int x=0,q=0; RG char ch=getchar(); while ((ch<'0' || ch>'9') && ch!='-') ch=getchar();
25     if (ch=='-') q=1,ch=getchar(); while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar(); return q ? -x : x;
26 }
27 
28 il void insert(RG int x,RG int &y,RG int l,RG int r,RG int v){
29     y=++sz,sum[y]=sum[x]+1,ls[y]=ls[x],rs[y]=rs[x]; if (l==r) return; RG int mid=(l+r)>>1;
30     if (v<=mid) insert(ls[x],ls[y],l,mid,v); else insert(rs[x],rs[y],mid+1,r,v);
31 }
32 
33 il int query(RG int x,RG int y,RG int l,RG int r,RG int v){
34     if (l==r) return sum[y]-sum[x]; RG int mid=(l+r)>>1;
35     if (v<=mid) return query(ls[x],ls[y],l,mid,v);
36     else return sum[ls[y]]-sum[ls[x]]+query(rs[x],rs[y],mid+1,r,v);
37 }
38 
39 il void work(){
40     n=gi(),m=gi(); for (RG int i=1;i<=n;++i) num[i]=a[i]=gi(); sort(num+1,num+n+1);
41     hashh[++tot]=num[1]; for (RG int i=2;i<=n;++i) if (num[i]!=num[i-1]) hashh[++tot]=num[i];
42     for (RG int i=1;i<=n;++i) insert(root[i-1],root[i],1,tot,lower_bound(hashh+1,hashh+tot+1,a[i])-hashh);
43     for (RG int i=1;i<=m;++i){
44     RG int l=gi()+1,r=gi()+1,h=gi(),H=upper_bound(hashh+1,hashh+tot+1,h)-hashh-1;
45     if (!H) printf("0\n"); else printf("%d\n",query(root[l-1],root[r],1,tot,H));
46     }
47     return;
48 }
49 
50 int main(){
51     File("hdu4417");
52     RG int T=gi();
53     for (RG int i=1;i<=T;++i){ sz=tot=0; printf("Case %d:\n",i); work(); }
54     return 0;
55 }

 

posted @ 2017-01-20 16:00  wfj_2048  阅读(137)  评论(0编辑  收藏  举报