Time Limit: 20000MS |
|
Memory Limit: 65536K |
Total Submissions: 45283 |
|
Accepted: 15067 |
Case Time Limit: 2000MS |
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.
题解:
静态区间查第k小,用可持久化线段树,也叫主席树。
主席树:
对于序列的每一个前缀a[1]~a[i]建一棵以序列里的值为下标的线段树(离散化),记录该前缀序列里出现的值的次数;记离散后的标记为1~N; (下面值直接用1~N代替;)
因为N棵线段树形态相同,假设有线段树A和线段树B(B>A),那么线段树B区间为[x,y]里的数字的个数减去线段树A区间为[x,y]里数字的个数,得到的就是原数列A~B之间数字在[x,y]区间里数字的个数。
对于区间[x,y]的第k大的值,让root[]记录每棵线段树的根节点的编号,则 t=root[B].[1,mid]-root[A-1].[1,mid] ,t表示区间[A,B]内值在[1,mid]的个数,先判断t是否大于K,如果t大于k,那么说明在区间[x,y]内存在[1,mid]的数的个数大于k,也就是第k大的值在[1,mid]中,否则在[mid+1,r]中;这样我们依次同时从root[x-1],root[y]往下走当l==r时 第k大的值就是离散后标记为l的值。
如果每棵线段都建完整的化,n*(n<<2)肯定会MLE,我们发现对于前缀[1,i]和前缀[1,i+1]的线段树,如果b[i+1]<=mid (b[i+1]表示a[i+1]离散后的标记)那么线段树i和线段树i+1的左边是完全相同的,根本不需要再建,只需要用指针指一下就好,如下图:
那么对于一棵新的线段树其实我们最多要建的节点数为log(n);nlog(n)的节点数还是可以忍受的。
1 #include<iostream>
2 #include<cstdio>
3 #include<cstdlib>
4 #include<cstring>
5 #include<algorithm>
6 #include<cmath>
7 using namespace std;
8 const int maxn=1e5+10;
9 int N,M;
10 struct Tree{
11 int lc,rc,w;
12 }T[maxn*20];
13 int a[maxn],pos[maxn],b[maxn],root[maxn],siz;
14 int cmp(int i,int j){//用a[]的权值对pos[]进行排序
15 return a[i]<a[j];
16 }
17 inline void insert(int &i,int l,int r,int x){
18 T[++siz]=T[i]; i=siz;//第i棵线段树与第i-1棵线段树之间建立联系,节约空间
19 T[i].w++;
20 if(l==r) return ;
21 int mid=(l+r)>>1;
22 if(x<=mid) insert(T[i].lc,l,mid,x);
23 else insert(T[i].rc,mid+1,r,x);
24 }
25 int query(int i,int j,int l,int r,int k){
26 if(l==r) return l;
27 int t=T[T[j].lc].w-T[T[i].lc].w;
28 int mid=(l+r)>>1;
29 if(t>=k) return query(T[i].lc,T[j].lc,l,mid,k);
30 else return query(T[i].rc,T[j].rc,mid+1,r,k-t);
31 }
32 int main(){
33 root[0]=0;
34 scanf("%d%d",&N,&M);
35 for(int i=1;i<=N;i++){
36 scanf("%d",&a[i]);
37 pos[i]=i;
38 }
39 sort(pos+1,pos+N+1,cmp);//pos根据 a数组的大小排序 pos[i]=j 表示第 i小的数在a中的第 j个位置
40 for(int i=1;i<=N;i++) b[pos[i]]=i;
41
42 siz=0;
43 for(int i=1;i<=N;i++){
44 root[i]=root[i-1];
45 insert(root[i],1,N,b[i]);
46 }
47 for(int i=1,x,y,k;i<=M;i++){
48 scanf("%d%d%d",&x,&y,&k);
49 int t=query(root[x-1],root[y],1,N,k);
50 printf("%d\n",a[pos[t]]);
51 }
52 return 0;
53 }