K-th Number
Time Limit: 20000MS | Memory Limit: 65536K | |
Total Submissions: 21235 | Accepted: 5681 | |
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.
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).
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.
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=101000;
const int DEEP=20;//划分树最多层数
struct Node
{
int l,r;
};
Node tree[N*4];//线段树
int data[N];//数据
int seg[DEEP][N];//划分树
int LessMid[DEEP][N];//表示在[L,R]内有几个数小于等于date[mid]
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=101000;
const int DEEP=20;//划分树最多层数
struct Node
{
int l,r;
};
Node tree[N*4];//线段树
int data[N];//数据
int seg[DEEP][N];//划分树
int LessMid[DEEP][N];//表示在[L,R]内有几个数小于等于date[mid]
void buildtree(int root,int l,int r,int d)//节点,左右区间,层数
{
tree[root].l=l,tree[root].r=r;
if(l==r) return ;//叶子节点
int mid=(l+r)>>1;
int lsame=mid-l+1;//左面最多可放几个与data[mid]相同的数
for(int i=l;i<=r;i++)//得出实际能放几个相同的数
{
if(seg[d][i]<data[mid]) lsame--;
}
{
tree[root].l=l,tree[root].r=r;
if(l==r) return ;//叶子节点
int mid=(l+r)>>1;
int lsame=mid-l+1;//左面最多可放几个与data[mid]相同的数
for(int i=l;i<=r;i++)//得出实际能放几个相同的数
{
if(seg[d][i]<data[mid]) lsame--;
}
int tl=l,tr=mid+1,same=0;//划分树 ,tl,tr表示数的左右子树的起点
for(int i=l;i<=r;i++)
{
if(i==l) LessMid[d][i]=0;//表示在[L,R]内有几个数小于等于date[mid]
else LessMid[d][i]=LessMid[d][i-1];
for(int i=l;i<=r;i++)
{
if(i==l) LessMid[d][i]=0;//表示在[L,R]内有几个数小于等于date[mid]
else LessMid[d][i]=LessMid[d][i-1];
if(seg[d][i]<data[mid]) LessMid[d][i]++,seg[d+1][tl++]=seg[d][i];//划分到左子树
else if(seg[d][i]>data[mid]) seg[d+1][tr++]=seg[d][i];//划分到右子树
else //相等情况
{
if(same<lsame) same++,LessMid[d][i]++,seg[d+1][tl++]=seg[d][i];//左子树未满
else seg[d+1][tr++]=seg[d][i];
}
}
buildtree(root<<1,l,mid,d+1);
buildtree((root<<1)+1,mid+1,r,d+1);
}
//查询[l,r]中的第k小数
int Query(int root,int l,int r,int d,int cnt)//节点,要查询的区间,层数,第cnt小数
{
if(l==r) return seg[d][l];
else if(seg[d][i]>data[mid]) seg[d+1][tr++]=seg[d][i];//划分到右子树
else //相等情况
{
if(same<lsame) same++,LessMid[d][i]++,seg[d+1][tl++]=seg[d][i];//左子树未满
else seg[d+1][tr++]=seg[d][i];
}
}
buildtree(root<<1,l,mid,d+1);
buildtree((root<<1)+1,mid+1,r,d+1);
}
//查询[l,r]中的第k小数
int Query(int root,int l,int r,int d,int cnt)//节点,要查询的区间,层数,第cnt小数
{
if(l==r) return seg[d][l];
int s;//表示在[l,r]中有几个小于等于data[mid]的个数
int ss;//表示在[tree[root].l,l-1]中有几个小于等于data[mid]的个数
if(l==tree[root].l) s=LessMid[d][r],ss=0;
else s=LessMid[d][r]-LessMid[d][l-1],ss=LessMid[d][l-1];
if(s>=cnt) return Query(root<<1,tree[root].l+ss,tree[root].l+ss+s-1,d+1,cnt);
else
{
int mid=(tree[root].l+tree[root].r)>>1;
int bb=l-tree[root].l-ss;//表示[tree[root].l,l-1]中有多少个分到右面
int b=r-l+1-s;//表示[l,r]有多少个分到右面
return Query((root<<1)+1,mid+bb+1,mid+bb+b,d+1,cnt-s);
}
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)==2)
{
for(int i=1;i<=n;i++)
{
scanf("%d",&data[i]);
seg[1][i]=data[i];//划分树第一层
}
sort(data+1,data+1+n);
buildtree(1,1,n,1);
while(m--)
{
int l,r,k;scanf("%d%d%d",&l,&r,&k);
int res=Query(1,l,r,1,k);//查询[l,r]中的第k小数
printf("%d/n",res);
}
}
return 0;
}
int ss;//表示在[tree[root].l,l-1]中有几个小于等于data[mid]的个数
if(l==tree[root].l) s=LessMid[d][r],ss=0;
else s=LessMid[d][r]-LessMid[d][l-1],ss=LessMid[d][l-1];
if(s>=cnt) return Query(root<<1,tree[root].l+ss,tree[root].l+ss+s-1,d+1,cnt);
else
{
int mid=(tree[root].l+tree[root].r)>>1;
int bb=l-tree[root].l-ss;//表示[tree[root].l,l-1]中有多少个分到右面
int b=r-l+1-s;//表示[l,r]有多少个分到右面
return Query((root<<1)+1,mid+bb+1,mid+bb+b,d+1,cnt-s);
}
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)==2)
{
for(int i=1;i<=n;i++)
{
scanf("%d",&data[i]);
seg[1][i]=data[i];//划分树第一层
}
sort(data+1,data+1+n);
buildtree(1,1,n,1);
while(m--)
{
int l,r,k;scanf("%d%d%d",&l,&r,&k);
int res=Query(1,l,r,1,k);//查询[l,r]中的第k小数
printf("%d/n",res);
}
}
return 0;
}