hdu 4417 Super Mario
Super Mario
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 823 Accepted Submission(s): 451
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
Source
Recommend
liuyiding
//划分树,本来划分树是求区间第k大元素的、现在换个角度,给定区间和某个数,求改数在该区间的排名、、、、
//只要修改下query函数就可以了
#include <iostream> #include <stdio.h> #include <math.h> #include <string.h> #include <algorithm> using namespace std; #define N 100002 #define lson l,m,lv+1 #define rson m+1,r,lv+1 int tr[22][N]; int num[22][N]; int st[N]; int ar[N]; void build(int l,int r,int lv) { if(l==r) return; int m=(l+r)>>1; int same=0,i; for(i=m;i>=l;i--) if(st[i]==st[m]) same++; else break; int lid=l,rid=m+1; for(i=l;i<=r;i++) if(tr[lv][i]<st[m]) { num[lv][i]=i==l?1:num[lv][i-1]+1; tr[lv+1][lid++]=tr[lv][i]; } else if(tr[lv][i]==st[m]&&same>0) { num[lv][i]=i==l?1:num[lv][i-1]+1; tr[lv+1][lid++]=tr[lv][i]; same--; } else { num[lv][i]=i==l?0:num[lv][i-1]; tr[lv+1][rid++]=tr[lv][i]; } build(lson); build(rson); } int nm; void query(int L,int R,int k,int l,int r,int lv) { if(l==r) { if(k>=tr[lv][l]) nm++; return ; } int m=(l+r)>>1; int cnt=num[lv][l+R-1]-(L==1?0:num[lv][l+L-2]); if(st[m]>k) { if(cnt==0) return; int lid=L==1?1:(num[lv][l+L-2]+1); int rid=cnt+lid-1; query(lid,rid,k,lson); } else { nm+=cnt; int lid=L==1?1:(L-num[lv][l+L-2]); int rid=R-num[lv][l+R-1]; if(rid<lid) return;//没有这步,在样例 中测试8 8 8 就会错,这个我在纸上一步步写出来才发现、、纠结的细节哈、、 query(lid,rid,k,rson); } } int main() { int T,t=1; int n,m; int i; scanf("%d",&T); while(T--) { scanf("%d %d",&n,&m); for(i=0;i<n;i++) { scanf("%d",&tr[0][i]); st[i]=tr[0][i]; } sort(st,st+n); n--; build(0,n,0); int l,r,k; printf("Case %d:\n",t++); for(i=0;i<m;i++) { scanf("%d %d %d",&l,&r,&k); nm=0; query(l+1,r+1,k,0,n,0); printf("%d\n",nm); } } return 0; }