给n个数,然后m个询问,询问任意区间的第k小的数,特别的,任意两个区间不存在包含关系,

也就是说,将所有的询问按L排序之后, 对于i<j ,   Li < Lj 且 Ri < Rj

所以只要每次查询的时候,treap都是对应区间[Li,Ri] ,那么查找第k小就很简单了

 #include <iostream>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <algorithm>
using namespace std;
const int INF = <<;
const int N = + ;
struct Treap
{
int ch[];
int fix,key;
int same,cnt;
}treap[N*];
struct Node
{
int l, r,k,id;
bool operator<(const Node&rhs)const
{
return l < rhs.l;
}
}q[N*];
int tot;
int val[N];
int ans[N*];
void maintain(int o)
{
int lCnt = treap[o].ch[] == ? : treap[treap[o].ch[]].cnt;
int rCnt = treap[o].ch[] == ? : treap[treap[o].ch[]].cnt;
treap[o].cnt = treap[o].same + lCnt + rCnt;
}
void rotate(int &o, int d)
{
int t = treap[o].ch[d^];
treap[o].ch[d^] = treap[t].ch[d];
treap[t].ch[d] = o;
maintain(o);
maintain(t);
o = t;
}
void insert(int &o, int tkey)
{
if(o==)
{
o = ++tot;
if(tot>=N*) while(true);;
treap[o].ch[] = treap[o].ch[] = ;
treap[o].cnt = treap[o].same = ;
treap[o].key = tkey;
treap[o].fix = rand();
maintain(o);
}
else if(tkey < treap[o].key)
{
insert(treap[o].ch[],tkey);
maintain(o);
if(treap[treap[o].ch[]].fix > treap[o].fix)
rotate(o,);
}
else if(tkey > treap[o].key)
{
insert(treap[o].ch[],tkey);
maintain(o);
if(treap[treap[o].ch[]].fix > treap[o].fix)
rotate(o,);
}
else
{
treap[o].same++;
maintain(o);
} }
void remove(int &o, int tkey)
{
if(tkey < treap[o].key)
{
remove(treap[o].ch[],tkey);
}
else if(tkey > treap[o].key)
{
remove(treap[o].ch[],tkey);
}
else
{
if(treap[o].same!=)
treap[o].same--;
else if(treap[o].ch[]== && treap[o].ch[]==)
o = ;
else if(treap[o].ch[]==)
{
rotate(o,);
remove(treap[o].ch[],tkey);
}
else if(treap[o].ch[]==)
{
rotate(o,);
remove(treap[o].ch[],tkey);
}
else if(treap[treap[o].ch[]].fix <= treap[treap[o].ch[]].fix)
{
rotate(o,);
remove(treap[o].ch[],tkey);
}
else
{
rotate(o,);
remove(treap[o].ch[],tkey);
}
}
maintain(o);
} int query(int o, int k)
{
while(o)
{
int cnt = treap[o].ch[]== ? : treap[treap[o].ch[]].cnt;
if(cnt>=k)
o = treap[o].ch[];
else if(cnt<k && k<=cnt+treap[o].same)
return treap[o].key;
else
{ k -= cnt + treap[o].same;
o = treap[o].ch[];
}
}
}
int main()
{
//freopen("d:/in.txt","r",stdin);
int n,m;
srand(time());
while(scanf("%d%d",&n,&m)!=EOF)
{ for(int i=;i<=n;++i)
{
scanf("%d",&val[i]);
}
tot = ;
for(int i=;i<m;++i)
{
scanf("%d%d%d",&q[i].l,&q[i].r,&q[i].k);
q[i].id = i;
}
sort(q,q+m);
int root = ;
int L=,R=;//初始在Treap中的节点所属区间[L,R),注意半闭半开区间
for(int i=;i<m;i++)
{
while(L<q[i].l)
{
if(L<R) remove(root,val[L]);
L++;
}
if(R<L) R=L;
while(R<=q[i].r)
{
insert(root,val[R]);
R++;
}
ans[q[i].id]=query(root,q[i].k);
}
for(int i=;i<m;i++) printf("%d\n",ans[i]);
}
return ;
}

