算是一道模板题了,可惜弱的一B的我并不会划分树,花了点时间学了下,回头A了这道题
3s的限时跑了2.8s也是醉了。。。
Description
In this problem you are given a number sequence P consisting of N integer and Pi is the ith element in the sequence. Now you task is to answer a list of queries, for each query, please tell us among [L, R], how many Pi is not less than A and
not greater than B( L<= i <= R). In other words, your task is to count the number of Pi (L <= i <= R, A <= Pi <= B).
Input
In the first line there is an integer T (1 < T <= 50), indicates the number of test cases.
For each case, the first line contains two numbers N and M (1 <= N, M <= 50000), the size of sequence P, the number of queries. The second line contains N numbers Pi(1 <= Pi <= 10^9), the number sequence P. Then there are M lines, each line contains
four number L, R, A, B(1 <= L, R <= n, 1 <= A, B <= 10^9)
Output
For each case, at first output a line ‘Case #c:’, c is the case number start from 1. Then for each query output a line contains the answer.
Sample Input
1 13
5 6
9 5 2 3 6 8 7 3 2 5 1 4
1 13 1 10
1 13 3 6
3 6 3 6
2 8 2 8
1 9 1 9
Sample Output
Case #1:
1
3
7
3
6
9
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 50005; int n,m; int tree[20][maxn]; int lnum[20][maxn]; int sorted[maxn]; void debug(int v){ for(int i = 1; i <= n; i++) printf("%d ",sorted[i]); puts(""); for(int i = 0; i < v; i++){ for(int j = 1; j <= n; j++) printf("%d ",tree[i][j]); puts(""); } } void build(int l,int r,int d){ if(l == r) return; int mid = (l + r) >> 1; int same = mid - l + 1; for(int i = l; i <= r; i++) if(tree[d][i] < sorted[mid]) same --; int lpos = l,rpos = mid + 1; lnum[d][0] = 0; for(int i = l; i <= r; i++){ if(tree[d][i] < sorted[mid]){ lnum[d][i] = lnum[d][i - 1] + 1; tree[d + 1][lpos++] = tree[d][i]; } else if(tree[d][i] == sorted[mid] && same > 0){ same --; lnum[d][i] = lnum[d][i - 1] + 1; tree[d + 1][lpos++] = tree[d][i]; } else{ lnum[d][i] = lnum[d][i - 1]; tree[d + 1][rpos++] = tree[d][i]; } } build(l,mid,d + 1); build(mid + 1,r,d + 1); } int query(int L,int R,int l,int r,int d,int k){ //printf("%d %d %d %d\n",l,r,d,k); if(l == r) return tree[d][l]; int mid = (L + R) >> 1; int cnt = lnum[d][r] - lnum[d][l - 1]; if(cnt >= k){ int newl = L + lnum[d][l - 1] - lnum[d][L - 1]; int newr = newl + cnt - 1; return query(L,mid,newl,newr,d + 1,k); } else{ int newr = r + lnum[d][R] - lnum[d][r]; int newl = newr - (r - l - cnt); return query(mid + 1,R,newl,newr,d + 1,k - cnt); } } void solve(){ int L,R,a,b; scanf("%d%d%d%d",&L,&R,&a,&b); int l = 1,r = (R - L + 2); int v1 = 0,v2 = 0; //找大于等于 while(l < r){ int mid = (l + r) >> 1; int value = query(1,n,L,R,0,mid); if(value < a) l = mid + 1; else if(value >= a) r = mid; } v1 = l; l = 1; r = (R - L + 2); //找大于 while(l < r){ int mid = (l + r) >> 1; int value = query(1,n,L,R,0,mid); if(value <= b){ l = mid + 1; } else if(value > b) r = mid; } v2 = l; printf("%d\n",v2 - v1); } int main(){ int T,Case = 1; scanf("%d",&T); while(T--){ scanf("%d%d",&n,&m); for(int i = 1; i <= n; i++){ scanf("%d",&tree[0][i]); sorted[i] = tree[0][i]; } sort(sorted + 1,sorted + n + 1); build(1,n,0); printf("Case #%d:\n",Case++); for(int i = 0; i < m; i++) solve(); } return 0; } /* 5 5 1 1 1 1 1 1 5 2 3 2 */