POJ 2104 K-th Number(分块+二分)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 2104 K-th Number(分块+二分)相关的知识,希望对你有一定的参考价值。

题目链接:http://poj.org/problem?id=2104

题目:

Description

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

Output

For each question output 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 has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.
题意:给出n个数,m次测试。每次测试求出[l,r]区间内的第k大的数。
题解:整体二分求解
 1 #include <cstdio>
 2 #include <vector>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 const int B=1000;
 7 const int N=100000+10;
 8 int num[N],I[N],J[N],K[N],sorted_num[N];
 9 vector <int> block[N/B];
10 
11 int n,m;
12 
13 void solve(){
14     for(int i=0;i<n;i++){
15         block[i/B].push_back(num[i]);
16         sorted_num[i]=num[i];
17     }
18     sort(sorted_num,sorted_num+n);
19     for(int i=0;i<n/B;i++) sort(block[i].begin(),block[i].end());
20     for(int i=0;i<m;i++){
21         int l=I[i]-1,r=J[i],k=K[i];
22         int lb=-1,rb=n-1;
23         while(rb-lb>1){
24             int mid=(rb+lb)/2;
25             int x=sorted_num[mid];
26             int tl=l,tr=r,c=0;
27             while(tl<tr&&tl%B!=0) if(num[tl++]<=x) c++;
28             while(tl<tr&&tr%B!=0) if(num[--tr]<=x) c++;
29             while(tl<tr){
30                 c+=upper_bound(block[tl/B].begin(),block[tl/B].end(),x)-block[tl/B].begin();
31                 tl+=B;
32             }
33             if(c>=k) rb=mid;
34             else lb=mid;
35         }
36         printf("%d\n",sorted_num[rb]);
37     }
38 }
39 
40 int main(){
41     scanf("%d %d",&n,&m);
42     for(int i=0;i<n;i++) scanf("%d",&num[i]);
43     for(int i=0;i<m;i++) scanf("%d %d %d",&I[i],&J[i],&K[i]);
44     solve();
45     return 0;
46 }

 

 

以上是关于POJ 2104 K-th Number(分块+二分)的主要内容,如果未能解决你的问题,请参考以下文章

POJ 2104 K-th Number ( 求取区间 K 大值 )

[POJ2104] K-th Number(归并树)

POJ 2104 K-th Number

整体二分初识--POJ2104:K-th Number

POJ2104 K-th Number(主席树)

poj[2104]K-th Number