Poj2104k-th Number (Chairman tree)

Source: Internet
Author: User
Tags integer numbers

k-th number
Time Limit: 20000MS Memory Limit: 65536K
Total Submissions: 46886 Accepted: 15682
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.

Source

Northeastern Europe 2004, Northern subregion

[Submit]   [Go back]   [Status] [Discuss]

Home Page Go back to top


Baidu search can be persisted in the line tree there is this problem

Because it was very early to know that the chairman of the tree unfortunately has not read the text inadvertently in the Bilibili Bomb screen Web site to see the speech algorithm video incredible haha

I went to the B station to learn the algorithm ... Video Address

http://www.bilibili.com/video/av4619406 This is the explanation of the words http://seter.is-programmer.com/posts/31907.html

Read the video according to the impression ... The AC is slowly digesting ~
#include <stdio.h> #include <vector> #include <algorithm>using namespace std;const int maxn=1e5+5; struct node{int l,r;int sum;} T[maxn*40];int root[maxn],a[maxn],cnt,sum;vector<int>v;int getid (int x) {return lower_bound (V.begin (), V.end () , x)-v.begin () +1;} void update (int l,int r,int &x,int y,int pos) {t[++cnt]=t[y],t[cnt].sum++,x=cnt;if (l==r) return; int mid= (L+R)/2;if ( Mid>=pos) update (L,MID,T[X].L,T[Y].L,POS); else update (MID+1,R,T[X].R,T[Y].R,POS);} int query (int l,int r,int x,int y,int k) {if (l==r) return l;int mid= (l+r)/2;int sum=t[t[y].l].sum-t[t[x].l].sum;if (sum &GT;=K) return query (L,MID,T[X].L,T[Y].L,K), Else return query (mid+1,r,t[x].r,t[y].r,k-sum);} int main () {int n,m;scanf ("%d%d", &n,&m), for (int i=1;i<=n;i++) scanf ("%d", &a[i]), V.push_back (A[i]); Sort (V.begin (), V.end ()), V.erase (Unique (V.begin (), V.end ()), V.end ()), for (int i=1;i<=n;i++) update (1,n,root[i], Root[i-1],getid (A[i])); for (int i=0;i<m;i++) {int a,b,k;scanf ("%d%d", &a,&b,&k);p rintf ("%d\n", V[query (1,n,root[a-1],root[b],k)-1]);} return 0;}




Poj2104k-th Number (Chairman 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.