码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2104 K-th Number

时间:2014-07-28 15:56:43      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   os   strong   io   for   


静态区间第K大,主席树写法。

主席树第一题,如果对整段区间建线段树可以求1~n范围内的第K大,要想求任意区间内的第K大需要建1~i 的n棵线段树。求某一段的时候区间相减就可以了。但是这样空间消耗太大所以要尽量利用以前的节点。注意到1~i的线段树和i~i+1的线段树只某一条链上的值不同,所以我们只要建出这一条链就可以了,剩下的节点和1~i+1的线段树是一样的,我们直接把节点指过去。

T[ ... ]记录以1~...的线段树的root节点,保存各种不同版本持久化

c[ ... ]记录下面有多少个节点


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

[Submit]   [Go Back]   [Status]   [Discuss]


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn=110000;

int n,m,q;
int a[maxn],t[maxn];
int T[maxn],lson[maxn*50],rson[maxn*50],c[maxn*50],tot;

void init_hash()
{
	tot=0;
	sort(t+1,t+1+n);
	m=unique(t+1,t+1+n)-t;
}

int hash(int x)
{
	return lower_bound(t+1,t+1+n,x)-t;
}

int build(int l,int r)
{
	int root=tot++,temp=root;
	c[root]=0;
	if(l==r)
	{
		int mid=(l+r)/2;
		lson[root]=build(l,mid);
		rson[root]=build(mid+1,r);
	}
	return temp;
}

int update(int root,int pos,int val)
{
	int newroot=tot++,temp=newroot;
	c[newroot]=c[root]+val;
	int l=1,r=m;
	while(l<r)
	{
		int mid=(l+r)/2;
		if(pos<=mid)
		{
			r=mid;
			lson[newroot]=tot++;
			rson[newroot]=rson[root];
			root=lson[root];
			newroot=lson[newroot];
		}
		else
		{
			l=mid+1;
			rson[newroot]=tot++;
			lson[newroot]=lson[root];
			root=rson[root];
			newroot=rson[newroot];
		}
		c[newroot]=c[root]+val;
	}
	return temp;	
}

int query(int left_son,int right_son,int x)
{
	int l=1,r=m;
	while(l<r)	
	{
		int mid=(l+r)/2;
		int tk=c[lson[left_son]]-c[lson[right_son]];
		if(tk>=x)
		{
			left_son=lson[left_son];
			right_son=lson[right_son];
			r=mid;
		}
		else
		{
			l=mid+1;
			x-=tk;
			left_son=rson[left_son];
			right_son=rson[right_son];
		}
	}
	return l;
}

int main()
{
	while(scanf("%d%d",&n,&q)!=EOF)
	{
		for(int i=1;i<=n;i++)
		{
			scanf("%d",a+i);
			t[i]=a[i];
		}
		init_hash();
		T[n+1]=build(1,m);
		for(int i=n;i;i--)
		{
			T[i]=update(T[i+1],hash(a[i]),1);
		}
		while(q--)
		{
			int a,b,c;
			scanf("%d%d%d",&a,&b,&c);
			printf("%d\n",t[query(T[a],T[b+1],c)]);
		}
	}
	return 0;
}



POJ 2104 K-th Number,布布扣,bubuko.com

POJ 2104 K-th Number

标签:des   style   http   color   os   strong   io   for   

原文地址:http://blog.csdn.net/ck_boss/article/details/38228037

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!