POJ 2104 K-th Number (Chair tree), pojk-th

Source: Internet
Author: User
Tags integer numbers

POJ 2104 K-th Number (Chair tree), pojk-th
K-th Number

Time Limit:20000 MS   Memory Limit:65536 K
Total Submissions:57427   Accepted:19856
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.

Source

Northeastern Europe 2004, template Question of the Northern Subregion chair tree.
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # include <queue> 6 # include <algorithm> 7 using namespace std; 8 const int MAXN = 10000001; 9 void read (int & n) 10 {11 char c = '+'; int x = 0; bool flag = 0; 12 while (c <'0' | c> '9') 13 {c = getchar (); if (c = '-') flag = 1 ;} 14 while (c >='0' & c <= '9') 15 {x = x * 10 + c-48, c = getchar ();} 16 flag = 1? N =-x: n = x; 17} 18 int n, m; 19 int a [MAXN]; 20 int hash [MAXN]; 21 int root [MAXN/2]; 22 int now, tot; // total number of occurrences of all nodes 23 int ls [MAXN], rs [MAXN], cnt [MAXN]; 24 void build (int & cur, int l, int r) 25 {26 cur = tot ++; // total number of nodes 27 cnt [cur] = 0; 28 if (l! = R) 29 {30 int mid = (l + r)/2; 31 build (ls [cur], l, mid); 32 build (rs [cur], mid + 1, r); 33} 34} 35 void update (int pre, int pos, int & cur, int l, int r) 36 {37 cur = tot ++; 38 cnt [cur] = cnt [pre] + 1; 39 ls [cur] = ls [pre]; rs [cur] = rs [pre]; 40 if (l = r) 41 return; 42 int mid = (l + r)/2; 43 if (pos <= mid) 44 update (ls [pre], pos, ls [cur], l, mid); 45 else 46 update (rs [pre], pos, rs [cur], mid + 1, r ); 47} 48 int query (int lt, int rt, int l, int r, int k) 49 {50 if (l = r) 51 return l; 52 int now = cnt [ls [rt]-cnt [ls [lt]; 53 int mid = (l + r)/2; 54 if (k <= now) 55 return query (ls [lt], ls [rt], l, mid, k); 56 else 57 return query (rs [lt], rs [rt], mid + 1, r, k-now); 58} 59 int main () 60 {61 while (scanf ("% d", & n, & m) = 2) 62 {63 for (int I = 1; I <= n; I ++) 64 {65 read (a [I]); 66 hash [I] = a [I]; 67} 68 sort (hash + 1, hash + n + 1); 69 int size = unique (hash + 1, hash + n + 1)-hash-1; 70 for (int I = 1; I <= n; I ++) 71 a [I] = lower_bound (hash + 1, hash + size + 1, a [I])-hash; // sort + discretization 72 73 tot = 0; 74 build (root [0], 1, size ); 75 76 for (int I = 1; I <= n; I ++) 77 update (root [I-1], a [I], root [I], 1, size); // create all trees 78 79 for (int I = 1; I <= m; I ++) 80 {81 int x, y, z; 82 read (x); read (y); read (z); 83 printf ("% d \ n", hash [query (root [x-1], root [y], 1, size, z)]); 84} // query 85} 86 87 return 0; 88}

 

 

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.