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
]
a[1...n]
a[1...n] of different integer numbers, your program must answer a series of questions
Q
(
i
,
j
,
k
)
Q(i, j, k)
Q(i,j,k) in the form: “What would be the k-th number in
a
[
i
.
.
.
j
]
a[i...j]
a[i...j] segment, if this segment was sorted?”
For example, consider the array
a
=
(
1
,
5
,
2
,
6
,
3
,
7
,
4
)
a = (1, 5, 2, 6, 3, 7, 4)
a=(1,5,2,6,3,7,4). Let the question be
Q
(
2
,
5
,
3
)
Q(2, 5, 3)
Q(2,5,3). The segment
a
[
2...5
]
a[2...5]
a[2...5] is
(
5
,
2
,
6
,
3
)
(5, 2, 6, 3)
(5,2,6,3). If we sort this segment, we get
(
2
,
3
,
5
,
6
)
(2, 3, 5, 6)
(2,3,5,6), the third number is
5
5
5, and therefore the answer to the question is
5
5
5.
Input
The first line of the input file contains
n
n
n — the size of the array, and
m
m
m — the number of questions to answer (
1
<
=
n
<
=
100000
,
1
<
=
m
<
=
5000
1 <= n <= 100 000, 1 <= m <= 5 000
1<=n<=100000,1<=m<=5000).
The second line contains
n
n
n different integer numbers not exceeding
1
0
9
10^9
109 by their absolute values — the array for which the answers should be given.
The following
m
m
m lines contain question descriptions, each description consists of three numbers:
i
,
j
i, j
i,j, and
k
k
k (
1
<
=
i
<
=
j
<
=
n
,
1
<
=
k
<
=
j
−
i
+
1
1 <= i <= j <= n, 1 <= k <= j - i + 1
1<=i<=j<=n,1<=k<=j−i+1) and represents the question
Q
(
i
,
j
,
k
)
Q(i, j, k)
Q(i,j,k).
Output
For each question output the answer to it — the k-th number in sorted
a
[
i
.
.
.
j
]
a[i...j]
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.
思路
翻译一下题意:
给出一个长度为
n
n
n 的序列,有
m
m
m 次询问,每次询问查询区间
[
l
,
r
]
[l,r]
[l,r] 内的第
k
k
k 小数。
1
≤
n
≤
1
0
5
,
a
[
i
]
≤
1
0
9
,
m
≤
5000
1\le n \le10^5 ,a[i] \le10^9,m \le 5000
1≤n≤105,a[i]≤109,m≤5000
首先对序列离散化。
我们对序列 a 1 a_1 a1 ~ a i a_i ai 建立一棵权值线段树,也即是说,离散化后的每个数字 j j j,若是出现过一次,就在对应的桶中 c n t + + cnt++ cnt++,线段树维护着这些桶。
那么,要找到 a 1 a_1 a1 ~ a i a_i ai 中的第 k k k 小数,只需要使用二分的方法,查询 a 1 a_1 a1 ~ a 1 + i 2 a_{\frac{1+i}2} a21+i 中的数字共出现多少次,再递归对 a 1 a_1 a1 ~ a 1 + i 2 a_{\frac{1+i}2} a21+i 或 a 1 + i 2 + 1 a_{\frac{1+i}2+1} a21+i+1 ~ a i a_i ai 进行搜索,即可找到答案。
那又如何查询区间 a i a_i ai ~ a j a_j aj 中的第 k 小数呢?用一个类似前缀和的方法,区间 a i a_i ai ~ a i + j 2 a_\frac{i+j}2 a2i+j 中数字出现的个数,可以通过区间 a 1 a_1 a1 ~ a i + j 2 a_\frac{i+j}2 a2i+j 减去区间 a 1 a_1 a1 ~ a i − 1 a_{i-1} ai−1来计算。这样,就可以计算任意区间的第 k k k 小数了。
想法很不错,但是这需要对序列的任意前缀建立一棵对应的权值线段树,这样的时空复杂度可不是一般家庭能够承受的。
可持久化线段树就是这样的数据结构,一棵主席树里包含着很多棵线段树。
假设我们已经建立好了一棵线段树,维护着 a 1 a_1 a1 ~ a i a_{i} ai 这个区间。那么,如果要建另一棵线段树,维护 a 1 a_1 a1 ~ a i + 1 a_{i+1} ai+1 这个区间,其实两棵线段树之间不同的结点只有修改过的那一条链。如果能把相同的结点共用,那么就会大大降低复杂度。
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=1e5+10,M=25e5;
int ls[M],rs[M],sum[M],r[M],tot;
int a[N],b[N],c[N],n,m,v,cnt;
void build(int &p,int l,int r){
p=++tot;
if(l==r) return;
int mid=(l+r)>>1;
build(ls[p],l,mid);
build(rs[p],mid+1,r);
}
int change(int p,int l,int r){
int q=++tot;
ls[q]=ls[p],rs[q]=rs[p];
sum[q]=sum[p]+1;
if(l==r) return q;
int mid=(l+r)>>1;
if(v<=mid) ls[q]=change(ls[q],l,mid);
if(v>=mid+1) rs[q]=change(rs[q],mid+1,r);
return q;
}
int query(int i,int j,int k,int l,int r){
if(l==r) return l;
int mid=(l+r)>>1,x=sum[ls[j]]-sum[ls[i]];
if(x<k) return query(rs[i],rs[j],k-x,mid+1,r);
else return query(ls[i],ls[j],k,l,mid);
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",&a[i]),b[i]=a[i];
sort(b+1,b+n+1);
cnt=unique(b+1,b+n+1)-b-1;
for(int i=1;i<=n;i++)
c[i]=lower_bound(b+1,b+cnt+1,a[i])-b;
build(r[0],1,cnt);
for(int i=1;i<=n;i++)
v=c[i],r[i]=change(r[i-1],1,cnt);
while(m--){
int L,R,k;
scanf("%d%d%d",&L,&R,&k);
printf("%d\n",b[query(r[L-1],r[R],k,1,cnt)]);
}
}