POJ 2104 k-th Number (segment tree)

Source: Internet
Author: User

k-th number
Time Limit: 20000MS Memory Limit: 65536K
Total Submissions: 46589 Accepted: 15553
Case Time Limit: 2000MS

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

Hint

This problem have huge input,so please use C-style input (scanf,printf), or your may got time limit exceed.

Test instructions

Given a sequence of a1,a2,..., the query represented by an and m ternary groups. For each query (i,j,k), the output ai,ai+1,..., the number of K in the ascending order of AJ.

Analysis:

Use the segment tree to solve this problem. We maintain the series with line-segment trees. Each node in the segment tree holds the result of the corresponding interval sequence. The line tree nodes we touched before were all values, and this time it was different, and each node saved a sequence.

The process of establishing a segment tree is similar to that of a merge sort, and the sequence of each node is the result of merging the sequence of its two son nodes. The complexity of the achievement is O (logn). By the way, this tree is a complete rendition of the merge sort.

To calculate the number of numbers not exceeding x in an interval, simply do the following recursively.

* Returns 0 if the given interval has no overlap with the current node's interval.

* If the given interval contains exactly the interval corresponding to the current node, then a binary search method is used to find the array saved on that node.

* Otherwise the sum of two sons can be calculated recursively.

Since nodes corresponding to the same depth have access to only a constant number of times, the number of no more than X can be calculated in O (log2n) time. So the complexity of the whole algorithm is O (Nlogn + mlog3n).

#include <iostream> #include <cstring> #include <cstdio> #include <vector> #include <cmath    > #include <algorithm> #define MAXN 100000using namespace std;vector<int> DAT[4*MAXN + 50];        Segment Tree Data int A[MAXN + 50];int N, q;//build segment tree//k is the node number, and interval [L, R) corresponds to void build (int k, int l, int r) {if (r-l = = 1) { Dat[k].push_back (A[l]);    Return } int lc = k << 1, rc = k << 1 |    1;    Build (LC, L, (L + R)/2);    Build (RC, (L + R)/2, R);    Dat[k].resize (R-L); Use the STL's merge function to merge the two-son sequence into merge (Dat[lc].begin (), Dat[lc].end (), Dat[rc].begin (), Dat[rc].end (), Dat[k].begin ());} The number of numbers not exceeding x in the calculation [I, J]//k is the number of the node, and the interval [L, R] corresponds to the int query (int i, int j, int x, int k, int l, int r) {if (J <= L | | r <=    i)//completely disjoint return 0; else if (i <= l&&r <= j) {//fully contained inside return Upper_bound (Dat[k].begin (), Dat[k].end (), x)-dat[    K].begin (); } else {//to the son recursively computes int lcnt = Query (i, J, X, K << 1, L, (L + R)/2);        int rcnt = Query (i, J, X, K << 1 | 1, (l + R)/2, R);    return lcnt + rcnt;    }}int Search (int x, int y, int k) {int l =-1000000000-1;    int r =-L + 2;        while (L < r) {int mid = (L + r) >> 1;        int num = query (x, Y+1, Mid, 1, 1, n+1);        if (k <= num) R = Mid;        else{L = mid + 1; }} return L;} int main () {while (CIN >> n >> q) {for (int i = 1; I <= n; i++) {scanf ("%d", A + I        );        } build (1, 1, n + 1);        int Li, ri, Ki;            for (int i = 0; i < Q; i++) {scanf ("%d%d%d", &li, &ri, &ki);        printf ("%d\n", Search (Li, ri, ki)); }} return 0;}


POJ 2104 k-th Number (segment tree)

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.