POJ-2104 K-th Number(主席树)
Posted 可惜没如果=_=
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ-2104 K-th Number(主席树)相关的知识,希望对你有一定的参考价值。
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.
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).
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.
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 }
以上是关于POJ-2104 K-th Number(主席树)的主要内容,如果未能解决你的问题,请参考以下文章