poj2761(treap入门)的更多相关文章

  1. Treap入门(转自NOCOW)

    Treap 来自NOCOW Treap,就是有另一个随机数满足堆的性质的二叉搜索树,其结构相当于以随机顺序插入的二叉搜索树.其基本操作的期望复杂度为O(log n). 其特点是实现简单,效率高于伸展树 ...

  2. treap入门

    这几天刚学了treap,听起来还行,就是调题调到恶心了…… 就以这道题作为板子吧(”你本来也就做了一道题!”) https://www.luogu.org/problemnew/show/P3369 ...

  3. 【bzoj3173-最长上升子序列-一题两解】

    这道题不就是简单的DP吗,BZOJ在水我!不,你是错的. ·本题特点:       不断向不同位置插入数字(按数字1,2,3,4,5,6……),需要求出每一次插入后的最长上升子序列. ·分析      ...

  4. 入门平衡树: Treap

    入门平衡树:\(treap\) 前言: 如有任何错误和其他问题,请联系我 微信/QQ同号:615863087 前置知识: 二叉树基础知识,即简单的图论知识. 初识\(BST\): \(BST\)是\( ...

  5. [POJ2761] Feed the dogs (Treap)

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

  6. 【POJ2761】【fhq treap】A Simple Problem with Integers

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  7. [转载]无旋treap:从好奇到入门(例题:bzoj3224 普通平衡树)

    转载自ZZH大佬,原文:http://www.cnblogs.com/LadyLex/p/7182491.html 今天我们来学习一种新的数据结构:无旋treap.它和splay一样支持区间操作,和t ...

  8. [您有新的未分配科技点]无旋treap:从好奇到入门(例题:bzoj3224 普通平衡树)

    今天我们来学习一种新的数据结构:无旋treap.它和splay一样支持区间操作,和treap一样简单易懂,同时还支持可持久化. 无旋treap的节点定义和treap一样,都要同时满足树性质和堆性质,我 ...

  9. 快速入门Treap(代码实现)

    学习数据结构对我来说真的相当困难,网上讲\(Treap\)的我也看不太懂,前前后后花了大概六天才把\(Treap\)学会.为了避免再次忘记,这里我整理一下\(Treap\)的基础知识和模板. 阅读此文 ...

随机推荐

  1. Hadoop--序列化

    序列化: 对象的序列化用于将一个对象编码成字节流,以及从字节流中重新构建对象. 将一个对象编码成一个字节流称为序列化该对象. 序列化三种主要的用途: 1.作为一种持久化格式. 2.作为一种通信的数据格 ...

  2. qt button以及label实现不规则图形(五种方法:使用QSS,设置Mask图片,自己画)

    1.方法1:准备一张边界是透明的不规则图形 QPushButton * pbtn = new QPushButton;    pbtn->setStyleSheet("QPushBut ...

  3. Windows消息队列

    一 Windows中有一个系统消息队列,对于每一个正在执行的Windows应用程序,系统为其建立一个“消息队列”,即应用程序队列,用来存放该程序可能 创建的各种窗口的消息.应用程序中含有一段称作“消息 ...

  4. CSS 控制应为Html页面高度导致抖动的问题

    在CSS中添加如下代码: html,body{ overflow-y:scroll;} html,body{ overflow:scroll; min-height:101%;} html{ over ...

  5. 程序启动读取和关闭时保存应用程序设置(QSettings)

    保存应用程序设置(QSettings)1. QSettings 类 QSettings 提供保存应用程序当前设置的接口,可以方便地保存程序的状态,例如窗口大小和位置,选项的选中状态等等.在 Windo ...

  6. 搜索引擎爬虫蜘蛛的USERAGENT大全

    搜索引擎爬虫蜘蛛的USERAGENT大全 搜索引擎爬虫蜘蛛的USERAGENT收集,方便制作采集的朋友.   百度爬虫 * Baiduspider+(+http://www.baidu.com/sea ...

  7. DMP文件的生成和使用

    1.生成dmp的程序 #include  <dbghelp.h> #pragma comment(lib,  "dbghelp.lib") //设置异常处理回调函数Se ...

  8. [置顶] 页面缓存,cache,设置缓存过期时间,OutputCache

    页面缓存 方法一: protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //缓存有数据 if (Cach ...

  9. Transparency Tutorial with C# - Part 2

    Download Compositing Mode demo project - 24 Kb Download Compositing Mode source - 26 Kb Download Com ...

  10. FMCG行业是什么行业?

    FMCG行业是什么行业?_百度知道 FMCG行业是什么行业?    2008-05-21 20:03 搏浪峰 | 分类:创业投资 | 浏览13089次 在网上看到搜狐公司招聘“FMCG行业(高级)客户 ...