「POJ2104」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.
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).
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
1 2 3 4 5 |
7 3 1 5 2 6 3 7 4 2 5 3 4 4 1 1 7 3 |
Sample Output
1 2 3 |
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
Hint
学了点可持久很激动地来YY这题。。结果YY了一个nlogn^2的做法
离散化搞n棵权值线段树
如果查询(l,r)这个区间第k大
二分一个值 l-1棵上小等它的有x个,r棵上小等它的有y个
使得在y-x=k。。。
权值线段树上查排名。。。卧槽我到底在想什么
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; int n,m,sz,tot; int root[100001],a[100001],ls[8000001],rs[8000001],s[8000001]; int hash[100001],num[100001]; int find(int x) { int l=1,r=tot,mid; while(l<=r) { int mid=(l+r)>>1; if(hash[mid]<x)l=mid+1; else r=mid-1; } return l; } void insert(int l,int r,int x,int &y,int v) { y=++sz; s[y]=s[x]+1; if(l==r)return; ls[y]=ls[x];rs[y]=rs[x]; int mid=(l+r)>>1; if(v<=mid)insert(l,mid,ls[x],ls[y],v); else insert(mid+1,r,rs[x],rs[y],v); } int rank(int k,int l,int r,int x) { if(r==x)return s[k]; int mid=(l+r)>>1; if(x<=mid)return rank(ls[k],l,mid,x); else return s[ls[k]]+rank(rs[k],mid+1,r,x); } int ask(int L,int R,int v) { int x=root[L-1],y=root[R]; int l=1,r=tot; while(l<=r) { int mid=(l+r)>>1; int t=rank(y,1,tot,mid)-rank(x,1,tot,mid); if(t>=v)r=mid-1; else l=mid+1; } return l; } int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); num[i]=a[i]; } sort(num+1,num+n+1); hash[++tot]=num[1]; for(int i=2;i<=n;i++) if(num[i]!=num[i-1]) hash[++tot]=num[i]; for(int i=1;i<=n;i++) insert(1,tot,root[i-1],root[i],find(a[i])); for(int i=1;i<=m;i++) { int l,r,x;scanf("%d%d%d",&l,&r,&x); printf("%d\n",hash[ask(l,r,x)]); } return 0; } |
正常是这样做的。。。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; int n,m,sz,tot; int root[100001],a[100001],ls[8000001],rs[8000001],s[8000001]; int num[100001],hash[100001]; int find(int x) { int l=1,r=tot,mid; while(l<=r) { int mid=(l+r)>>1; if(hash[mid]<x)l=mid+1; else r=mid-1; } return l; } void insert(int l,int r,int x,int &y,int v) { y=++sz; s[y]=s[x]+1; if(l==r)return; ls[y]=ls[x];rs[y]=rs[x]; int mid=(l+r)>>1; if(v<=mid)insert(l,mid,ls[x],ls[y],v); else insert(mid+1,r,rs[x],rs[y],v); } int ask(int l,int r,int x,int y,int k) { if(l==r)return l; int mid=(l+r)>>1; if(s[ls[y]]-s[ls[x]]>=k) return ask(l,mid,ls[x],ls[y],k); else return ask(mid+1,r,rs[x],rs[y],k-(s[ls[y]]-s[ls[x]])); } int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); num[i]=a[i]; } sort(num+1,num+n+1); hash[++tot]=num[1]; for(int i=2;i<=n;i++) if(num[i]!=num[i-1]) hash[++tot]=num[i]; for(int i=1;i<=n;i++) insert(1,tot,root[i-1],root[i],find(a[i])); for(int i=1;i<=m;i++) { int l,r,x;scanf("%d%d%d",&l,&r,&x); printf("%d\n",hash[ask(1,tot,root[l-1],root[r],x)]); } return 0; } |
树套树各种T,放弃了。。。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<ctime> #include<algorithm> #define N 100001 #define M 2300001 using namespace std; inline int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return f*x; } int n,m,sz,tmp; int root[3*N],hash[N]; int ls[M],rs[M],rnd[M],v[M],s[M],w[M]; void update(int k) {s[k]=s[ls[k]]+s[rs[k]]+w[k];} void rturn(int &k) {int t=ls[k];ls[k]=rs[t];rs[t]=k;s[t]=s[k];update(k);k=t;} void lturn(int &k) {int t=rs[k];rs[k]=ls[t];ls[t]=k;s[t]=s[k];update(k);k=t;} void insert(int &k,int val) { if(!k){k=++sz;v[k]=val;w[k]=s[k]=1;rnd[k]=rand();return;} s[k]++; if(val==v[k])w[k]++; else if(val<v[k]) {insert(ls[k],val);if(rnd[ls[k]]<rnd[k])rturn(k);} else {insert(rs[k],val);if(rnd[rs[k]]<rnd[k])lturn(k);} } void askrank(int k,int val) { if(!k)return; if(val==v[k])tmp+=s[ls[k]]; else if(val<v[k])askrank(ls[k],val); else {tmp+=s[ls[k]]+w[k];askrank(rs[k],val);} } void getrank(int k,int l,int r,int x,int y,int val) { if(l==x&&r==y){askrank(root[k],val);return;} int mid=(l+r)>>1; if(mid>=y)getrank(k<<1,l,mid,x,y,val); else if(mid<x)getrank(k<<1|1,mid+1,r,x,y,val); else { getrank(k<<1,l,mid,x,mid,val); getrank(k<<1|1,mid+1,r,mid+1,y,val); } } void build(int k,int l,int r,int x,int val) { insert(root[k],val); if(l==r)return; int mid=(l+r)>>1; if(x<=mid)build(k<<1,l,mid,x,val); else build(k<<1|1,mid+1,r,x,val); } int ask(int x,int y,int rk) { int l=1,r=n,t=0; while(l<=r) { tmp=1; int mid=(l+r)>>1; getrank(1,1,n,x,y,hash[mid]); if(tmp<=rk){t=hash[mid];l=mid+1;} else r=mid-1; } return t; } int main() { n=read();m=read(); for(int i=1;i<=n;i++) { int x=hash[i]=read(); build(1,1,n,i,x); } sort(hash+1,hash+n+1); for(int i=1;i<=m;i++) { int x=read(),y=read(),v=read(); int t=ask(x,y,v); printf("%d\n",t); } return 0; } |
Subscribe