程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> poj2761 Feed the dogs

poj2761 Feed the dogs

編輯:關於C++

 

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

POJ Monthly--2006.02.26,zgl & twb

 

 

 

Treap模板題

這道題是支持離線的,所以我們可以將區間按左端點從小到大排序,每次相應加點和刪點在求出Treap中第k小的數。

Treap模板寫掛了...整整調了3個小時才找出錯...最後發現是在刪點時沒有將節點數減1...QAQ


 

 

 

#include
#include
#include
#include
#include
#include
#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
#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].rnd1) 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=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.ra[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]);
}


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved