POJ2104 K-th Number【划分树】

K-th Number

Time Limit: 20000MS   Memory Limit: 65536K
Total Submissions: 68626   Accepted: 24285
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

题解:求区间[l,r]的第K小数,划分树模板题

AC的C++代码:

#include<iostream>
#include<algorithm>

using namespace std;
const int N=100100;
const int Deep=20;

struct PartitionTree{
	int num[N],cnt[N];
}tree[Deep];

int sorted[N];

void build(int deep,int l,int r)
{
	if(l==r)
	  return;
	int mid=(l+r)>>1;
	int key=sorted[mid];
	int scnt=mid-l+1;
	for(int i=l;i<=r;i++)
	  if(tree[deep].num[i]<key)
	    scnt--;
	int p=l-1,p2=mid;
	for(int i=l,cnt=0;i<=r;i++){
		int num=tree[deep].num[i];
		if(num<key||(num==key&&scnt)){
			if(num==key)
			  scnt--;
			cnt++;
			tree[deep+1].num[++p]=num;
		}
		else
		  tree[deep+1].num[++p2]=num;
		tree[deep].cnt[i]=cnt;
	}
	build(deep+1,l,mid);
	build(deep+1,mid+1,r);
}

int query(int deep,int l,int r,int L,int R,int k)
{
	if(l==r)
	  return tree[deep].num[l];
	int mid=(l+r)>>1;
	int left=0,sum_in_left=tree[deep].cnt[R];
	if(l!=L){
		left=tree[deep].cnt[L-1];
		sum_in_left-=left;
	}
	if(sum_in_left>=k){
		L=l+left;
		R=L+sum_in_left-1;
		return query(deep+1,l,mid,L,R,k);
	}
	else{
		int a=L-l-left;
		int b=R-L+1-sum_in_left;
		L=mid+1+a;
		R=L+b-1;
		return query(deep+1,mid+1,r,L,R,k-sum_in_left);
	}
}

int main()
{
	int n,m,L,R,k;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		scanf("%d",&sorted[i]);
		tree[0].num[i]=sorted[i];
	}
	sort(sorted+1,sorted+1+n);
	build(0,1,n);
	while(m--){
		scanf("%d%d%d",&L,&R,&k);
		printf("%d\n",query(0,1,n,L,R,k));
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/SongBai1997/article/details/82622200