K-th Number
Time Limit: 20000MS Memory Limit: 65536K
Total Submissions: 57392 Accepted: 19837
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
给定一个序列A
显然序列中第k大的数x,在序列中有至少k个数比x小
首先 简化问题为 求出在指定区间[s,e]中,a[s…e]中<=x的数量:
在线段树中 每个节点保存所在区间的排序后的序列
如果节点的端点为[l,r],要查询的区间为[s,e]
若[s,e]包含了[l,r],则[l,r]中<=x的数可以二分求出
每次查询为
log2(n)
于是二分法求出这个x
mlog3(n)+nlog(n)
#include<stdio.h>
#include<iostream>
#include<string>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#define ll long long
#define pii pair<int,int>
#define MEM(a,x) memset(a,x,sizeof(a))
#define lowbit(x) ((x)&-(x))
using namespace std;
const int inf=1e9+7;
const int N = 1e5+5;
const int M = 19;//log2(4*N)+1;
int elem[N];
int a[M][N],dat[4*N];
void init(int k,int l,int r,int d){
if(l<=r){
dat[k]=d;
if(l!=r){
int mid=(l+r)/2;
init(2*k+1,l,mid,d+1);
init(2*k+2,mid+1,r,d+1);
merge(a[d+1]+l,a[d+1]+mid+1,a[d+1]+mid+1,a[d+1]+r+1,a[d]+l);
}
else{
a[d][l]=elem[l];
}
}
}
int lowerNums(int k,int l,int r,int s,int e,int x){//[a,b]内<=x的数的个数
if(e<l||r<s){
return 0;
}
int d=dat[k];
if(s<=l&&r<=e){
return upper_bound(a[d]+l,a[d]+r+1,x)-(a[d]+l);
}
if(l<r){
int mid=(l+r)/2;
int ans=0;
ans+=lowerNums(2*k+1,l,mid,s,e,x);
ans+=lowerNums(2*k+2,mid+1,r,s,e,x);
return ans;
}
}
int slove(int i,int j,int k,int n){
int ans=-1;
int l=0,r=n-1;
while(l<=r){
int mid=(l+r)/2;
if(lowerNums(0,0,n-1,i-1,j-1,elem[mid])>=k){//[i,j]区间内<=elem[mid]的数个数>=k
ans=elem[mid];
r=mid-1;
}
else{
l=mid+1;
}
}
return ans;
}
int main()
{
//freopen("/home/lu/code/r.txt","r",stdin);
int n,m;
while(~scanf("%d%d",&n,&m)){
for(int i=0;i<n;++i){
scanf("%d",&elem[i]);
}
init(0,0,n-1,0);
sort(elem,elem+n);
while(m--){
int i,j,k;
scanf("%d%d%d",&i,&j,&k);
printf("%d\n",slove(i,j,k,n));
}
}
return 0;
}