Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 823 Accepted Submission(s): 451
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.)
//划分树,本来划分树是求区间第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; }