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.
题目大意,给定一个整数序列a[N],询问区间ai到aj之间第k大的数是多少。共m次询问,对每次询问输出结果。
暴力
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=100000+10;
struct node{
int s,location;
};
node a[maxn];
int comp(node x,node y){
return x.s<y.s;
}
int main(){
int i,j,k,m,n,x,y;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++){
scanf("%d",&a[i].s);
a[i].location=i;
}
sort(a+1,a+n+1,comp);
for(i=1;i<=m;i++){
scanf("%d%d%d",&x,&y,&k);
for(j=1;j<=n;j++){
if(a[j].location>=x && a[j].location<=y)k--;
if(k==0){
printf("%d\n",a[j].s);
break;
}
}
}
return 0;
}
划分树
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAX 100005
int tree[20][MAX],sorted[MAX];
int toLeft[20][MAX];
void build(int level,int x,int y){
if(x==y)return;
int mid=(x+y)/2;
int i;
int suppose;
suppose=mid-x+1;
for(i=x;i<=y;i++)
if(tree[level][i]<sorted[mid])
suppose--;
int lpos=x,rpos=mid+1;
for(i=x;i<=y;i++){
if(i==x)
toLeft[level][i]=0;
else
toLeft[level][i]=toLeft[level][i-1];
if(tree[level][i]<sorted[mid]){
toLeft[level][i]++;
tree[level+1][lpos++]=tree[level][i];
}
else if(tree[level][i]>sorted[mid])
tree[level+1][rpos++]=tree[level][i];
else{
if(suppose!=0){
suppose--;
toLeft[level][i]++;
tree[level+1][lpos++]=tree[level][i];
}
else
tree[level+1][rpos++]=tree[level][i];
}
}
build(level+1,x,mid);
build(level+1,mid+1,y);
}
int find(int level,int x,int y,int ql,int qr,int k){
if(ql==qr)
return tree[level][ql];
int s,ss;
int mid=(x+y)/2;
if(x==ql){
s=0;
ss=toLeft[level][qr];
}
else{
s=toLeft[level][ql-1];
ss=toLeft[level][qr]-s;
}
int nl,nr;
if(k<=ss){
nl=x+s;
nr=x+s+ss-1;
return find(level+1,x,mid,nl,nr,k);
}
else{
nl=mid-x+1+ql-s;
nr=mid-x+1+qr-s-ss;
return find(level+1,mid+1,y,nl,nr,k-ss);
}
}
int main(){
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
int i;
for(i=1;i<=n;i++){
scanf("%d",&tree[0][i]);
sorted[i]=tree[0][i];
}
sort(sorted+1,sorted+n+1);
build(0,1,n);
int ql,qr,k;
for(i=1;i<=m;i++){
scanf("%d%d%d",&ql,&qr,&k);
printf("%d\n",find(0,1,n,ql,qr,k));
}
}
return 0;
}