关闭

poj2761 Feed the dogs

标签: poj
1060人阅读 评论(0) 收藏 举报
分类:

Feed the dogs
Time Limit: 6000MS   Memory Limit: 65536K
Total Submissions: 17095   Accepted: 5349

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




Treap模板题

这道题是支持离线的,所以我们可以将区间按左端点从小到大排序,每次相应加点和删点在求出Treap中第k小的数。

Treap模板写挂了...整整调了3个小时才找出错...最后发现是在删点时没有将节点数减1...QAQ




#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdlib>
#define F(i,j,n) for(int i=(j);i<=(n);i++)
#define D(i,j,n) for(int i=(j);i>=(n);i--)
#define LL long long
#define pa pair<int,int>
#define MAXN 100005
using namespace std;
int n,m,root=0,tot=0,b[MAXN],ans[MAXN];
struct tree_type
{
	int l,r,s,rnd,v,w;
}t[MAXN];
struct query_type
{
	int l,r,x,num;
}a[MAXN];
int read()
{
	int ret=0,flag=1;
	char ch=getchar();
	while (ch<'0'||ch>'9')
	{
		if (ch=='-') flag=-1;
		ch=getchar();
	}
	while (ch>='0'&&ch<='9')
	{
		ret=ret*10+ch-'0';
		ch=getchar();
	}
	return ret*flag;
}
void rturn(int &k)
{
	int tmp=t[k].l;
	t[k].l=t[tmp].r;
	t[tmp].r=k;
	t[tmp].s=t[k].s;
	t[k].s=t[t[k].l].s+t[t[k].r].s+t[k].w;
	k=tmp;
}
void lturn(int &k)
{
	int tmp=t[k].r;
	t[k].r=t[tmp].l;
	t[tmp].l=k;
	t[tmp].s=t[k].s;
	t[k].s=t[t[k].l].s+t[t[k].r].s+t[k].w;
	k=tmp;
}
void ins(int &k,int x)
{
	if (!k)
	{
		k=++tot;
		t[k].v=x;
		t[k].s=t[k].w=1;
		t[k].l=t[k].r=0;
		t[k].rnd=rand();
		return;
	}
	t[k].s++;
	if (t[k].v==x) t[k].w++;
	else if (t[k].v>x)
	{
		ins(t[k].l,x);
		if (t[t[k].l].rnd<t[k].rnd) rturn(k);
	}
	else
	{
		ins(t[k].r,x);
		if (t[t[k].r].rnd<t[k].rnd) lturn(k);
	}
}
void del(int &k,int x)
{
	if (t[k].v==x)
	{
		if (t[k].w>1) t[k].w--,t[k].s--;
		else if (!t[k].l||!t[k].r) k=t[k].l+t[k].r;
		else if (t[t[k].l].rnd<t[t[k].r].rnd)
		{
			rturn(k);
			del(k,x);
		}
		else
		{
			lturn(k);
			del(k,x);
		}
		return;
	}
	t[k].s--;
	if (x<t[k].v) del(t[k].l,x);
	else del(t[k].r,x);
}
int getans(int k,int x)
{
	int ln=t[t[k].l].s;
	if (ln<x&&ln+t[k].w>=x) return t[k].v;
	else if (x<=ln) return getans(t[k].l,x);
	else return getans(t[k].r,x-ln-t[k].w);
}
bool cmp(query_type x,query_type y)
{
	return x.l==y.l?x.r<y.r:x.l<y.l;
}
int main()
{
	t[0].s=0;
	a[0].l=1;a[0].r=0;
	n=read();m=read();
	F(i,1,n) b[i]=read();
	F(i,1,m)
	{
		a[i].l=read();
		a[i].r=read();
		a[i].x=read();
		a[i].num=i;
	}
	sort(a+1,a+m+1,cmp);
	F(i,1,m)
	{
		if (a[i].l>a[i-1].r)
		{
			root=0;
			F(j,a[i].l,a[i].r) ins(root,b[j]);
		}
		else
		{
			F(j,a[i-1].l,a[i].l-1) del(root,b[j]);
			F(j,a[i-1].r+1,a[i].r) ins(root,b[j]);
		}
		ans[a[i].num]=getans(root,a[i].x);
	}
	F(i,1,m) printf("%d\n",ans[i]);
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:702848次
    • 积分:11189
    • 等级:
    • 排名:第1579名
    • 原创:417篇
    • 转载:3篇
    • 译文:0篇
    • 评论:44条
    云中谁寄锦书来