2104:K-th Number

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

可持久化线段树,我们可以先把序列离散化,然后线段树上每一位表示第i大的数有几个,那么我们寻找第k大值的时候只需要判断,如果左子树个数大于k就在左子树里找,小于k就在右子树里寻找。
我们发现这样只能解决整个区间的第k大值,然后我们发现这个和前缀和类似,l~r之间的个数就是1~r的个数减去1~(l-1)的个数,那么我们会发现,我们应该建立n棵线段树来加减,可是空间上不允许,
我们发现事实上,n棵线段树中有很多重复的部分,我们可以让线段树公用这些部分,使得空间复杂度降低,这样就是可持久化线段树的思路,运用重复的部分,我们可以发现这样可以推广到对历史版本的保存上,
因为保存历史版本实际上就是保存很多棵线段树。
 1 #include<iostream>
 2 #include<cstdlib>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<algorithm>
 7 #include<string>
 8 #include<map>
 9 #include<queue>
10 #include<vector>
11 #include<set>
12 #define inf 1000000000
13 #define maxn 100000+5
14 #define maxm 8000000+5
15 #define eps 1e-10
16 #define ll long long
17 #define for0(i,n) for(int i=0;i<=(n);i++)
18 #define for1(i,n) for(int i=1;i<=(n);i++)
19 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
20 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
21 #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go)
22 using namespace std;
23 int read(){
24     int x=0,f=1;char ch=getchar();
25     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
26     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
27     return x*f;
28 }
29 int n,m,sz,tot;
30 int root[maxn],ls[maxm],rs[maxm],a[maxn],s[maxm];
31 int num[maxm],hash[maxn];
32 int find(int x){
33     int l=1,r=tot,mid;
34     while(l<=r){
35         int mid=(l+r)>>1;
36         if(hash[mid]<x)l=mid+1;
37         else r=mid-1;
38     }
39     return l;
40 }
41 void insert(int l,int r,int x,int &y,int v){
42     y=++sz;
43     s[y]=s[x]+1;
44     if(l==r)return ;
45     ls[y]=ls[x];rs[y]=rs[x];
46     int mid=(l+r)>>1;
47     if(v<=mid)insert(l,mid,ls[x],ls[y],v);
48     else insert(mid+1,r,rs[x],rs[y],v);
49 }
50 int ask(int l,int r,int x,int y,int k){
51     if(l==r)return l;
52     int mid=(l+r)>>1;
53     if(s[ls[y]]-s[ls[x]]>=k)
54         return ask(l,mid,ls[x],ls[y],k);
55     else return ask(mid+1,r,rs[x],rs[y],k-(s[ls[y]]-s[ls[x]]));
56 }
57 int main(){
58     //freopen("input.txt","r",stdin);
59     //freopen("output.txt","w",stdout);
60     n=read();m=read();
61     for1(i,n)num[i]=a[i]=read();
62     sort(num+1,num+n+1);
63     hash[++tot]=num[1];
64     for(int i=2;i<=n;i++)
65         if(num[i]!=num[i-1])
66             hash[++tot]=num[i];
67     for1(i,n)
68         insert(1,tot,root[i-1],root[i],find(a[i]));
69     for1(i,m){
70         int l=read(),r=read(),x=read();
71         printf("%d\n",hash[ask(1,tot,root[l-1],root[r],x)]);
72     }
73     return 0;
74 }
View Code

 

posted @ 2016-07-04 20:43  HTWX  阅读(134)  评论(0编辑  收藏  举报