首页 > 代码库 > POJ 2104 K-th Number
POJ 2104 K-th Number
静态区间第K大,主席树写法。
主席树第一题,如果对整段区间建线段树可以求1~n范围内的第K大,要想求任意区间内的第K大需要建1~i 的n棵线段树。求某一段的时候区间相减就可以了。但是这样空间消耗太大所以要尽量利用以前的节点。注意到1~i的线段树和i~i+1的线段树只某一条链上的值不同,所以我们只要建出这一条链就可以了,剩下的节点和1~i+1的线段树是一样的,我们直接把节点指过去。
T[ ... ]记录以1~...的线段树的root节点,保存各种不同版本持久化
c[ ... ]记录下面有多少个节点
K-th Number
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. Source Northeastern Europe 2004, Northern Subregion |
[Submit] [Go Back] [Status] [Discuss]
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn=110000; int n,m,q; int a[maxn],t[maxn]; int T[maxn],lson[maxn*50],rson[maxn*50],c[maxn*50],tot; void init_hash() { tot=0; sort(t+1,t+1+n); m=unique(t+1,t+1+n)-t; } int hash(int x) { return lower_bound(t+1,t+1+n,x)-t; } int build(int l,int r) { int root=tot++,temp=root; c[root]=0; if(l==r) { int mid=(l+r)/2; lson[root]=build(l,mid); rson[root]=build(mid+1,r); } return temp; } int update(int root,int pos,int val) { int newroot=tot++,temp=newroot; c[newroot]=c[root]+val; int l=1,r=m; while(l<r) { int mid=(l+r)/2; if(pos<=mid) { r=mid; lson[newroot]=tot++; rson[newroot]=rson[root]; root=lson[root]; newroot=lson[newroot]; } else { l=mid+1; rson[newroot]=tot++; lson[newroot]=lson[root]; root=rson[root]; newroot=rson[newroot]; } c[newroot]=c[root]+val; } return temp; } int query(int left_son,int right_son,int x) { int l=1,r=m; while(l<r) { int mid=(l+r)/2; int tk=c[lson[left_son]]-c[lson[right_son]]; if(tk>=x) { left_son=lson[left_son]; right_son=lson[right_son]; r=mid; } else { l=mid+1; x-=tk; left_son=rson[left_son]; right_son=rson[right_son]; } } return l; } int main() { while(scanf("%d%d",&n,&q)!=EOF) { for(int i=1;i<=n;i++) { scanf("%d",a+i); t[i]=a[i]; } init_hash(); T[n+1]=build(1,m); for(int i=n;i;i--) { T[i]=update(T[i+1],hash(a[i]),1); } while(q--) { int a,b,c; scanf("%d%d%d",&a,&b,&c); printf("%d\n",t[query(T[a],T[b+1],c)]); } } return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。