给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. 出现异常 child-&gt;m_pParent == 0

    在cocos2d-x中,能够用CCNode类 自己new一个节点(或是用CCnode::node().create()),当将它作为其它若干item(如button项.sprite项.image项)的 ...

  2. XMPP入门

    本人原创,很多其它xmpp知识.ios知识.其它分享知识见:我的个人博客 简单介绍: 1.XMPP是可扩展消息与存在协议,主要用于im. 2.他是一种类似于http协议的传输数据协议.过程如:&quo ...

  3. ADO异步查询显示进度条

    一般,ADO都是以同步的方式来处理数据.这就是说,当ADO开始处理数据后,应用程序必须等到ADO处理完毕之后才可以继续执行.但是除了同步执行方式之外,ADO也提供了异步执行的方式,允许当ADO处理时, ...

  4. QT插件开发方式(没看懂)

    创建一个QT的库项目,删除自动生成的.h和.cpp文件,添加一个接口定义.h文件和一个接口实现类(一个.h一个.cpp).代码如下: 1.接口文件源码 #ifndef PLUGININTERFACE_ ...

  5. InputStream中read()与read(byte[] b)(转)

    read()与read(byte[] b)这两个方法在抽象类InputStream中前者是作为抽象方法存在的,后者不是,JDK API中是这样描述两者的: 1:read() : 从输入流中读取数据的下 ...

  6. jQuery 自学笔记—9 常见特效 (终章)

    隐藏.显示.切换,滑动,淡入淡出,以及动画 效果演示 点击这里,隐藏/显示面板 一寸光阴一寸金,因此,我们为您提供快捷易懂的学习内容. 在这里,您可以通过一种易懂的便利的模式获得您需要的任何知识. 实 ...

  7. ifconfig 源码

    贴一下ifconfig的源码(它属于net-tool软件包),以备平时查看网络信息的配置. /* * ifconfig This file contains an implementation of ...

  8. 使用ffmpeg 对视频截图,和视频转换格式

    //执行CMD命令方法 public static void CmdProcess(string command)//调用CMD        {            //实例化一个进程类      ...

  9. Java并发编程--Fork/Join框架使用

    上篇博客我们介绍了通过CyclicBarrier使线程同步,可是上述方法存在一个问题,那就是假设一个大任务跑了2个线程去完毕.假设线程2耗时比线程1多2倍.线程1完毕后必须等待线程2完毕.等待的过程线 ...

  10. jquery实现鼠标拖动

    <html>   <head>   <script type='text/javascript' src='js/jquery-1.5.1.js'></scr ...