POJ-2104 K-th Number(主席树)

本文详细解析了K-thNumber问题的解决方法,该问题要求在给定数组的指定区间内找到排序后的第k个数。文章通过使用离散化、二分查找等技术实现了高效的查询算法,并提供了一个完整的C++实现示例。
K-th Number
Time Limit: 20000MS Memory Limit: 65536K
Total Submissions: 60557 Accepted: 21228
Case Time Limit: 2000MS

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 10 9 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.

Source

Northeastern Europe 2004, Northern Subregion
 
终于攻克主席树第一个难关:) 这题额外的学会了stl的二分,stl的去重还有stl的离散化:> 反正收获多多啦
最后查询的时候应该是work(y)-work(x-1)才行,我一开始是work(y)-work(x)然后只在work里面的一个地方用了x-1所以错了,很尴尬
 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstdlib>
 4 #include <queue>
 5 #include <iostream>
 6 #include "algorithm"
 7 using namespace std;
 8 typedef long long LL;
 9 const int MAX=1e5+5;
10 int n,m;
11 int a[MAX],b[MAX],lb,root[MAX],cnt;
12 struct Node{
13     int l,r;
14     int sum;
15     Node (){l=r=sum=0;}
16 }t[MAX*40];
17 inline int read(){
18     int an=0,x=1;char c=getchar();
19     while (c<'0' || c>'9') {if (c=='-') x=-1;c=getchar();}
20     while (c>='0' && c<='9') {an=an*10+c-'0';c=getchar();}
21     return an*x;
22 }
23 void update(int l,int r,int &x,int y,int pos){
24     t[++cnt]=t[y],t[cnt].sum++;x=cnt;
25     if (l==r) return;
26     int mid=(l+r)>>1;
27     if (pos<=mid) update(l,mid,t[x].l,t[y].l,pos);
28     else update(mid+1,r,t[x].r,t[y].r,pos);
29 }
30 int query(int l,int r,int x,int y,int k){
31     if (l==r) return l;
32     int sum=t[t[y].l].sum-t[t[x].l].sum;
33     int mid=(l+r)>>1;
34     if (sum>=k) return query(l,mid,t[x].l,t[y].l,k);
35     else return query(mid+1,r,t[x].r,t[y].r,k-sum);
36 }
37 int main(){
38     freopen ("chairman.in","r",stdin);
39     freopen ("chairman.out","w",stdout);
40     int i,j;
41     int x,y,z;
42     n=read();m=read();
43     for (i=1;i<=n;i++){a[i]=read();b[i]=a[i];}sort(b+1,b+n+1);
44     lb=unique(b+1,b+n+1)-(b+1);
45     for (i=1;i<=n;i++){
46         int pos=lower_bound(b+1,b+lb+1,a[i])-b;
47         update(1,lb,root[i],root[i-1],pos);
48     }
49     for (i=1;i<=m;i++){
50         x=read();y=read();z=read();
51         printf("%d\n",b[query(1,lb,root[x-1],root[y],z)]);
52     }
53     return 0;
54 }

 

转载于:https://www.cnblogs.com/keximeiruguo/p/7599755.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值