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. 一个跨域请求的XSS续

    之前讨论过,在解决post跨域请求时,采用iframe+本域代理页的形式,兼容性(当然是包括IE6啦)是最好的.上次提到,代理页面的作用是:执行本域下的回调函数.就是这个原因,给XSS带来了便利.详细 ...

  2. scp 对拷文件夹 和 文件夹下的所有文件 对拷文件并重命名

    对拷文件夹 (包括文件夹本身) scp -r   /home/wwwroot/www/charts/util root@192.168.1.65:/home/wwwroot/limesurvey_ba ...

  3. c语言验证哥德巴赫猜想(从4开始 一个偶数由两个质数之和)

    #include <stdio.h> #include <stdlib.h> #include <math.h> int isit(int num) { int i ...

  4. PYCHARM配置文件如何导入

    请到此下载,如下: http://share.weiyun.com/c00040649e85c4ab1dbc47a0fe9a0a7a 或者:http://files.cnblogs.com/files ...

  5. SecureCRT 安装及初始化配置

    安装 SecureCRT 7.3.4 安装以及破解方法 SecureCRT 6.5.0 汉化解压版 初始化配置 这里配置以SecureCRT 6.5.0 汉化解压版为例 1.调整SecureCRT终端 ...

  6. 【leetcode】Merge k Sorted Lists(按大小顺序连接k个链表)

    题目:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...

  7. [Javascript] Regex: '$`', '$&', '$''

    var input = "foobar"; var result = input.replace('bar', '$`'); // $`: replace 'bar' with e ...

  8. 为iPhone6 设计自适应布局(一)

    译者的话:本文是自适应布局的巩固篇,所以对布局约束的添加操作步骤等没有详细的说明.如果看着吃力的话请先移步Swift自适应布局(Adaptive Layout)教程. Apple从iOS6加入了Aut ...

  9. c++11 NULL、0、nullptr

      C的NULL 在C语言中,我们使用NULL表示空指针,也就是我们可以写如下代码: int *i = NULL;foo_t *f = NULL; 实际上在C语言中,NULL通常被定义为如下: #de ...

  10. Linux的用户和组

    1. 查看配置文件/etc/shadow第一行中root账号的第三个字段(以':'分隔)中的数字,请算一下这个数字是怎么来的?距离1970年1月1日到上次更改密码的时间的间隔天数.例如root密码日期 ...