POJ2104 K-th Number(主席树)
2016-10-07 15:53
381 查看
题目
Source
http://poj.org/problem?id=2104Description
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 31 5 2 6 3 7 4
2 5 3
4 4 1
1 7 3
Sample Output
56
3
分析
题目是给一个序列,多次询问序列区间中第k小的元素。经典主席树入门题。。相当于序列各个前缀建一棵权值线段树,区间上的信息就是两个前缀和的差分了。。复习复习,已经忘了。
代码
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define MAXN 111111 int root[MAXN],tree[MAXN*20],lch[MAXN*20],rch[MAXN*20],x,y,N; void update(int i,int j,int a,int &b){ b=++N; if(i==j){ tree[b]=tree[a]+1; return; } lch[b]=lch[a]; rch[b]=rch[a]; int mid=i+j>>1; if(x<=mid) update(i,mid,lch[a],lch[b]); else update(mid+1,j,rch[a],rch[b]); tree[b]=tree[lch[b]]+tree[rch[b]]; } int query(int i,int j,int l,int r,int k){ if(i==j) return i; int mid=i+j>>1; int lcnt=tree[lch[r]]-tree[lch[l]]; if(k<=lcnt) return query(i,mid,lch[l],lch[r],k); return query(mid+1,j,rch[l],rch[r],k-lcnt); } int a[MAXN],b[MAXN]; int main(){ int n,m; scanf("%d%d",&n,&m); for(int i=1; i<=n; ++i){ scanf("%d",&a[i]); } memcpy(b,a,sizeof(b)); sort(b+1,b+1+n); int bn=unique(b+1,b+1+n)-b-1; for(int i=1; i<=n; ++i){ x=lower_bound(b+1,b+1+n,a[i])-b; update(1,bn,root[i-1],root[i]); } int l,r,k; while(m--){ scanf("%d%d%d",&l,&r,&k); printf("%d\n",b[query(1,bn,root[l-1],root[r],k)]); } return 0; }
相关文章推荐
- POJ 2104 K-th Number (主席树)
- POJ 2104 K-th Number(主席树)
- POJ 2104 K-th Number (划分树,主席树写过了,这次是整体二分解法 )
- POJ 2104 K-th Number 经典主席树
- Poj2104-K-th Number(主席树)
- 【POJ】2104 K-th Number 主席树
- 【poj 2104】K-th Number 主席树入门
- POJ_2104_K-th Number_主席树
- 【POJ】2104 K-th Number(区间k大+主席树)
- poj2104 k-th number 主席树入门讲解
- 【POJ】2104 K-th Number 静态第K小——主席树
- poj 2104 K-th Number (静态区间第k大,主席树)
- poj2104 K-th number 主席树模版
- poj 2104 K-th Number(主席树,详细有用)
- [POJ 2104]K-th Number【模板】(主席树)
- POJ-2104:K-th Number(主席树)
- poj 2104 K-th Number (主席树学习第一弹)
- poj-2104 K-th Number[主席树/函数式线段树/可持久化线段树]
- POJ 2104 K-th Number 主席树(求区间第k大)
- 【POJ 2104/HDU 2665】K-th Number【整体二分/主席树】