K-th Number
Time Limit: 20000MS
Memory Limit: 65536K
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.
Source
Northeastern Europe 2004, Northern Subregion
大致翻译如下:
给定一个由 n 个数字组成的数列和m 组询问 (1≤n≤100000,1≤m≤5000) ,每次询问包含 (i,j,k) ,回答 a[i..j] 按升序排列后第 k 个数(即第k 小的数)。
一个基本框架:二分结果,转化为判定性问题。
思路一:分块进行判断,把每个块都排好序后,若某一块完整在区间内则二分查找统计次数,否则就一个个枚举即可(我写的分块太差导致在
POJ
上
T
<script type="math/tex" id="MathJax-Element-9">T</script>了)
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
const int N=100010;
const int B=1000;//每一块的容量
int n,m,a[N],num[N];
vector<int>bucket[1100];//分块
void in(int &x)
{
int f=1;char t=getchar();x=0;
while((t<48)or(t>57)){if(t=='-')f=-1;t=getchar();}
while((t>=48)and(t<=57)){x=x*10+t-48;t=getchar();}
x*=f;
}
int main()
{
in(n),in(m);
for (int i=1;i<=n;++i)
{
in(num[i]);a[i]=num[i];
bucket[i/B].push_back(num[i]);
}
sort(num+1,num+n+1);
for (int i=0;i<=n/B;++i) sort(bucket[i].begin(),bucket[i].end());
for (int i=1;i<=m;++i)
{
int l,r,k;
in(l),in(r),in(k);++r;//查询区间[l,r),即左闭右开
int head=1,tail=n;
while(head<tail)
{
int mid=(head+tail)>>1;
int x=num[mid];
int tl=l,tr=r,w=0;
while((tl<tr)and(tl%B!=0)) w+=a[tl++]<=x;
while((tl<tr)and(tr%B!=0)) w+=a[--tr]<=x;
while(tl<tr)
{
int t=tl/B;
w+=upper_bound(bucket[t].begin(),bucket[t].end(),x)-bucket[t].begin();
tl+=B;
}
if (w>=k) tail=mid;
else head=mid+1;
}
printf("%d\n",num[head]);
}
return 0;
}
思路二:用线段树,每一个节点保存的是一个相应范围的有序的区间,合并操作相当于归并排序。
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
const int N=100010;
const int maxt=(1<<18)-1;
vector<int>tree[maxt];
int n,a[N],m,num[N];
void in(int &x)
{
int f=1;char t=getchar();x=0;
while((t<48)or(t>57)){if(t=='-')f=-1;t=getchar();}
while((t>=48)and(t<=57)){x=x*10+t-48;t=getchar();}
x*=f;
}
void updata(int now)
{
int lch=(now<<1),rch=(now<<1)+1;
merge(tree[lch].begin(),tree[lch].end(),tree[rch].begin(),tree[rch].end(),tree[now].begin());
}
void build(int now,int l,int r)
{
if (l==r)
{
tree[now].push_back(a[l]);
return;
}
int lch=(now<<1),rch=(now<<1)+1;
int mid=(l+r)>>1;
build(lch,l,mid);
build(rch,mid+1,r);
tree[now].resize(r-l+1);
updata(now);
}
int query(int now,int l,int r,int x,int lrange,int rrange)
{
if ((r<lrange)or(l>rrange)) return 0;
else if ((lrange<=l)and(r<=rrange))
return upper_bound(tree[now].begin(),tree[now].end(),x)-tree[now].begin();
else
{
int lch=(now<<1),rch=(now<<1)+1,ans=0;
int mid=(l+r)>>1;
ans+=query(lch,l,mid,x,lrange,rrange);
ans+=query(rch,mid+1,r,x,lrange,rrange);
return ans;
}
}
int main()
{
in(n),in(m);
for (int i=1;i<=n;++i)
{in(a[i]);num[i]=a[i];}
sort(num+1,num+n+1);
build(1,1,n);
for (int i=1;i<=m;++i)
{
int l,r,k;
in(l),in(r),in(k);
int head=1,tail=n;
while(head<tail)
{
int mid=(head+tail)>>1;
int w=query(1,1,n,num[mid],l,r);
if (w>=k) tail=mid;
else head=mid+1;
}
printf("%d\n",num[head]);
}
return 0;
}