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

题意:查询一段区间的第k大值,而且题目保证查询区间是不存在包含关系。 解析:因为不存在包含关系,所以直接离线排序,向右插入元素,向右删除元素。利用treap树实现名次树的功能。 代码
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
const int INF=1e9+;
const int maxn=;
int A[maxn],cnt; //A数组保存数,cnt是节点标号,我是用数组模拟的
struct treap
{
treap* son[]; //左右儿子
int v,s,r;
treap(){ v=s=r=; son[]=son[]=NULL; }
treap(int nv,int nr);
int rk(){ return son[]->s+; } //排名,第几个数
int cmp(int k) //比较,如果相等返回-1,小于返回0,大于1
{
if(k==v) return -;
return k<v?:;
}
void pushup(){ s=son[]->s+son[]->s+; } //更新大小
}null,tr[maxn];
treap::treap(int nv,int nr)
{
v=nv; r=nr;
s=;
son[]=son[]=&null;
}
treap* NewNode(int x,int r)//建新节点
{
tr[cnt]=treap(x,r);
return tr+cnt++;
}
struct splaytree
{
int Size;
treap* root;
splaytree(){ Size=; root=&null; }
void Rotate(treap* &t,int d) //翻转操作
{
treap* p=t->son[d^];
t->son[d^]=p->son[d];
p->son[d]=t;
t->pushup(); //要更新
t=p;
t->pushup();
}
void Insert(treap* &t,int x,int r) //插入
{
if(t==&null) //插入
{
t=NewNode(x,r); //申请新节点
return;
}
int d=t->cmp(x);
if(d==-) d=; //往右边走
Insert(t->son[d],x,r);
if(t->son[d]->r > t->r) Rotate(t,d^); //旋转
t->pushup();
}
void Remove(treap* &t,int x) //删除
{
int d=t->cmp(x);
if(d==-)
{
if(t->son[]==&null) t=t->son[];
else if(t->son[]==&null) t=t->son[];
else
{
int d2=(t->son[]->r > t->son[]->r ? : );
Rotate(t,d2);
Remove(t->son[d2],x);
}
}
else Remove(t->son[d],x);
if(t!=&null) t->pushup();
}
int Query(treap* &t,int kth) //查询
{
if(t==&null||kth<=||kth>t->s) return -;
int a=t->rk();
if(kth==a) return t->v;
else if(kth<a) return Query(t->son[],kth);
else return Query(t->son[],kth-a);
}
};
int N,M;
struct Ques
{
int x,y,k,id;
Ques(int x=,int y=,int k=,int id=):x(x),y(y),k(k),id(id){}
bool operator < (const Ques& t) const
{
if(x!=t.x) return x<t.x;
return y<t.y;
}
}q[maxn];
int ans[maxn];
int main()
{
scanf("%d%d",&N,&M);
splaytree spt; cnt=;
for(int i=;i<=N;i++) scanf("%d",&A[i]);
int x,y,k;
for(int i=;i<M;i++) //输入
{
scanf("%d%d%d",&x,&y,&k);
q[i]=Ques(x,y,k,i);
}
sort(q,q+M); //排序
int f=,r=;
for(int i=;i<M;i++)
{
Ques& t=q[i];
int x=t.x,y=t.y,k=t.k;
for(;f<x;f++) if(f<r) spt.Remove(spt.root,A[f]);
if(r<f) r=f;
for(;r<=y;r++) spt.Insert(spt.root,A[r],rand());
ans[t.id]=spt.Query(spt.root,k); //保存答案
}
for(int i=;i<M;i++) printf("%d\n",ans[i]);
return ;
}

Poj2761-Feed the dogs(伸展树求名次)的更多相关文章

  1. poj 2761 Feed the dogs (treap树)

    /************************************************************* 题目: Feed the dogs(poj 2761) 链接: http: ...

  2. [Poj2761]Feed the dogs(主席树)

    Desciption 题意:求区间第K小(N<=100000) Solution 主席树模板题 Code #include <cstdio> #include <algorit ...

  3. 【莫队算法】【权值分块】poj2104 K-th Number / poj2761 Feed the dogs

    先用莫队算法保证在询问之间转移的复杂度,每次转移都需要进行O(sqrt(m))次插入和删除,权值分块的插入/删除是O(1)的. 然后询问的时候用权值分块查询区间k小值,每次是O(sqrt(n))的. ...

  4. [POJ2761] Feed the dogs (Treap)

    题目链接:http://poj.org/problem?id=2761 题目大意:给你n个数,m次查询,m次查询分别是a,b,k,查询下表从a到b的第k小元素是哪个.这m个区间不会互相包含. Trea ...

  5. [POJ2761]Feed the dogs

    Problem 查询区间第k大,但保证区间不互相包含(可以相交) Solution 只需要对每个区间左端点进行排序,那它们的右端点必定单调递增,不然会出现区间包含的情况. 所以我们暴力对下一个区间加上 ...

  6. POJ 题目2761 Feed the dogs(主席树||划分树)

    Feed the dogs Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 16860   Accepted: 5273 De ...

  7. 划分树---Feed the dogs

    POJ  2761 Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to fee ...

  8. 【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 ...

  9. POJ 2761 Feed the dogs(平衡树or划分树or主席树)

    Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs ...

随机推荐

  1. JavaScript Function 函数深入总结

    整理了JavaScript中函数Function的各种,感觉函数就是一大对象啊,各种知识点都能牵扯进来,不单单是 Function 这个本身原生的引用类型的各种用法,还包含执行环境,作用域,闭包,上下 ...

  2. 关于ionic的一些坑(3)

    (1)对于页面中的input之类的输入框,取值的时候一般采用的是$scope.model=””的方式来取得input输入框的值,然后进行操作,但实际上在ionic里面是取不到的,取值之前必须先把inp ...

  3. oracle 同样数据删除(仅仅留一条)

    DELETE FROM reg_user t1 WHERE user_name='9527008' and rowid > ( SELECT min(rowid) FROM location t ...

  4. 破解Veeam过程

    1)运行Veeam_Backup_Setup.exe,但是不要继续下一步: 2)进入到%temp%\IXP000.TMP目录,例如windows xp sp3环境默认为C:\Documents and ...

  5. RAC 的一些概念性和原理性的知识(转)

    一 集群环境下的一些特殊问题 1.1 并发控制 在集群环境中, 关键数据通常是共享存放的,比如放在共享磁盘上. 而各个节点的对数据有相同的访问权限, 这时就必须有某种机制能够控制节点对数据的访问. O ...

  6. shell常用命令的用法

    1. 如何把 /etc/passwd 中用户uid 大于500 的行给打印出来?awk -F ':' '$3>500' /etc/passwd 2. awk中 NR,NF两个变量表示什么含义?N ...

  7. Docker的简单认知

    Docker images: docker image是一个只读打模板,用来创建Docker 容器 Docker Registers 互联网上存储images的地方 Docker containers ...

  8. S2SH简单介绍和理解

    struts2简介 Struts2是由WebWork基础上发展起来的,与struts1比较,选用struts2的理由是:①Struts1要求Action类继承一个抽象基类,而Struts2Action ...

  9. VC防止程序被多次运行 互斥体方法

    BOOL CXXXApp::InitInstance() //函数内添加代码 HANDLE hMutex=CreateMutex(NULL,TRUE,"test"); // 用于检 ...

  10. lunix安装jdk(rpm格式)

    1.下载后,首先把jdk-7u3-linux-x64.rpm复制到/usr/local/src#cp jdk-7u3-linux-x64.rpm /usr/local/src/2.给所有用户添加可执行 ...