K-th Number
Time Limit: 20000MS | Memory Limit: 65536K | |
Total Submissions: 59874 | Accepted: 20919 | |
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<algorithm>
using namespace std;
int tree[21][100008];//代表树
int toleft[21][100008];
int sorted[100008];
void build(int row,int left,int right){
if(left==right) return;
int mid=(left+right)>>1;
int leftequal=mid-left+1;
for(int i=left;i<=right;i++) //用来调整 与 中间值相同的数
{
if(tree[row][i]<sorted[mid])
{
leftequal--;
}
}
int lpos=left;
int rpos=mid+1;
for(int i=left;i<=right;i++)
{
if(i==left)
{
toleft[row][i]=0;
}
else
{
toleft[row][i]=toleft[row][i-1];
}
if(tree[row][i]<sorted[mid])
{
toleft[row][i]++;
tree[row+1][lpos++]=tree[row][i];
}
else if(tree[row][i]>sorted[mid])
{
tree[row+1][rpos++]=tree[row][i];
}
else
{
if(leftequal!=0)
{
leftequal--;
toleft[row][i]++;
tree[row+1][lpos++]=tree[row][i];
}
else
{
tree[row+1][rpos++]=tree[row][i];
}
}
}
build(row+1,left,mid);
build(row+1,mid+1,right);
}
int query(int row,int left,int right,int ql,int qr,int k){
if(ql==qr)
{
return tree[row][ql];
}
int s;
int ss;
int mid=(left+right)>>1;
if(left==ql)
{
s=0;
ss=toleft[row][qr];
}
else
{
s=toleft[row][ql-1];
ss=toleft[row][qr]-s;
}
int nl,nr;
if(k<=ss){
nl=left+s;
nr=left+s+ss-1;
return query(row+1,left,mid,nl,nr,k);
}
else
{
nl=mid-left+1+ql-s;
nr=mid-left+1+qr-s-ss;
return query(row+1,mid+1,right,nl,nr,k-ss);
}
}
int main(){
int n,m;cin>>n>>m;
// while(cin>>n>>m && n!=EOF & m!=EOF)
{
for(int i=1;i<=n;i++)
{
cin>>tree[1][i];
sorted[i]=tree[1][i];
}
sort(sorted+1,sorted+n+1);
build(1,1,n);
int ql,qr,k;
for(int i=1;i<=m;i++)
{
cin>>ql>>qr>>k;
cout<<query(1,1,n,ql,qr,k)<<endl;
}
}
return 0;
}