K-th Number
Time Limit: 20000MS | Memory Limit: 65536K | |
Total Submissions: 64172 | Accepted: 22573 | |
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.
题目意思就是给你一系列数,然后询问某个区间第k小数是哪个数字
划分树详细入门可以参考下面给出的大佬的博客,我只是对其中某些细节做了更详细的说明
/*
http://www.cnblogs.com/hchlqlz-oj-mrj/p/5744308.html#3847494
https://blog.csdn.net/xiaofengcanyuexj/article/details/10614203
https://blog.csdn.net/hcx11333/article/details/76944275
*/
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N 100010
using namespace std;
typedef long long LL;
int a[N];
int sorted[N];
int num[20][N];
int val[20][N];
void build(int l,int r,int ceng)
{
if(l==r) return ;
int mid=(l+r)/2,isame=mid-l+1; //isame保存有多少和sorted[mid]一样大的数进入左孩子
for(int i=l;i<=r;i++) if(val[ceng][i]<sorted[mid]) isame--;
int ln=l,rn=mid+1;
for(int i=l;i<=r;i++)
{
if(i==l) num[ceng][i]=0;
else num[ceng][i]=num[ceng][i-1];
if(val[ceng][i]<sorted[mid] || val[ceng][i]==sorted[mid]&&isame>0)
{
val[ceng+1][ln++]=val[ceng][i];
num[ceng][i]++;
if(val[ceng][i]==sorted[mid]) isame--;
}
else
{
val[ceng+1][rn++]=val[ceng][i];
}
}
build(l,mid,ceng+1);
build(mid+1,r,ceng+1);
}
int look(int ceng,int sl,int sr,int l,int r,int k)
{
if(sl==sr) return val[ceng][sl];
int ly;
if(l==sl) ly=0;
else ly=num[ceng][l-1];
int tolef=num[ceng][r]-ly;
if(tolef>=k)
{
return look(ceng+1,sl,(sl+sr)/2,sl+ly,sl+num[ceng][r]-1,k);
}
else
{
int lr = (sl+sr)/2 + 1 + (l-sl-ly);
return look(ceng+1,(sl+sr)/2+1,sr,lr,lr+r-l+1-tolef-1,k-tolef);
}
}
int main()
{
int n,m,l,r,k;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=1;i<=n;i++)
{
scanf("%d",&val[0][i]);
sorted[i]=val[0][i];
}
sort(sorted+1,sorted+n+1);
build(1,n,0);
while(m--)
{
scanf("%d%d%d",&l,&r,&k);
printf("%d\n",look(0,1,n,l,r,k));
}
}
return 0;
}
/*
7 3
1 5 2 6 3 7 4
2 5 3
4 4 1
1 7 3
*/