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

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

【思路】

给定一个范围确定且各数互异的序列,求区间第k小的数。

依照整个序列的原本顺序,在它在所有数中的大小顺序的位置插入1,于是可以得到一棵表示区间有多少个数的线段树,我们构造这样许多棵线段树,用tree表示,tree[r] - tree[l- 1]就是表示在原顺序[l, r]区间内插入的数的个数。查询时对涉及节点的左子节点进行减法得cnt,若k <= cnt,则应从左子树接着找,若k > cnt,就跑右子树,同时k赋值为k - cnt。

在排序后为获取每个数的大小顺序,可用STL的二分lower_bound,总时间复杂度为O(NlogN + NlogN * logN + MlogN)。


【代码】

//************************************************************************
// File Name: main.cpp
// Author: Shili_Xu
// E-Mail: [email protected] 
// Created Time: 2018年02月12日 星期一 13时18分03秒
//************************************************************************

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int MAXN = 1e5 + 5;

struct segment {
	int l, r, mid;
	int data, lson, rson;
};

int n, m, cnt;
int a[MAXN], b[MAXN], root[MAXN];
segment seg[MAXN * 21];

void build(int left, int right, int &rt)
{
	rt = ++cnt;
	seg[rt].l = left;
	seg[rt].r = right;
	seg[rt].mid = (left + right) >> 1;
	seg[rt].data = 0;
	if (left == right) return;
	build(left, seg[rt].mid, seg[rt].lson);
	build(seg[rt].mid + 1, right, seg[rt].rson);
}

void modify(int pos, int num, int pre, int &now)
{
	now = ++cnt;
	seg[now] = seg[pre];
	if (seg[now].l == seg[now].r) {
		seg[now].data += num;
		return;
	}
	if (pos <= seg[now].mid)
		modify(pos, num, seg[pre].lson, seg[now].lson);
	else
		modify(pos, num, seg[pre].rson, seg[now].rson);
	seg[now].data = seg[seg[now].lson].data + seg[seg[now].rson].data;
}

int query(int x, int y, int k)
{
	if (seg[y].l == seg[y].r) return b[seg[y].mid];
	int cnt = seg[seg[y].lson].data - seg[seg[x].lson].data;
	if (k <= cnt)
		return query(seg[x].lson, seg[y].lson, k);
	else
		return query(seg[x].rson, seg[y].rson, k - cnt);
}

int main()
{
	scanf("%d %d", &n, &m);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
		b[i] = a[i];
	}
	sort(b + 1, b + 1 + n);
	cnt = 0;
	build(1, n, root[0]);
	for (int i = 1; i <= n; i++) {
		int id = lower_bound(b + 1, b + 1 + n, a[i]) - b;
		modify(id, 1, root[i - 1], root[i]);
	}
	while (m--) {
		int l, r, k;
		scanf("%d %d %d", &l, &r, &k);
		printf("%d\n", query(root[l - 1], root[r], k));
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/shili_xu/article/details/79449592