Time Limit: 20000MS | Memory Limit: 65536K | |
Total Submissions: 52302 | Accepted: 17964 | |
Case Time Limit: 2000MS |
Description
You are working for Macrohard company in data structures department. After failing your previous task about key insertion you were asked to write a new data structure that would be able to return quickly k-th order statistics in the array segment.
That is, given an array a[1...n] of different integer numbers, your program must answer a series of questions Q(i, j, k) in the form: "What would be the k-th number in a[i...j] segment, if this segment was sorted?"
For example, consider the array a = (1, 5, 2, 6, 3, 7, 4). Let the question be Q(2, 5, 3). The segment a[2...5] is (5, 2, 6, 3). If we sort this segment, we get (2, 3, 5, 6), the third number is 5, and therefore the answer to the question is 5.
That is, given an array a[1...n] of different integer numbers, your program must answer a series of questions Q(i, j, k) in the form: "What would be the k-th number in a[i...j] segment, if this segment was sorted?"
For example, consider the array a = (1, 5, 2, 6, 3, 7, 4). Let the question be Q(2, 5, 3). The segment a[2...5] is (5, 2, 6, 3). If we sort this segment, we get (2, 3, 5, 6), the third number is 5, and therefore the answer to the question is 5.
Input
The first line of the input file contains n --- the size of the array, and m --- the number of questions to answer (1 <= n <= 100 000, 1 <= m <= 5 000).
The second line contains n different integer numbers not exceeding 109 by their absolute values --- the array for which the answers should be given.
The following m lines contain question descriptions, each description consists of three numbers: i, j, and k (1 <= i <= j <= n, 1 <= k <= j - i + 1) and represents the question Q(i, j, k).
The second line contains n different integer numbers not exceeding 109 by their absolute values --- the array for which the answers should be given.
The following m lines contain question descriptions, each description consists of three numbers: i, j, and k (1 <= i <= j <= n, 1 <= k <= j - i + 1) and represents the question Q(i, j, k).
Output
For each question output the answer to it --- the k-th number in sorted a[i...j] segment.
Sample Input
7 3 1 5 2 6 3 7 4 2 5 3 4 4 1 1 7 3
Sample Output
5 6 3
Hint
This problem has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.
Source
Northeastern Europe 2004, Northern Subregion
主席树入门题吧,这种东西自己画个图就懂了,:-(。
1 #include <algorithm> 2 #include <iostream> 3 #include <cstdlib> 4 #include <cstdio> 5 using namespace std; 6 const int N = 100000 + 11 ; 7 int n,m,tot,num[N],rank[N],root[N]; 8 9 struct node 10 { 11 int l,r,sum; 12 } T[N*40]; int T_cnt; 13 struct A 14 { 15 int num,id; 16 bool operator < (const A &a) const 17 { 18 return num < a.num; 19 } 20 } ins[N]; 21 22 void update(int l,int r,int &x ,int y,int pos) 23 { 24 T[++T_cnt] = T[y] ; x = T_cnt, ++T[T_cnt].sum; 25 if(l == r) return ; 26 int mid = (l + r) >> 1; 27 28 if(mid >= pos) update(l,mid,T[x].l,T[y].l,pos); 29 else update(mid + 1, r, T[x].r,T[y].r,pos); 30 } 31 32 int query(int l,int r,int x, int y , int k) 33 { 34 if( l == r ) return l ; 35 int sum = T[T[y].l].sum - T[T[x].l].sum; 36 int mid = (l + r) >> 1; 37 38 if(sum >= k) return query(l,mid,T[x].l,T[y].l,k); 39 else return query(mid+1,r,T[x].r,T[y].r,k-sum); 40 } 41 42 int main() 43 { 44 scanf("%d%d",&n,&m); 45 for(int x = 1 ; x <= n ; ++x) 46 { 47 scanf("%d",&ins[x].num); 48 ins[x].id = x ; 49 } 50 sort(ins + 1, ins + 1 + n); 51 for(int x = 1 ; x <= n; ++x) rank[ins[x].id] = x; 52 for(int x = 1; x <= n; ++x) update(1,n,root[x],root[x-1],rank[x]); 53 int l , r , k ; 54 for(int x = 1 ; x <= m; ++x) 55 { 56 scanf("%d%d%d",&l,&r,&k); 57 int i = query(1,n,root[l-1],root[r],k); 58 printf("%d ",ins[i].num); 59 } 60 61 return 0; 62 }