poj 2761 树状数组

树状数组主要解决的问题:1)区间求和类问题。2)区间操作问题(比如n次操作不确定区间加或减某个值,求某个位置的值)。3)第K值问题。4)逆序对问题。

Feed the dogs
Time Limit: 6000MS Memory Limit: 65536K
Total Submissions: 12798 Accepted: 3786

Description

Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs every day for Wind. Jiajia loves Wind, but not the dogs, so Jiajia use a special way to feed the dogs. At lunchtime, the dogs will stand on one line, numbered from 1 to n, the leftmost one is 1, the second one is 2, and so on. In each feeding, Jiajia choose an inteval[i,j], select the k-th pretty dog to feed. Of course Jiajia has his own way of deciding the pretty value of each dog. It should be noted that Jiajia do not want to feed any position too much, because it may cause some death of dogs. If so, Wind will be angry and the aftereffect will be serious. Hence any feeding inteval will not contain another completely, though the intervals may intersect with each other. 

Your task is to help Jiajia calculate which dog ate the food after each feeding. 

Input

The first line contains n and m, indicates the number of dogs and the number of feedings. 

The second line contains n integers, describe the pretty value of each dog from left to right. You should notice that the dog with lower pretty value is prettier. 

Each of following m lines contain three integer i,j,k, it means that Jiajia feed the k-th pretty dog in this feeding. 

You can assume that n<100001 and m<50001. 

Output

Output file has m lines. The i-th line should contain the pretty value of the dog who got the food in the i-th feeding.

Sample Input

7 2
1 5 2 6 3 7 4
1 5 3
2 7 1

Sample Output

3
2
分析:大题意思就是把狗编号,并付给他漂亮值(由于这个值是相对的所以可能是非正整数,所以我们要离散化这个值)。然后找一个区间【s,e】中的第K大值,这题其实是万能题,很多数据都适用这题,比如平衡树, 线段树,树状数组,划分树等等,我们就用比较常规的树状数组(树状数组相对于线段树结构要简单耗时要少所以我们选择树状数组)。
树状数组组成主要有四个函数
lowbit()求2^k;
add()添加数据:
sum()求和;
find_thk()求第K大值;
怎么进行离散化呢:比如数组a[4]值分别为7 5555555 1 3我们了解如果不离散化我们就需要开5555555个数组的C这显然是浪费的并且如果a有的值为负数也是不行的。所以我们就吧a排序用每个值的下标代替这个值,1 3 7 5555555 所以a[1]=3 a[2]= 4 a[3] = 1 a[4] = 2;这样我们C只需开五个就ok了
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int m, n, k;
int c[100005], f[100005], t_k[50005];
struct dog
{
	int v;
	int index;
}d[100005];
struct qe
{
	int s;
	int e;
	int i;
	int k;
}q[50005];
bool cmp1(const qe &q1, const qe &q2)
{
	return q1.s < q2.s;
}
bool cmp(const dog &d1, const dog &d2)
{
	return d1.v < d2.v;
}
int lowbit(int x)
{
	return x&(-x);
}
void add(int index, int d)
{
	while (index<=n)
	{
		c[index]+=d;
		index += lowbit(index);
	}
}
int find_kth()
{
	int ans = 0, cnt = 0 ;
	for (int i = 20; i >= 0; i--)
	{
		ans += (1<<i);
		if (ans > n || c[ans]+cnt>=k)
		{
			ans -=(1<<i);
		}
		else
		{
			cnt += c[ans];
		}
	}
	return ans+1;
}
void work()
{
	int i, j, mx, mi;
	for ( i = q[0].s; i<=q[0].e; i++ )
	{
	//	cout<<f[i]<<endl;
		add(f[i],1);
	}
//	cout<<q[0].s<<q[0].e<<endl;
	k = q[0].k;
	t_k[q[0].i] = find_kth();
	for ( i = 1; i< m; i++)
	{
		int wa, wb;
		if (q[i-1].e < q[i].s)
		{
			for (j = q[i-1].s; j< q[i-1].e; j++)
			{
				add(f[j], -1);
			}
			wa = q[i].s, wb = q[i].e;
		} 
		else
		{
			for (j = q[i-1].s; j< q[i].s;j++)
			{
				add(f[j],-1);
			}
			wa = q[i-1].e+1;
			wb = q[i].e;
		}
		for (j = wa; j<= wb; j++)
		{
			add(f[j],1);
		}
		k = q[i].k;
		t_k[q[i].i] = find_kth();
	}
}
int main()
{
	int i;
	scanf("%d %d",&n, &m);
	for ( i = 1; i<= n; i++)
	{
		scanf("%d",&d[i].v);
		d[i].index= i;
	}
	sort(d+1,d+n+1,cmp);//开始离散化数据
	for (i =1; i <= n; i++)
	{
		f[d[i].index] = i;
	}
	for (i = 0; i< m; i++)
	{
		scanf("%d %d %d",&q[i].s, &q[i].e, &q[i].k);
		q[i].i = i;
	}
	sort(q, q+m, cmp1);
	work();
	for (i = 0; i< m; i++)
	{
		printf("%d\n", d[t_k[i]].v);
	//	cout<<d[t_k[i]].v<<endl;
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值