hdu 2104 K-th Number(静态求区间第k小+整体二分)

针对大量整数数组的快速查询需求,本文介绍了一种高效的数据结构实现方法,用于解决K-thNumber问题。该方法能够在限定时间内准确返回指定区间内第K小的数值,通过分治策略减少不必要的计算。
K-th Number
Time Limit: 20000MS Memory Limit: 65536K
Total Submissions: 46551 Accepted: 15539
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

solution:

总共有添加和查询两种操作。 这个判定操作是属于左区间还是右区间的过程就是通过比较比二分的mid大的数的个数和k。同时我们看到,如果比二分的mid小的数的个数小于k了,我们是要去寻找大的答案,那么这些比mid大的数在以后的递归里始终会对答案有贡献,所以我们没必要去做重复的工作,只需要把这些数的个数累积到贡献里,以后递归的时候就不用考虑这些数了。如果比二分的mid小的数大于k,就要寻找小的答案,那么那些本来就比mid大的添加和删除操作对答案不会产生影响,可以放在答案右区间。 详细见代码~

注意 此题范围是绝对值不超过1e9,因此divide要从-inf~inf

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#define mem(a) memset(a,0,sizeof(a))
const int maxn = 250000;
const int inf = 1e9 + 7;
using namespace std;
struct query
{
	int x, y, k, s, tp, cur;//添加:tp=1 查询:tp=3
}q[maxn], q1[maxn], q2[maxn];
int a[maxn], ans[maxn], tmp[maxn], bit[maxn];
int n, m, num, cnt;//cnt为查询的顺序
void init()
{
	cnt = 0; num = 0;
	mem(bit); mem(tmp); mem(q);
}
void add(int x, int y)
{
	for (int i = x; i <= n; i += (i&-i))
		bit[i] += y;
}
int sum(int x)
{
	int tmp = 0;
	for (int i = x; i>0; i -= (i&-i))
		tmp += bit[i];
	return tmp;
}
void divide(int head, int tail, int l, int r)//表示下标head到tail的操作的答案区间为[l,r] 
{
	if (head>tail) return;
	if (l == r)
	{
		for (int i = head; i <= tail; i++)
			if (q[i].tp == 3) ans[q[i].s] = l;
		return;
	}
	int mid = (l + r) >> 1;
	for (int i = head; i <= tail; i++)
	{
		if (q[i].tp == 1 && q[i].y <= mid) add(q[i].x, 1);
		else if (q[i].tp == 3) tmp[i] = sum(q[i].y) - sum(q[i].x - 1);
	}
	for (int i = head; i <= tail; i++)
	{
		if (q[i].tp == 1 && q[i].y <= mid) add(q[i].x, -1);
	}
	int l1 = 0, l2 = 0;
	for (int i = head; i <= tail; i++)
		if (q[i].tp == 3)
		{
		if (q[i].cur + tmp[i]>q[i].k - 1)//q[i].cur+tmp[i]表示累积了多少个数
			q1[++l1] = q[i];
		else
		{
			q[i].cur += tmp[i];
			q2[++l2] = q[i];
		}
		}
		else
		{
			if (q[i].y <= mid) q1[++l1] = q[i];
			else q2[++l2] = q[i];
		}
	for (int i = 1; i <= l1; i++) q[head + i - 1] = q1[i];
	for (int i = 1; i <= l2; i++) q[head + l1 + i - 1] = q2[i];
	divide(head, head + l1 - 1, l, mid);
	divide(head + l1, tail, mid + 1, r);
}
int main()
{
	while (~scanf("%d%d", &n,&m))
	{
		init();
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &a[i]);
			q[++num].x = i; q[num].y = a[i];
			q[num].tp = 1; q[num].s = 0;
		}
		int x, y, z;
		for (int i = 1; i <= m; i++)
		{
			scanf("%d%d%d", &x, &y, &z);
			q[++num].x = x, q[num].y = y, q[num].k = z;
			q[num].tp = 3; q[num].s = ++cnt;
		}
		divide(1, num,-inf, inf);
		for (int i = 1; i <= cnt; i++)
			printf("%d\n", ans[i]);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值