poj2104 K-th Number
2016-10-04 20:24
253 查看
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.
可持久化线段树求区间第k大。
利用类似前缀和的思想,把每一位当成修改加入权值线段树中,然后对于询问可以迅速求出区间内的数个数。
注意离散化。
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.
可持久化线段树求区间第k大。
利用类似前缀和的思想,把每一位当成修改加入权值线段树中,然后对于询问可以迅速求出区间内的数个数。
注意离散化。
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int rd() { int x=0,f=1; char c=getchar(); while ((c<'0'||c>'9')&&c!='-') c=getchar(); if (c=='-') { f=-1; c=getchar(); } while (c>='0'&&c<='9') { x=x*10+c-'0'; c=getchar(); } return x*f; } int a[100010],ord[100010],cnt[4000010],ls[4000010],rs[4000010],rt[100010],tot,n,maxn; int ins(int x,int bro,int L,int R) { int i,j,k,p,mid; p=++tot; cnt[p]=cnt[bro]+1; if (L==R) return p; mid=(L+R)/2; if (x<=mid) { rs[p]=rs[bro]; ls[p]=ins(x,ls[bro],L,mid); } else { ls[p]=ls[bro]; rs[p]=ins(x,rs[bro],mid+1,R); } return p; } int qry(int pl,int pr,int L,int R,int k) { int i,j,x,t,z,sum,mid=(L+R)/2; if (L==R) return L; sum=cnt[ls[pr]]-cnt[ls[pl]]; if (k<=sum) return qry(ls[pl],ls[pr],L,mid,k); else return qry(rs[pl],rs[pr],mid+1,R,k-sum); } int main() { int i,j,k,m,p,q,x,y,z; scanf("%d%d",&n,&m); for (i=1;i<=n;i++) scanf("%d",&a[i]),ord[i]=a[i]; sort(ord+1,ord+n+1); maxn=unique(ord+1,ord+n+1)-ord-1; for (i=1;i<=n;i++) a[i]=lower_bound(ord+1,ord+maxn+1,a[i])-ord; for (i=1;i<=n;i++) rt[i]=ins(a[i],rt[i-1],1,maxn); while (m--) { scanf("%d%d%d",&x,&y,&z); printf("%d\n",ord[qry(rt[x-1],rt[y],1,maxn,z)]); } }
相关文章推荐
- POJ2104 k-th number 划分树
- [POJ2104]K-th Number(区间第k值 记录初始状态)
- POJ2104 K-th Number [分块做法]
- 【POJ2104】K-th Number-主席树(可持久化线段树)+离散化
- POJ2104--K-th Number(主席树)
- poj2104 K-th NumberK-th Number【主席树】
- 【序列莫队+二分答案+树状数组】POJ2104-K-th Number
- poj2104 k-th number 主席树入门讲解
- [POJ2104]K-th Number 做题笔记 主席树
- poj2104 K-th Number
- [POJ2104]K-th Number
- kyeremal-poj2104-K-th Number-主席树
- POJ2104-K-th Number-求区间第K大数(暴力or归并树or划分树)
- 185_K-th NUmber 线段树 (POJ2104)
- POJ2104 K-th Number(主席树)
- 【poj2104】K-th Number
- POJ2104-K-th Number
- POJ2104:K-th Number——题解
- POJ2104: K-th Number 题解
- POJ2104-K-th Number