【划分树】K-th Number POJ2104 POJ2761

本文介绍了一种解决K-thNumber问题的有效方法,通过构建划分树来快速获取数组某段区间内的第K个数。文章提供了完整的代码实现,并对关键步骤进行了注释说明。

K-th Number
Time Limit: 20000MS Memory Limit: 65536K
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 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.

Source

Northeastern Europe 2004, Northern Subregion






很裸的划分树

第一次写,蛋疼。。。。。。


具体就不说了,马上去写学习报告


代码中有很多+1-1在一起,大多是为了好理解就没有约掉,如果不习惯就约掉了在看吧


POJ2761换了一个背景,其他一模一样


测评情况(POJ)



/*http://blog.youkuaiyun.com/jiangzh7
By Jiangzh*/
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=100000+10;

int n,m;
int sorted[N];
struct Parti_Tree{int val,left;}val[20][N];

void build_tree(int d,int l,int r)
{
	if(l==r) return;
	int m=(l+r)>>1;
	int same=m-l+1;
	int lcnt=l,rcnt=m+1;
	for(int i=l;i<=r;i++) if(val[d][i].val<sorted[m]) same--;
	for(int i=l;i<=r;i++)
	{
		int flag=0;
		if((val[d][i].val<sorted[m])||(val[d][i].val==sorted[m]&&same>0))
		{
			flag=1;//表示当前数分到了左子树
			val[d+1][lcnt++]=val[d][i];
			if(val[d][i].val==sorted[m]) same--;
		}
		else{
			val[d+1][rcnt++]=val[d][i];
		}
		val[d][i].left=val[d][i-1].left+flag;
	}
	build_tree(d+1,l,m);
	build_tree(d+1,m+1,r);
}

int query(int d,int l,int r,int x,int y,int k)
{
	if(l==r) return val[d][l].val;
	int m=(l+r)>>1;
	int lx=val[d][x-1].left-val[d][l-1].left;//[l,x-1]中进入左子树的有lx个
	int ly=val[d][y].left-val[d][x-1].left;//[x,y]中进入左子树的有ly个
	int rx=(x-1)-l+1-lx;//[l,x-1]中进入右子树的有rx个
	int ry=y-x+1-ly;//[x,y]中进入右子树的有ry个
	if(ly>=k) return query(d+1,l,m,l-1+lx+1,l-1+lx+ly,k);
	else return query(d+1,m+1,r,m+1-1+rx+1,m+1-1+rx+ry,k-ly);
}

void work()
{
	int x,y,k;
	scanf("%d%d%d",&x,&y,&k);
	printf("%d\n",query(0,1,n,x,y,k));
}

int main()
{
	freopen("poj2104.in","r",stdin);
	freopen("poj2104.out","w",stdout);
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&sorted[i]);
		val[0][i].val=sorted[i];
	}
	sort(sorted+1,sorted+1+n);
	build_tree(0,1,n);
	while(m--) work();
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值