码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2761 Feed the dogs

时间:2014-07-29 14:25:38      阅读:365      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   os   strong   io   for   

静态区间第K大,主席树。。。。


Feed the dogs
Time Limit: 6000MS   Memory Limit: 65536K
Total Submissions: 15491   Accepted: 4780

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

Source

[Submit]   [Go Back]   [Status]   [Discuss]


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn=110000;

int n,m,q;
int a[maxn],t[maxn];
int tot,T[maxn],lson[maxn*30],rson[maxn*30],c[maxn*30];

void hash_init()
{
	sort(t+1,t+1+n);
	m=unique(t+1,t+1+n)-t;	
}

int hash(int x)
{
	return lower_bound(t+1,t+1+m,x)-t;
}

int build(int l,int r)
{
	int root=tot++,temp=root;
	c[root]=0;
	if(l!=r)
	{
		int mid=(l+r)/2;
		lson[root]=build(l,mid);
		rson[root]=build(mid+1,r);
	}
	return temp;
}

int update(int root,int pos,int val)
{
	int newroot=tot++,temp=newroot;	
	c[newroot]=c[root]+val;
	int l=1,r=m;
	while(l<r)
	{
		int mid=(l+r)/2;
		if(pos<=mid)
		{
			lson[newroot]=tot++; rson[newroot]=rson[root];
			root=lson[root]; newroot=lson[newroot];
			r=mid;
		}
		else
		{
			rson[newroot]=tot++; lson[newroot]=lson[root];
			root=rson[root]; newroot=rson[newroot];	
			l=mid+1;
		}
		c[newroot]=c[root]+val;
	}
	return temp;
}

int query(int left_root,int right_root,int x)
{
	int l=1,r=m;
	while(l<r)	
	{
		int mid=(l+r)/2;
		int tt=c[lson[left_root]]-c[lson[right_root]];
		if(tt>=x)
		{
			left_root=lson[left_root];
			right_root=lson[right_root];
			r=mid;
		}
		else
		{
			x-=tt;
			left_root=rson[left_root];
			right_root=rson[right_root];
			l=mid+1;
		}
	}
	return l;
}

int main()
{
	while(scanf("%d%d",&n,&q)!=EOF)
	{
		memset(c,0,sizeof(c)); tot=0;
		for(int i=1;i<=n;i++)	
		{
			scanf("%d",a+i);
			t[i]=a[i];
		}
		hash_init();
		T[n+1]=build(1,m);
		for(int i=n;i;i--)
		{
			T[i]=update(T[i+1],hash(a[i]),1);
		}
		while(q--)
		{
			int a,b,c;
			scanf("%d%d%d",&a,&b,&c);
			printf("%d\n",t[query(T[a],T[b+1],c)]);
		}
	}
	return 0;
}



POJ 2761 Feed the dogs,布布扣,bubuko.com

POJ 2761 Feed the dogs

标签:des   style   http   color   os   strong   io   for   

原文地址:http://blog.csdn.net/ck_boss/article/details/38237959

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!