POJ 2104 K-th number

第K小数查询算法
本文介绍了一种数据结构问题的解决方法,即快速找出数组某区间内的第K小数。探讨了主席树、划分树、归并树及分块+二分等不同算法,并对比了它们的空间和时间效率。
Time Limit: 20000MS Memory Limit: 65536K
Total Submissions: 56956 Accepted: 19644
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 10 9 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.

Source

Northeastern Europe 2004, Northern Subregion

【中文题意】

给出一个长度为n的序列a1~an,有m次询问(x,y,k),每次询问a[x]~a[y]内的第k数。

输入第一行为n,m,第二行为a1~an,接下来m行是m个(x,y,k)。

由于数据较大,请使用C风格的输入输出。

1<=n<=100000,1<=m<=5000

思路

主席树+权值线段树

代码实现
 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 const int maxn=1e5+10;
 5 int n,k,sz,ts;
 6 int a,b,c;
 7 int s[maxn],hs[maxn],tt[maxn];
 8 struct tree{int s,l,r,mid,lp,rp;}t[maxn<<5];
 9 void build(int l,int r,int k){
10     t[k].l=l,t[k].r=r;
11     if(l==r) return;
12     t[k].mid=l+r>>1,t[k].lp=++ts,t[k].rp=++ts;
13     build(l,t[k].mid,t[k].lp);
14     build(t[k].mid+1,r,t[k].rp);
15 }
16 void put(int l,int r,int k,int nk,int p){
17     t[nk]=(tree){t[k].s+1,t[k].l,t[k].r,t[k].mid};
18     if(l==r) return;
19     if(p<=t[nk].mid){
20         t[nk].lp=++ts,t[nk].rp=t[k].rp;
21         put(l,t[nk].mid,t[k].lp,t[nk].lp,p);
22     }
23     else{
24         t[nk].rp=++ts,t[nk].lp=t[k].lp;
25         put(t[nk].mid+1,r,t[k].rp,t[nk].rp,p);
26     }
27 }
28 int search(int l,int r,int k,int nk,int v){
29     if(l==r) return l;
30     int w=t[t[nk].lp].s-t[t[k].lp].s;
31     if(v<=w) return search(l,t[k].mid,t[k].lp,t[nk].lp,v);
32     else return search(t[k].mid+1,r,t[k].rp,t[nk].rp,v-w);
33 }
34 int main(){
35     freopen("kthnumber.in","r",stdin);
36     freopen("kthnumber.out","w",stdout);
37     scanf("%d%d",&n,&k);
38     for(int i=1;i<=n;i++){
39         scanf("%d",&s[i]);
40         hs[i]=s[i];
41     }
42     sort(hs+1,hs+n+1);
43     sz=unique(hs+1,hs+n+1)-hs-1;
44     build(1,sz,tt[0]);
45     for(int i=1;i<=n;i++){
46         int pos=lower_bound(hs+1,hs+sz+1,s[i])-hs;
47         tt[i]=++ts;
48         put(1,sz,tt[i-1],tt[i],pos);
49         
50     }
51     for(int i=1;i<=k;i++){
52         scanf("%d%d%d",&a,&b,&c);
53         printf("%d\n",hs[search(1,n,tt[a-1],tt[b],c)]);
54     }
55     return 0;
56 }

仍然不太会,这并不是板子(这份代码应该有坑)。//虽然能AT.

2104Accepted47820K1922MSG++1412B2017-06-07 19:59:16

划分树

我听说划分树是把快速排序的流程存储在线段树上;

代码实现
 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 const int maxn=1e5+10;
 5 int n,q,a,b,c;
 6 int s[maxn],str[20][maxn],cnt[20][maxn];
 7 //s记录排序后的,str记录每一层的排序结果 ,cnt[i][j]表示第i层前j个数中有多少个数进入左子树;
 8 int lt[maxn<<2],rt[maxn<<2],md[maxn<<2];
 9 void build(int k,int l,int r,int dp){
10     lt[k]=l,rt[k]=r,md[k]=l+r>>1;
11     if(l==r) return;
12     int mid=md[k],v=s[mid],lmid=mid-l+1,L=l,R=mid+1;
13     for(int i=l;i<=r;i++) if(str[dp][i]<v) lmid--;//lmid表示左子树中还需要多少个中值;
14     for(int i=l;i<=r;i++){
15         if(i==l) cnt[dp][i]=0;
16         else cnt[dp][i]=cnt[dp][i-1];
17         if(str[dp][i]<v||(str[dp][i]==v&&lmid>0)){//构建下一层的左子树; 
18             str[dp+1][L++]=str[dp][i],cnt[dp][i]++;
19             if(str[dp][i]==v) lmid--;
20         }
21         else str[dp+1][R++]=str[dp][i];
22     }
23     build(k<<1,l,mid,dp+1);
24     build(k<<1|1,mid+1,r,dp+1);
25 }
26 int query(int k,int l,int r,int v,int dp){
27     if(l==r) return str[dp][l];
28     int s1,s2;
29     if(lt[k]==l) s1=0;//s1为(lt[k],l-1)中分到左子树的个数;
30     else s1=cnt[dp][l-1];
31     s2=cnt[dp][r]-s1;//s2为(l,r)中分到左子树的个数;
32     if(v<=s2) return query(k<<1,lt[k]+s1,lt[k]+s1+s2-1,v,dp+1);
33     //左子树的数量大于k,递归左子树;
34     int b1=l-lt[k]-s1;//b1为(lt[k],l-1)中分到右子树的个数; 
35     int b2=r-l-s2+1;//b2为(l,r)中分到右子树的个数; 
36     return query(k<<1|1,md[k]+b1+1,md[k]+b1+b2,v-s2,dp+1);
37 }
38 int main(){
39     scanf("%d%d",&n,&q);
40     for(int i=1;i<=n;i++){
41         scanf("%d",&str[1][i]);
42         s[i]=str[1][i];
43     }
44     sort(s+1,s+n+1);
45     build(1,1,n,1);
46     while(q--){
47         scanf("%d%d%d",&a,&b,&c);
48         printf("%d\n",query(1,a,b,c,1));
49     }
50     return 0;
51 }
2104Accepted17572K1313MSG++1396B2017-06-07 21:25:11

