首页 > 代码库 > POJ-2104-K-th Number(可持久化线段树)

POJ-2104-K-th Number(可持久化线段树)

K-th Number
Time Limit: 20000MS Memory Limit: 65536K
Total Submissions: 55456 Accepted: 19068
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 31 5 2 6 3 7 42 5 34 4 11 7 3

Sample Output

563

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
 
先离散化下,在跑一个求区间第K小的可持久化线段树模板,没了。。。
不过,开始用读入外挂read()会莫名WA,改成scanf就A掉过了。。。
怪哉怪哉!
 
#include<iostream>#include<fstream>#include<cstdio>#include<algorithm>#include<string>#include<vector>#include<queue>#include<deque>#include<utility>#include<map>#include<set>#include<cmath>#include<cstdlib>#include<ctime>#include<functional>#include<sstream>#include<cstring>#include<bitset>#include<stack>using namespace std;int n,m,x,y,z,size;int kkk[100005],root[5000005],lef[5000005],rig[5000005],sum[5000005];struct sdt{	int num,id;}a[100005];bool cmp(sdt x,sdt y){	return x.num<y.num;}void build(int l,int r,int pre,int &now,int v){	now=++size;	sum[now]=sum[pre]+1;	if(l==r)return ;	lef[now]=lef[pre];	rig[now]=rig[pre];	int mid=(l+r)/2;	if(v<=mid)build(l,mid,lef[pre],lef[now],v); else build(mid+1,r,rig[pre],rig[now],v);}int query(int l,int r,int v){	int ll=root[l-1],rr=root[r];	int L=1,R=n,mid;	while(L!=R)	{		mid=(L+R)/2;		if(sum[lef[rr]]-sum[lef[ll]]>=v)		{			R=mid;			ll=lef[ll];			rr=lef[rr];		}		else		{			v-=(sum[lef[rr]]-sum[lef[ll]]);			L=mid+1;			ll=rig[ll];			rr=rig[rr];		}	}	return L;}int main(){	scanf("%d%d",&n,&m);	for(int i=1;i<=n;i++)	{		scanf("%d",&a[i].num);		a[i].id=i;	}		sort(a+1,a+n+1,cmp);	for(int i=1;i<=n;i++)kkk[a[i].id]=i;	for(int i=1;i<=n;i++)build(1,n,root[i-1],root[i],kkk[i]);		for(int i=1;i<=m;i++)	{		scanf("%d%d%d",&x,&y,&z);		printf("%d\n",a[query(x,y,z)].num);	}		return 0;}

  

POJ-2104-K-th Number(可持久化线段树)