"POJ 2104" k-th number

Source: Internet
Author: User

Description

You is working for Macrohard company in data Structures Department. After failing your previous task on key insertion you were asked to write a new data structure that would being able to re Turn quickly k-th order statistics in the array segment.
That's, given an array A[1...N] of different integer numbers, your program must answer a series of questions Q (I, J, K) I n the form: "What would is the k-th number in A[I...J] segment, if this segment is sorted?"
For example, consider the array a = (1, 5, 2, 6, 3, 7, 4). Let the question is 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 was 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 <=, 1 <= m <= 5 000).
The second line contains n different integer numbers not exceeding 109 by their absolute values---the array for which th E answers should be given.
The following m lines contain question descriptions, each description consists of three numbers:i, J, and K (1 <= i &L T;= j <= N, 1 <= k <= j-i + 1) and represents the question Q (I, J, K).

Output

For each question output of 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

Main topic:

Find the interval K small.

Analysis:

The line tree can be persisted to create a new version in the order of the numbers in the sequence, where each number is small in the number of all numbers in the segment tree.

Code:

1#include <cstdio>2#include <algorithm>3 4 Const intMax_n =2000000;5 6 structSegmenttree7 {8     intchild[2];9     intCount, number;Ten } Tree[max_n]; One  A intRoot[max_n], treeSize; - intNum[max_n], Id[max_n], back[max_n]; - intN, M, Qi, QJ, qk; the  - #defineMID ((left + right) >> 1) -  - intBuild (intLeftintRight ) + { -     inti = + +treeSize; +Tree[i]. Count =0; A     if(Left <Right ) at     { -Tree[i]. child[0] =Build (left, MID); -Tree[i]. child[1] = Build (MID +1, right); -     } -     returni; - } in  - intModify (intLeftintRightintPosintKeyintpre) to { +     inti = + +treeSize; -     if(Left <Right ) the     { *         intCH = (pos <= MID)?0:1; $Tree[i]. CHILD[!CH] = Tree[pre]. child[!CH];Panax NotoginsengCh? left = MID +1: right =MID; -Tree[i]. CHILD[CH] =Modify (left, right, POS, key, Tree[pre]. Child[ch]); theTree[i]. Count = Tree[tree[i]. child[0]]. Count + tree[tree[i]. child[1]]. Count; +}ElseTree[i]. Number = key, Tree[i]. Count =1; A     returni; the } +  - #defineLeftsize (Tree[tree[late]. Child[0]]. Count-tree[tree[early]. Child[0]]. Count) $  $ intQuery (intLeftintRightintEarlyintLate,intk) - { -     if(left = right)returnTree[late]. number; the     intch = (k <= leftsize)?0:1; -Ch? left = MID +1: right =MID;Wuyi     returnQuery (left, right, tree[early]. Child[ch], tree[late]. CHILD[CH], ch? Kleftsize:k); the } -  Wu BOOLCMP (intAintb) - { About     returnNum[a] <Num[b]; $ } -  - intMain () - { Ascanf ("%d%d", &n, &m); +      for(inti =1; I <= N; i++) thescanf ("%d", &num[i]), id[i] =i; -Std::sort (ID +1, ID + n +1, CMP); $root[0] = Build (1, n); the      for(inti =1; I <= N; i++) theBack[id[i]] =i; the      for(inti =1; I <= N; i++) theRoot[i] = Modify (1, N, Back[i], num[i], root[i-1]); -      for(inti =0; I < m; i++) in     { thescanf (" %d%d%d", &qi, &AMP;QJ, &qk); theprintf ("%d\n", Query (1, N, Root[qi-1], Root[qj], qk)); About     } the}

"POJ 2104" k-th number

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.