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 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个数的数组。然后叫你求i,j。之间的第k大数。
思路:
划分树裸题。又重新写了次。加深了印象。也发现了一些需注意的地方。
详细见代码:
#include<algorithm> #include<iostream> #include<string.h> #include<sstream> #include<stdio.h> #include<math.h> #include<vector> #include<string> #include<queue> #include<set> #include<map> using namespace std; const int INF=0x3f3f3f3f; const int maxn=100010; int seg[20][maxn],lnum[20][maxn],sa[maxn]; int n,m; void btree(int L,int R,int d) { int i,ls,rs,lm,mid; if(L==R) return ; mid=(L+R)>>1; ls=L,rs=mid+1; lm=mid-L+1; for(i=L;i<=R;i++) if(seg[d][i]<sa[mid]) lm--; for(i=L;i<=R;i++) { lnum[d][i]=(i==L)?0:lnum[d][i-1]; if(seg[d][i]==sa[mid]) { if(lm>0) { lm--; lnum[d][i]++; seg[d+1][ls++]=seg[d][i]; } else seg[d+1][rs++]=seg[d][i]; } else if(seg[d][i]<sa[mid]) { lnum[d][i]++; seg[d+1][ls++]=seg[d][i]; } else seg[d+1][rs++]=seg[d][i]; } btree(L,mid,d+1); btree(mid+1,R,d+1); } int qu(int L,int R,int l,int r,int d,int k) { int ss,s,bb,b,mid; if(L==R) return seg[d][L]; ss=(l==L)?0:lnum[d][l-1];//[1,l-1]进入左树的个数 s=lnum[d][r]-ss;//[l,r]进入左树的个数 mid=(L+R)>>1; if(s>=k)//在左树中 return qu(L,mid,L+ss,L+ss+s-1,d+1,k);//注意边界。可以确定L+ss-1不为所求所以从L+ss else { bb=l-L-ss;//[1,l-1]进入右树的个数.(l-1)-L+1-ss b=r-l+1-s;//[l,r]进入右树的个数 return qu(mid+1,R,mid+bb+1,mid+bb+b,d+1,k-s); } } void init() { int i; for(i=1;i<=n;i++) { scanf("%d",&seg[0][i]); sa[i]=seg[0][i]; } sort(sa+1,sa+n+1);//注意排序起点啊。。 btree(1,n,0);//mid+1+bb+b-1 } int main() { int i,j,k; while(~scanf("%d%d",&n,&m)) { init(); while(m--) { scanf("%d%d%d",&i,&j,&k); printf("%d\n",qu(1,n,i,j,0,k)); } } return 0; }
主席树是个神奇的东东。
#include<algorithm> #include<iostream> #include<string.h> #include<sstream> #include<stdio.h> #include<math.h> #include<vector> #include<string> #include<queue> #include<set> #include<map> using namespace std; const int INF=0x3f3f3f3f; const int maxn=100010; const int maxm=20*maxn; int ls[maxm],rs[maxm],c[maxm],T[maxm]; int arr[maxn],H[maxn]; int n,m,tot; void init() { sort(H,H+m); m=unique(H,H+m)-H; } int Hash(int x) { return lower_bound(H,H+m,x)-H; } int build(int L,int R) { int rt=tot++,mid=(L+R)>>1; c[rt]=0; if(L!=R) { ls[rt]=build(L,mid); rs[rt]=build(mid+1,R); } return rt; } int Insert(int prt,int x,int val) { int nrt=tot++,l=0,r=m-1,mid,tp=nrt; c[nrt]=c[prt]+val; while(l<r) { mid=(l+r)>>1; if(x<=mid) { ls[nrt]=tot++,rs[nrt]=rs[prt]; prt=ls[prt],nrt=ls[nrt]; r=mid; } else { ls[nrt]=ls[prt],rs[nrt]=tot++; prt=rs[prt],nrt=rs[nrt]; l=mid+1; } c[nrt]=c[prt]+val; } return tp; } int qu(int L,int R,int k) { int lrt=T[L-1],rrt=T[R],l=0,r=m-1,mid,tp; while(l<r) { mid=(l+r)>>1; tp=c[ls[rrt]]-c[ls[lrt]]; if(k<=tp) { r=mid; lrt=ls[lrt],rrt=ls[rrt]; } else { l=mid+1; k-=tp; lrt=rs[lrt],rrt=rs[rrt]; } } return l; } int main() { int i,j,k,q; while(~scanf("%d%d",&n,&q)) { tot=m=0; for(i=1;i<=n;i++) scanf("%d",&arr[i]),H[m++]=arr[i]; init(); T[0]=build(1,m); for(i=1;i<=n;i++) T[i]=Insert(T[i-1],Hash(arr[i]),1); while(q--) { scanf("%d%d%d",&i,&j,&k); printf("%d\n",H[qu(i,j,k)]); } } return 0; }