K-th Number (K-Chairman of the large tree template)

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.

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 10  9 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.
 
. 1 #include <cstdio>
 2 #include <Vector>
 . 3 #include <algorithm>
 . 4  the using  namespace STD;
 . 5  
. 6  const  int N = 1E5 + . 5 ;
 . 7  int A [N]; /// input array 
. 8  int the root [N ]; /// the root [i] denotes the i-th segment tree before the array of elements of the root 
. 9  struct node {
 10      int L, R & lt; /// left child node segment tree 
. 11      int SUM; /// statistics sub- number of elements in the tree 
12 is } T [N * 40 ]; /// opening 20-fold or 40-fold
13 is  int NUM = 0 ; /// node number 
14 Vector < int > VEC;
 15  
16  int getId ( int K) {
 . 17      return lower_bound (vec.begin (), vec.end (), K) -vec.begin ( ) + . 1 ;
 18 is  }
 . 19  
20 is  void update ( int L, int R & lt, int & now, int BEF, int K) { /// update operation 
21 is      T [NUM ++] = T [BEF]; /// replicate a segment tree 
22 is      now NUM =;// Update the current root of the tree segment 
23 is      T [NUM] .sum ++ ;
 24      IF (R & lt == L) return ;
 25      int MID = (L + R & lt) >> . 1 ;
 26 is      IF (K <= MID) {
 27          Update (L, MID, T [now] .L, T [BEF] .L, K);
 28      }
 29      the else {
 30          Update (MID + . 1 , R & lt, T [now] .r, T [BEF]. R & lt, K);
 31 is      }
 32  }
 33 is  
34 is  int Query ( int L, int R & lt, int X, int Y,int k) { /// Query the interval [x, y] k-th largest numbers 
35      IF (R & lt == L) return L;
 36      int MID = (L + R & lt) >> . 1 ;
 37 [      int CNT T = [T [y] .L] .sum-T [T [x] .L] .sum; /// y-x particles than the particles tree tree in the left subtree of the plurality of nodes 
38 is      IF (CNT> = K) { /// left subtree 
39          return Query (L, MID, T [X] .L, T [Y] .L, K);
 40      }
 41 is      the else {
 42 is          return Query (MID + . 1 , R & lt, T [ X] .r, T [Y] .r, - K- CNT);
 43 is      }
 44 is  }
 45  
46 is  intmain () {
 47      int n-, m;
 48      Scanf ( " % D% D " , & n-, & m);
 49      for ( int I = . 1 ; I <= n-; I ++ ) {
 50          Scanf ( " % D " , a & [I]);
 51 is          vec.push_back (a [I]);
 52 is      }
 53 is      Sort (vec.begin (), vec.end ());
 54 is      /// UNIQUE to return after the container is not re-repeat sequence the last element of the next element
 55      /// ERASE deleted [left, right) of the element 
56 is      vec.erase (UNIQUE (vec.begin (), vec.end ()), vec.end ());
 57 is      for (int i=1;i<=n;i++){
58         update(1,n,root[i],root[i-1],getid(a[i]));
59     }
60     while(m--){
61         int x,y,k;
62         scanf("%d%d%d",&x,&y,&k);
63         printf("%d\n",vec[query(1,n,root[x-1],root[y],k)-1]);
64     }
65 }

 

Guess you like

Origin www.cnblogs.com/ChangeG1824/p/11412412.html