归并树

 我听说归并树就是把归并排序的流程存储在树上;

 1 #include<cstdio>
 2 #include<vector>
 3 #include<algorithm>
 4 using namespace std;
 5 const int maxn=3e5+10;
 6 vector<int>t[maxn<<2];
 7 int n,q,a,b,c;
 8 void build(int k,int l,int r){
 9     if(l==r){
10         scanf("%d",&a);
11         t[k].clear();
12         t[k].push_back(a);
13         return;
14     }
15     int mid=l+r>>1,ls=k<<1,rs=ls|1;
16     build(ls,l,mid);
17     build(rs,mid+1,r);
18     t[k].resize(r-l+1);//调整容器t[]的大小为r-l+1; 
19     merge(t[ls].begin(),t[ls].end(),t[rs].begin(),t[rs].end(),t[k].begin());//将两个儿子的数列合并; 
20 }
21 int query(int k,int l,int r,int al,int ar,int v){
22     if(al==l&&ar==r) return upper_bound(t[k].begin(),t[k].end(),v)-t[k].begin();
23     int mid=l+r>>1,ls=k<<1,rs=ls|1;
24     if(ar<=mid) return query(ls,l,mid,al,ar,v);
25     if(al>mid) return query(rs,mid+1,r,al,ar,v);
26     return query(ls,l,mid,al,mid,v)+query(rs,mid+1,r,mid+1,ar,v);
27 }
28 int main(){
29     scanf("%d%d",&n,&q);
30     build(1,1,n);
31     while(q--){
32         scanf("%d%d%d",&a,&b,&c);
33         int l=-1,r=n-1,mid;
34         while(r-l>1){
35             mid=l+r>>1;
36             if(query(1,1,n,a,b,t[1][mid])>=c) r=mid;
37             else l=mid;
38         }
39         printf("%d\n",t[1][r]);
40     }
41     return 0;
42 }
2104Accepted26840K6266MSG++1012B2017-06-08 09:35:11

分块+二分

 1 #include<cmath>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 const int maxm=3e3+10;
 7 const int maxn=3e5+10;
 8 int n,q,a,b,c;
 9 int bks,bs;
10 int s[maxn],ss[maxn];
11 int bk[maxm][maxm],len[maxm],bkl[maxm],bkr[maxm];
12 void build(int n){
13     bs=n/((int)(sqrt(n*log2(n)))+1);
14     if(n%bs) bks=n/bs+1;
15     else bks=n/bs;
16     for(int i=1;i<=n;i++){
17         if(i%bks==1) bkl[i/bks+1]=i;
18         if(i%bks) bk[i/bks+1][++len[i/bks+1]]=s[i];
19         if(i%bks==0) bkr[i/bks]=i,bk[i/bks][++len[i/bks]]=s[i];
20     }
21     bkr[bs]=n;
22     for(int i=1;i<=bs;i++) sort(bk[i]+1,bk[i]+len[i]+1);
23 }
24 int find(int i,int v){
25     int l,r,ans;
26     l=ans=0,r=len[i]+1;
27     while(r-l>0){
28         int mid=l+r>>1;
29         if(bk[i][mid]<=v) ans=mid,l=mid+1;
30         else r=mid;
31     }
32     return ans;
33 }
34 bool check(int l,int r,int k,int v){
35     int ans=0;
36     for(int i=1;i<=bs;i++){
37         if(bkl[i]>=l&&bkr[i]<=r) ans+=find(i,v);
38         else if(bkl[i]<=l&&bkr[i]>=r) for(int j=l;j<=r;j++){if(s[j]<=v) ans++;}
39         else if(bkl[i]<=l&&bkr[i]>=l) for(int j=l;j<=bkr[i];j++){if(s[j]<=v) ans++;}
40         else if(bkl[i]<=r&&bkr[i]>=r) for(int j=bkl[i];j<=r;j++){if(s[j]<=v) ans++;}
41     }
42     return ans<k;
43 }
44 int div(int L,int R,int k){
45     int l,r;
46     l=0,r=n;
47     while(r-l>1){
48         int mid=l+r>>1;
49         if(check(L,R,k,ss[mid])) l=mid;
50         else r=mid;
51     }
52     return ss[r];
53 }
54 int main(){
55     scanf("%d%d",&n,&q);
56     for(int i=1;i<=n;i++){
57         scanf("%d",&s[i]);
58         ss[i]=s[i];
59     }
60     sort(ss+1,ss+n+1);
61     build(n);
62     while(q--){
63         scanf("%d%d%d",&a,&b,&c);
64         printf("%d\n",div(a,b,c));
65     }
66     return 0;
67 }
2104Accepted1868K8985MSG++1571B2017-06-08 08:26:44

个人偏好主席树。//只会写主席树。。。

显然划分树最快而且其空间也是最小的(如果归并树的STL减益不是太大);

主席树次之,但是空间使用量最大;

归并树用了大量的空间换取时间;

分块看起来最暴力,时间上也确实如此;

转载于:https://www.cnblogs.com/J-william/p/6638310.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值