POJ 2104 K-th Number
2014-06-19 20:41
309 查看
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 quicklyk-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.题解
我的做法是可持久化线段树。当然还有其他的做法,像归并树+二分之类的。我比较弱~~~~~百度翻译上的翻译还是可以看懂的,题意自寻。
100000个数在1000000000的范围内,首先就是离散。接着就像模板题一样,自己想就好。
#include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> #include<cmath> #include<algorithm> using namespace std; int n,m,size,zz,hash[100002],a[100002],num[100002]; int root[100002],ls[3000002],rs[3000002],sum[3000002]; int getw(int x) { int l=1,r=zz; while(l<=r) {int mid=(l+r)>>1; if(hash[mid]<x) l=mid+1; else r=mid-1; } return l; } void insert(int l,int r,int x,int &w,int v)//2^17=131072 { w=++size; sum[w]=sum[x]+1; if(l==r) return; ls[w]=ls[x]; rs[w]=rs[x]; int mid=(l+r)>>1; if(mid>=v) insert(l,mid,ls[x],ls[w],v); else insert(mid+1,r,rs[x],rs[w],v); } void init() { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) {scanf("%d",&a[i]); num[i]=a[i]; } sort(num+1,num+n+1); hash[++zz]=num[1]; for(int i=1;i<=n;i++) {if(num[i]!=num[i-1]) hash[++zz]=num[i];} for(int i=1;i<=n;i++) insert(1,zz,root[i-1],root[i],getw(a[i])); } int ask(int l,int r,int x,int y,int w) { if(l==r) return l; int mid=(l+r)>>1; if(sum[ls[y]]-sum[ls[x]]>=w) return ask(l,mid,ls[x],ls[y],w); else return ask(mid+1,r,rs[x],rs[y],w-(sum[ls[y]]-sum[ls[x]])); } int main() { init(); for(int i=1;i<=m;i++) {int l,r,x; scanf("%d%d%d",&l,&r,&x); printf("%d\n",hash[ask(1,zz,root[l-1],root[r],x)]); } }
相关文章推荐
- [PKU 2104]K-th Number(树套树版)
- ★【划分树】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(划分线段树)
- poj2104 K-th Number(主席树)
- POJ 2104 K-th Number(划分树)
- 【poj2104】K-th Number 归并树
- 主席树K-th Number
- poj 2104 K-th Number 区间第K大 二分 离散化 + (莫队 树状数组/平方分解/线段树)
- [POJ2104]K-th Number 做题笔记 主席树
- POJ 2104 K-th Number
- poj2104 K-th Number (主席树入门题|模板题)
- POJ 2104 - K-th Number
- POJ 2104 K-th Number 主席树(区间第k大)
- POJ 2104 K-th Number
- POJ 2104: K-th Number
- POJ2104K-th Number(主席树)