poj2104 K-th Number

K-th Number
Time Limit: 20000MS Memory Limit: 65536K
Total Submissions: 43988 Accepted: 14569
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



有没有觉得题目很熟悉啊?是不是感觉和poj2761很像啊?不过这道题稍微麻烦一点,区间之间存在包含关系,所以就不能用排序+平衡树做了。

这道题的正确解法是划分树(可用于求区间第k小数,复杂度log(n))。划分树是一种基于线段树的数据结构,基本思想是对于一个区间,将它划分成两个区间,左区间的数全部小于等于右区间的数,分别对应左右子树。构建划分树时要记录到达某个位置时进入左子树的数的个数num,查询时就可以通过num确定下一个查询区间,最后将区间范围缩小到1,也就找到了答案。(详见代码…)




#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#define F(i,j,n) for(int i=j;i<=n;i++)
#define D(i,j,n) for(int i=j;i>=n;i--)
#define LL long long
#define pa pair<int,int>
#define MAXN 100005
using namespace std;
int n,m,a[MAXN],val[20][MAXN],num[20][MAXN];
inline int read()
{
	int ret=0,flag=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') flag=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){ret=ret*10+ch-'0';ch=getchar();}
	return ret*flag;
}
inline void build(int l,int r,int x)
{
	if (l==r) return;
	int mid=(l+r)>>1,lsame=mid-l+1,same=0,ln=l,rn=mid+1;
	F(i,l,r) if (val[x][i]<a[mid]) lsame--;
	F(i,l,r)
	{
		if (i==l) num[x][i]=0;else num[x][i]=num[x][i-1];
		if (val[x][i]<a[mid]) num[x][i]++,val[x+1][ln++]=val[x][i];
		else if (val[x][i]>a[mid]) val[x+1][rn++]=val[x][i];
		else
		{
			if (lsame>=++same) num[x][i]++,val[x+1][ln++]=val[x][i];
			else val[x+1][rn++]=val[x][i];
		}
	}
	build(l,mid,x+1);
	build(mid+1,r,x+1);
}
inline int query(int st,int ed,int k,int l,int r,int x)
{
	if (l==r) return val[x][l];
	int lx,ly,rx,ry,mid=(l+r)>>1;
	if (st==l) lx=0,ly=num[x][ed];
	else lx=num[x][st-1],ly=num[x][ed]-lx;
	if (ly>=k)
	{
		st=l+lx;ed=st+ly-1;
		return query(st,ed,k,l,mid,x+1);
	}
	else
	{
		rx=st-l-lx;ry=ed-st+1-ly;
		st=mid+1+rx;ed=st+ry-1;
		return query(st,ed,k-ly,mid+1,r,x+1);
	}
}
int main()
{
	n=read();m=read();
	F(i,1,n) val[0][i]=a[i]=read();
	sort(a+1,a+n+1);
	build(1,n,0);
	F(i,1,m)
	{
		int x=read(),y=read(),k=read();
		printf("%d\n",query(x,y,k,1,n,0));
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值