POJ Topic 2140 k-th Number (partition tree)

Source: Internet
Author: User
Tags integer numbers

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

AC Code

#include <stdio.h> #include <string.h> #include <stdlib.h>int tree[30][100100],sorted[100010], Toleft[30][100010];int cmp (const void *a,const void *b) {return * (int *) a-* (int *) b;} void build (int l,int r,int dep) {if (l==r) {return;} int mid= (l+r) >>1;int same=mid-l+1;int i;for (i=l;i<=r;i++) if (Tree[dep][i]<sorted[mid]) Same--;int lpos=l ; int Rpos=mid+1;for (i=l;i<=r;i++) {if (Tree[dep][i]<sorted[mid]) {tree[dep+1][lpos++]=tree[dep][i];} ElseIf (tree[dep][i]==sorted[mid]&&same>0) {tree[dep+1][lpos++]=tree[dep][i];same--;} ELSETREE[DEP+1][RPOS++]=TREE[DEP][I];TOLEFT[DEP][I]=TOLEFT[DEP][L-1]+LPOS-L;//1 L Silly Division unclear}build (l,mid,dep+1);// It's 1 L, and I don't know. Build (mid+1,r,dep+1);} int query (int l,int r,int l,int r,int dep,int k) {if (l==r) {return tree[dep][l];} int mid= (L+R) >>1;int cnt=toleft[dep][r]-toleft[dep][l-1];if (cnt>=k) {int newl=l+toleft[dep][l-1]-toleft[ Dep][l-1];int newr=newl+cnt-1;return Query (l,mid,newl,newr,dep+1,k);} Else{int Newr=r+toleft[dep][r]-toleft[dep][r];iNT newl=newr-(r-l-cnt); return query (MID+1,R,NEWL,NEWR,DEP+1,K-CNT);}} int main () {int n,m;//while (scanf ("%d%d", &n,&m)!=eof)//{int i;scanf ("%d%d", &n,&m); Memset (tree,0, sizeof (tree)), for (i=1;i<=n;i++) {scanf ("%d", &tree[0][i]); sorted[i]=tree[0][i];} Qsort (Sorted+1,n,sizeof (sorted[1]), CMP), build (1,n,0), while (m--) {int a,b,k;scanf ("%d%d%d", &a,&b,&k); int Ans=query (1,n,a,b,0,k);p rintf ("%d\n", ans); //}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ Topic 2140 k-th Number (partition 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.