[POJ 2104] K-th Number【归并树】

  • 2018-01-22
  • 0
  • 0

Problem:

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 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

Solution:

区间第 k 最值是一个非常经典的问题,据说可以用主席树(可持久化线段树)平衡树等高端技术解决,但是还有一种相对简单的解决方案,就是将线段树归并排序巧妙地结合在一起,成为一种专门求解第 k 最值的数据结构“归并树”

关于归并树的实现和复杂度分析: 【转载】归并树

Code: O(nlogn+mlog3n) [8208K, 2000MS]

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

inline void getint(int &num){
	char ch; int sym = 1;
	while(!isdigit(ch = getchar())) if(ch == '-') sym = -1;
	num = ch - '0';
	while(isdigit(ch = getchar())) num = (num << 1) + (num << 3) + ch - '0';
	num *= sym;
}

int n, m, a[100010];

struct Merge_Tree{
	int node[18][300010];
	
	inline void build(int depth, int l, int r, int *a){
		if(l == r){
			node[depth][l] = a[l];
			return;
		}
		int smid = l + r >> 1;
		build(depth + 1, l, smid, a);
		build(depth + 1, smid + 1, r, a);
		int i = l, j = smid + 1, k = l;
		while(i <= smid && j <= r){
			if(node[depth + 1][i] < node[depth + 1][j])
				node[depth][k++] = node[depth + 1][i++];
			else node[depth][k++] = node[depth + 1][j++];
		}
		while(i <= smid) node[depth][k++] = node[depth + 1][i++];
		while(j <= r) node[depth][k++] = node[depth + 1][j++];  // Thus data in every node are sorted
	}
	
	inline int rank(int depth, int l, int r, int ql, int qr, const int &key){  // Return the rank of key in [ql, qr], where the minimum is regarded as Rank 0
		if(l == ql && r == qr) return lower_bound(node[depth] + l, node[depth] + r + 1, key) - node[depth] - l;
		int smid = l + r >> 1;
		if(qr <= smid) return rank(depth + 1, l, smid, ql, qr, key);
		else if(ql > smid) return rank(depth + 1, smid + 1, r, ql, qr, key);
		else return rank(depth + 1, l, smid, ql, smid, key) + rank(depth + 1, smid + 1, r, smid + 1, qr, key);
	}
	
	inline int query(int ql, int qr, int qrnk){
		int l = 1, r = n;
		while(l < r){
			int smid = l + r + 1 >> 1;
			int rnk = rank(0, 1, n, ql, qr, node[0][smid]);
			if(rnk <= qrnk) l = smid;
			else r = smid - 1;
		}  // Thus we get the maximum of the query rank
		return node[0][l];
	}
} mgt;

int main(){
	getint(n), getint(m);
	for(register int i = 1; i <= n; i++) getint(a[i]);
	mgt.build(0, 1, n, a);
	while(m--){
		int ql, qr, k;
		getint(ql), getint(qr), getint(k);
		printf("%d\n", mgt.query(ql, qr, k - 1));
	}
	return 0;
}

评论

还没有任何评论,你来说两句吧



新博客地址: darkleafin.cf

常年不在线的QQ:
49750

不定期更新的GitHub:
https://github.com/Darkleafin


OPEN AT 2017.12.10

如遇到代码不能正常显示的情况,请刷新页面。
Please refresh the page if the code cannot be displayed normally.


发现一个优美的网站:
https://visualgo.net/en
















- Theme by Qzhai