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. 最简单的内核模块hello world

    [root@rt2m09617.sqa.tbc /home/ahao.mah/main] #cat hello.c // Defining __KERNEL__ and MODULE allows u ...

  2. warning: the `gets' function is dangerous and should not be used.(转)

    今天在LINUX下编译C程序时,出现了:warning: the `gets' function is dangerous and should not be used. 这个warning. 百度之 ...

  3. _js day9

  4. SVG 路径(path)

    本文转自:https://developer.mozilla.org/zh-CN/docs/Web/SVG/Tutorial/Paths <path>元素是SVG基本形状中最强大的一个,它 ...

  5. NFinal 视图—模板

    创建模板 1.新建Header.ascx用户控件,此控件就是模板,修改内容如下: <%@ Control Language="C#" AutoEventWireup=&quo ...

  6. Sys.WebForms.PageRequestManagerParserErrorException:无法分析从服务器收到的消息

    我引起此原因的功能如下: 在aspx页面添加按钮 JS方法: function downPPT() { $("#Btn_DownPPT").click();    } <bo ...

  7. VC++函数(win32_exe)

    1.windows输出,以对话框的方式. int MessageBox( HWND hWnd, // handle to owner window LPCTSTR lpText, // text in ...

  8. python进行base64编解码

    [转] 直接上代码 import base64 fin = open(r"D:\2.zip", "rb") fout = open(r"D:\2.x. ...

  9. JSON基础知识

    1.什么是json •        JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) •        JSON 是轻量级的文本数据交换格式 ...

  10. 02—从Cocos2DX视角看游戏组成

    Cocos2DX引擎按照层次逻辑将游戏元素细分为:导演CCDirector.场景CCScene.图层CCLayer.精灵CCSprite.在上面篇中我们已经知道除导演CCDirector外都继承了CC ...