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. C++基础回顾1(数据类型, 控制语句, 数组)

    最近两天打开本科学校的C++教材,快速回顾了一下C++方面的内容.虽然书本内容比较基础,但是还是有些知识点值得自己强化记忆.分几篇文章,加上自己的理解记录如下. 先回顾面向过程的部分. C++数据类型 ...

  2. 第24讲 UI_布局 之帧布局 表格布局 绝对布局

    第24讲 UI_布局 之帧布局 表格布局 绝对布局 3. FrameLayout(帧布局) 帧布局是从屏幕的左上角(0,0)坐标开始布局,多个组件层叠排序,后一个组件总会将前一个组件所覆盖,除非最后一 ...

  3. (转)A drop-in universal solution for moving text fields out of the way of the keyboard

    There are a hundred and one proposed solutions out there for how to move UITextField andUITextView o ...

  4. IOS详解TableView——选项抽屉(天猫商品列表)

    在之前的有篇文章讲述了利用HeaderView来写类似QQ好友列表的表视图. 这里写的天猫抽屉其实也可以用该方法实现,具体到细节每个人也有所不同.这里采用的是点击cell对cell进行运动处理以展开“ ...

  5. Zedboard甲诊opencv图像处理(二)

    通过前面的努力已经得到了n个轮廓了,现在要把最终的轮廓确定下来 ,然后进行特征提取. 先深入分析下轮廓和处理轮廓的方法:http://blog.csdn.net/hitwengqi/article/d ...

  6. python-操作缓存

    参考王智刚同学博客 操作Mmecached 1. 安装API python -m pip install python-memcached 2. 启动memcached memcached -d -u ...

  7. extjs folder is lost解决方法 和 FineUI主题切换时 iframe内的内容主题不变的解决方法

    错误原因:extjs包和FineUI版本不一致 或者是 webconfig配置中 没有设置为任何人可访问  解放方法下载和FineUI版本相同的extjs包就ok了 解决方法:FineUI主题切换时 ...

  8. Oracle内链接+外连接详解

    inner join(内连接) 内连接也称为等同连接,返回的结果集是两个表中所有相匹配的数据,而舍弃不匹配的数据.也就是说,在这种查询中,DBMS只返回来自源表中的相关的行,即查询的结果表包含的两源表 ...

  9. HashMap陷入死循环的例子

    //使用这个例子可以模拟HashMap陷入死循环的效果,可能需要执行多次才会出现. 1 package com.hanzi; import java.util.HashMap; public clas ...

  10. 如何在Silverlight应用程序中获取ASP.NET页面参数

    asp.net Silverlight应用程序中获取载体aspx页面参数 有时候SL应用中需要使用由aspx页面中传递过来的参数值,此时通常有两种方法获取 1. 使用InitParameters属性, ...