hdu4417 Super Mario
地址:http://acm.hdu.edu.cn/showproblem.php?pid=4417
题目:
Super Mario
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6561 Accepted Submission(s): 2805
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
思路:
这题意真是hack人,,看了半天愣是没看懂,原来区间是从0开始的(盲人acmer)
求区间内小于h的数的个数。
这不是主席树模板题吗?
把query随便改改就好了。
1 #include <bits/stdc++.h>
2
3 using namespace std;
4
5 #define MP make_pair
6 #define PB push_back
7 typedef long long LL;
8 typedef pair<int,int> PII;
9 const double eps=1e-8;
10 const double pi=acos(-1.0);
11 const int K=5e6+7;
12 const int mod=1e9+7;
13
14 int tot,ls[K],rs[K],rt[K];
15 int sum[K],a[K],b[K];
16 //sum[o]记录的是该节点区间内出现的数的个数
17 //区间指的是将数离散化后的区间
18 void build(int &o,int l,int r)
19 {
20 o=tot++,sum[o]=0;
21 if(l==r) return ;
22 int mid=l+r>>1;
23 build(ls[o],l,mid),build(rs[o],mid+1,r);
24 }
25 void update(int &o,int p,int l,int r,int x)
26 {
27 o=tot++,ls[o]=ls[p],rs[o]=rs[p],sum[o]=sum[p]+1;
28 if(l==r) return ;
29 int mid=l+r>>1;
30 if(x<=mid) update(ls[o],ls[p],l,mid,x);
31 else update(rs[o],rs[p],mid+1,r,x);
32 }
33 int query(int ra,int rb,int l,int r,int k)
34 {
35 if(k>=r) return sum[ra]-sum[rb];
36 int mid=l+r>>1,ans=0;
37 if(mid<=k) ans+=sum[ls[ra]]-sum[ls[rb]];
38 else ans+=query(ls[ra],ls[rb],l,mid,k);
39 if(k>mid) ans+=query(rs[ra],rs[rb],mid+1,r,k);
40 return ans;
41 }
42 int main(void)
43 {
44 int T,n,m,cnt=1;scanf("%d",&T);
45 while(T--)
46 {
47 tot=0;
48 scanf("%d%d",&n,&m);
49 for(int i=1;i<=n;i++)
50 scanf("%d",a+i),b[i]=a[i];
51 sort(b+1,b+1+n);
52 int sz=unique(b+1,b+1+n)-b-1;
53 for(int i=1;i<=n;i++)
54 a[i]=lower_bound(b+1,b+1+sz,a[i])-b;
55 build(rt[0],1,sz);
56 for(int i=1;i<=n;i++)
57 update(rt[i],rt[i-1],1,sz,a[i]);
58 int l,r,h,ans,x;
59 printf("Case %d:\n",cnt++);
60 while(m--)
61 {
62 scanf("%d%d%d",&l,&r,&h);
63 l++,r++;
64 if(h<b[1])
65 ans=0;
66 else
67 {
68 x=lower_bound(b+1,b+1+sz,h)-b;
69 if(b[x]>h||x>sz)x--;
70 ans=query(rt[r],rt[l-1],1,sz,x);
71 }
72 printf("%d\n",ans);
73 }
74 }
75 return 0;
76 }
作者:weeping
出处:www.cnblogs.com/weeping/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。