Poj2104 (Chairman tree)

Source: Internet
Author: User

K-th Number

Time Limit:20000 MS Memory Limit:65536 K
Total Submissions:35704 Accepted:11396
Case Time Limit:2000 MS

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 wocould 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 wocould 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 shocould 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 31 5 2 6 3 7 42 5 34 4 11 7 3

Sample Output

563

Hint

This problem has huge input, so please use c-style input (scanf, printf), or you may got time limit exceed. question: This question requires the k-small element of the given range. You can use the Division tree. In my blog, the Division tree solution is available in the poj2104 division tree solution, for the decision tree, you can refer to the decision tree. The relevant knowledge about the decision tree can be found in the decision tree Reading Notes. Each node in the chairman tree corresponds to a line segment tree. The meaning of each node is a suffix of the original discrete sequence. If an element appears in each line segment tree, it is marked as 1. The problem can be converted to the number of the first k in the interval. The number of the k is the element smaller than the k, operations on intervals are characteristic of a line segment tree. Post Code:
# Include
 
  
# Include
  
   
Using namespace std; const int MAXN = 100000 + 100; const int MAXM = MAXN * 20; int tot, n, m; int da [MAXN], sDa [MAXN]; int leftChild [MAXM], rightChild [MAXM], wei [MAXM], chairTNode [MAXM]; /*********************************** parameters: element interval to be processed * function: Create an empty line segment tree * return value: returns the root node subscript ***********************************/ int Build (int left, int right) {int id = tot ++; wei [id] = 0; if (left
   
    
> 1; leftChild [id] = Build (left, mid); rightChild [id] = Build (mid + 1, right);} return id;} int Update (int root, int pos, int val) {int l = 1, r = m, mid, newRoot = tot ++, 1_ot = newRoot; wei [newRoot] = wei [root] + val; while (l
    
     
> 1; if (pos <= mid) {// determine the node's child node leftChild [newRoot] = tot ++; rightChild [newRoot] = rightChild [root]; // confirm the new node to be followed and the previous version newRoot = leftChild [newRoot]; root = leftChild [root]; r = mid;} else {rightChild [newRoot] = tot ++; leftChild [newRoot] = leftChild [root]; newRoot = rightChild [newRoot]; root = rightChild [root]; l = mid + 1 ;} wei [newRoot] = wei [root] + val;} return invalid ot;} int Query (int leftRoot, int rightRoot, int k) {int l = 1, r = m, mid; while (l
     
      
> 1; if (wei [leftChild [leftRoot]-wei [leftChild [rightRoot]> = k) // The k-th small value is in the left subtree {// confirm to find leftRoot = leftChild [leftRoot]; rightRoot = leftChild [rightRoot]; r = mid ;} else {k-= wei [leftChild [leftRoot]-wei [leftChild [rightRoot]; leftRoot = rightChild [leftRoot]; rightRoot = rightChild [rightRoot]; l = mid + 1 ;}} return l ;}int main () {int q, I; int ql, qr, k; while (scanf ("% d ", & n, & q )! = EOF) {m = 0; tot = 0; for (I = 1; I <= n; I ++) {scanf ("% d ", & da [I]); sDa [I] = da [I];} sort (sDa + 1, sDa + n + 1); m = unique (sDa + 1, sDa + 1 + n)-sDa-1; chairTNode [n + 1] = Build (1, m ); // cout <"************" <
      
        = 1; I --) {int pos = lower_bound (sDa + 1, sDa + 1 + m, da [I])-sDa; chairTNode [I] = Update (chairTNode [I + 1], pos, 1);} while (q --) {scanf ("% d", & ql, & qr, & k); printf ("% d \ n", sDa [Query (chairTNode [ql], chairTNode [qr + 1], k)]);} system ("pause"); return 0 ;}
      
     
    
   
  
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.