POJ 2104 K-th Number(主席树,区间第K大的数)

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

求区间中第k大的数。

#include 
#include 
#define MAXN 100010
using namespace std;
int T, n, m, tot, a[MAXN], b[MAXN], cnt;
//T为测试样例数,n为元数组个数,m为查询个数,tot为建树的新节点
//a为原数列,b用来离散化的数列,其unique后元素的个数为cnt
struct N {
	int ls, rs, w;
	//ls左结点下标,rs右结点下标,w区间元素个数
}tree[30*MAXN];
int root[MAXN];//记录每棵树的根节点下标
int Build_tree(int l, int r) {//建树
	int newnode = tot++;
	tree[newnode].w = 0;
	if(l != r) {
		int mid = (l + r) >> 1;
		tree[newnode].ls = Build_tree(l, mid);
		tree[newnode].rs = Build_tree(mid + 1, r);		
	}
	return newnode;
}
int update(int rt, int pos, int  val) {
	int newnode = tot++;
	int temp = newnode;
	tree[newnode].w = tree[rt].w + val;
	int l = 1, r = cnt;
	while(l < r) {
		int mid = (l + r) >> 1;
		if(pos <= mid) {
			tree[newnode].ls = tot++;
			tree[newnode].rs = tree[rt].rs;
			newnode = tree[newnode].ls;
			rt = tree[rt].ls;
			r = mid;
		} else {
			tree[newnode].ls = tree[rt].ls;
			tree[newnode].rs = tot++;
			newnode = tree[newnode].rs;
			rt = tree[rt].rs;
			l = mid + 1;
		}
		tree[newnode].w = tree[rt].w + val;
	}
	return temp;
}
int query(int rt1, int rt2, int k) {
	int l = 1, r = cnt;
	while(l < r) {
		int mid = (l + r) >> 1;
		int tmp = tree[tree[rt2].ls].w - tree[tree[rt1].ls].w;
		if(tmp >= k) {
			rt1 = tree[rt1].ls;
			rt2 = tree[rt2].ls;
			r= mid;
		} else {
			k -= tmp;
			rt1 = tree[rt1].rs;
			rt2 = tree[rt2].rs;
			l = mid + 1;
		}
	}
	return l;
}
int main() {
		scanf("%d %d", &n, &m);
		for(int i = 1; i <= n; i++) {
 			scanf("%d", &a[i]);
 			b[i - 1] = a[i];
		}
		sort(b, b + n);
		cnt = unique(b, b + n) - b;
		tot = 0;
		root[0] = Build_tree(1, cnt);
		for(int i = 1; i <= n; i++) {
			int tmp = (int)(lower_bound(b, b + cnt, a[i]) - b) + 1;
			root[i] = update(root[i - 1], tmp, 1);
		}
		int l, r, k;
		for(int i = 0; i < m; i++) {
			scanf("%d %d %d", &l, &r, &k);
			int tmp = query(root[l - 1], root[r], k);
			printf("%d\n", b[tmp - 1]);
		}
	return 0;
}

 

你可能感兴趣的:(算法,数据结构,刷题之路)