POJ 2761 Feed the dogs

静态区间第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

POJ Monthly--2006.02.26,zgl & twb

[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

时间: 2024-11-10 10:51:05

POJ 2761 Feed the dogs的相关文章

Splay树(区间第k小)——POJ 2761 Feed the dogs

对应POJ题目:点击打开链接 Feed the dogs Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 16655   Accepted: 5203 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 Win

POJ 2761 Feed the dogs (主席树)(K-th 值)

                                                            Feed the dogs Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 20634   Accepted: 6494 Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed

POJ 2761 Feed the dogs(树状数组求区间第K大)

题目链接: 戳我 题目大意:Jiajia要为宠物狗,宠物狗按成一排站好(1 < i <= n),第 i 只狗的喜欢程度是 a[i], 之后他会先喂某个区间内第k个 即 n 个数, m个询问,接着是 n个数 接下来 m 行,每行是 l r k即 l 到 r 这个区间第 k 小的数,每个询问输出一个答案,即 a[i] 求区间第k大有很多算法, 详见此博客 [数据结构练习] 求区间第K大数的几种方法 我用的树状数组解法,来自 树状数组从前往后求和,用来解第k大(或小)的数 poj 2985 The

POJ 2761 Feed the dogs 基础Treap

同样是插入和寻找第k大,这里因为区间不存在包含的情况,所以可以现将区间排序然后直接搞就行了.如果存在包含的情况那就只能上主席树或者是莫队算法来搞了. #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <cstdlib> using namespace std; struct Node { Node *ch[2]; int r

平衡树---Feed the dogs

POJ  2761 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

POJ2761 Feed the dogs

Time Limit: 6000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu 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 sp

POJ 2761(求区间第k小值)

Feed the dogs Time Limit: 6000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status 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 no

【POJ2761】【区间第k大】Feed the dogs(吐槽)

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,

【poj2761】 Feed the dogs

http://poj.org/problem?id=2761 (题目链接) 题意:求区间第K大. Solution  和poj2104一模一样. 主席树代码: // poj2761 #include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<cmath> #define LL long long