"POJ2104" kth num

Source: Internet
Author: User
Tags integer numbers

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 3
1 5 2 6 3 7 4
2 5 3
4 4 1
1 7 3
Sample Output

5
6
3
Hint

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

Main topic

Find the value of k small in interval [l,r]

Chairman Tree naked, I can only do a bare question now

1#include <cstdio>2#include <algorithm>3#include <cstdlib>4 using namespacestd;5 Const intn= +;6 intA[n],b[n],ls[n],rs[n],sum[n],root[n];7 intt,sz,n,m;8 voidBuildintLintRintXint&y,intv) {9y=++sz;TenLS[Y]=LS[X]; rs[y]=Rs[x]; Onesum[y]=sum[x]+1; A     if(L==R)return; -     intMid= (l+r) >>1; -     if(V>mid) Build (mid+1, r,rs[x],rs[y],v); the     Elsebuild (l,mid,ls[x],ls[y],v); - } -  - intQueryintLintRintW) { +     intL=1, r=t,mid= (l+r) >>1; -     intx=root[l-1],y=Root[r]; +      while(l!=R) { A         if(SUM[LS[Y]]-SUM[LS[X]]&GT;=W) {r=mid;x=ls[x];y=ls[y];mid= (l+r) >>1;} at         Else{w-=sum[ls[y]];w+=sum[ls[x]];l=mid+1; x=rs[x];y=rs[y];mid= (L+r) >>1;} -     } -     returnl; - } -  - intMain () { inscanf"%d%d",&n,&m); -      for(intI=1; i<=n;i++) {scanf ("%d", &a[i]); b[i]=a[i];} toSort (b +1, b+n+1); +T=unique (b +1, b+n+1)-b-1; -      for(intI=1; i<=n;i++){ the         intW=lower_bound (b +1, b+t+1, A[i])-b; *Build1, t,root[i-1],root[i],w); $     }Panax Notoginseng     intl,r,w; -      for(intI=1; i<=m;i++){ thescanf"%d%d%d",&l,&r,&W); +printf"%d\n", B[query (l,r,w)]); A     } the}

"POJ2104" kth num

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.