poj2761(treap入门)
给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入门)的更多相关文章
- Treap入门(转自NOCOW)
Treap 来自NOCOW Treap,就是有另一个随机数满足堆的性质的二叉搜索树,其结构相当于以随机顺序插入的二叉搜索树.其基本操作的期望复杂度为O(log n). 其特点是实现简单,效率高于伸展树 ...
- treap入门
这几天刚学了treap,听起来还行,就是调题调到恶心了…… 就以这道题作为板子吧(”你本来也就做了一道题!”) https://www.luogu.org/problemnew/show/P3369 ...
- 【bzoj3173-最长上升子序列-一题两解】
这道题不就是简单的DP吗,BZOJ在水我!不,你是错的. ·本题特点: 不断向不同位置插入数字(按数字1,2,3,4,5,6……),需要求出每一次插入后的最长上升子序列. ·分析 ...
- 入门平衡树: Treap
入门平衡树:\(treap\) 前言: 如有任何错误和其他问题,请联系我 微信/QQ同号:615863087 前置知识: 二叉树基础知识,即简单的图论知识. 初识\(BST\): \(BST\)是\( ...
- [POJ2761] Feed the dogs (Treap)
题目链接:http://poj.org/problem?id=2761 题目大意:给你n个数,m次查询,m次查询分别是a,b,k,查询下表从a到b的第k小元素是哪个.这m个区间不会互相包含. Trea ...
- 【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 ...
- [转载]无旋treap:从好奇到入门(例题:bzoj3224 普通平衡树)
转载自ZZH大佬,原文:http://www.cnblogs.com/LadyLex/p/7182491.html 今天我们来学习一种新的数据结构:无旋treap.它和splay一样支持区间操作,和t ...
- [您有新的未分配科技点]无旋treap:从好奇到入门(例题:bzoj3224 普通平衡树)
今天我们来学习一种新的数据结构:无旋treap.它和splay一样支持区间操作,和treap一样简单易懂,同时还支持可持久化. 无旋treap的节点定义和treap一样,都要同时满足树性质和堆性质,我 ...
- 快速入门Treap(代码实现)
学习数据结构对我来说真的相当困难,网上讲\(Treap\)的我也看不太懂,前前后后花了大概六天才把\(Treap\)学会.为了避免再次忘记,这里我整理一下\(Treap\)的基础知识和模板. 阅读此文 ...
随机推荐
- POJ 2186 Popular Cows (强联通)
id=2186">http://poj.org/problem? id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 655 ...
- 暂停和屏蔽右键网页中的Flash
如何暂停网页中的Flash?原理很简单,就是屏蔽Flash的消息即可.屏蔽右键也可以通过此方法 直接贴代码吧,加了注释,很容易就能懂了 新建工程,加一个WebBrowser,再加两个按钮.Flash ...
- Java线程并发中常见的锁--自旋锁 偏向锁
随着互联网的蓬勃发展,越来越多的互联网企业面临着用户量膨胀而带来的并发安全问题.本文着重介绍了在java并发中常见的几种锁机制. 1.偏向锁 偏向锁是JDK1.6提出来的一种锁优化的机制.其核心的思想 ...
- PHP从数据库获取的下拉树
<?php include "config.php"; include "mysql.php"; $db = new Mysql('test'); //几 ...
- 特么的. 最终把 amobbs 的站长阿莫(莫进明)给回骂了一顿.
起因: 假设你居住的地方,要上马PX等高污染的项目,你会怎么做. 鼓动别人上街暴力示威与军警对抗. 自己待在家里支持怂恿. 这样的人真心猥琐! 鉴于他常常私自改动帖子, 在此截图留存. 真特么没劲. ...
- [计算机基础]关于实体( Entity )和模型( Model )
实体与模型的浅析 在日常开发过程中经常看到Entity,Model,DataModel,它们之间到底有什么异同?下面是我个人的一些理解. 一.Entity,Model,它们是什么? 维基百科描述: 实 ...
- PageHeap,调试Heap问题的工具
<Windows用户态程序高效排错>第二章主要介绍用户态调试相关的知识和工具.本文主要讲了PageHeap,调试Heap问题的工具. AD:51CTO学院:IT精品课程在线看! 2.4.2 ...
- PhoneGap-----Contacts
Everything in the code!!! <!DOCTYPE html> <html> <head> <title>Contact Examp ...
- js错误 SyntaxError: missing : after property id
在用jquery的post方法时 $.post('adminCheckTpmisPlans.do',{'test',str},function(f){ ... }) 报如下错误 SyntaxError ...
- DataReader和DataSet的区别以及使用
DataReader和DataSet这两个对象都可以将检索的关系数据存储在内存中.它们在功能使用方面非常相似,但是它们不可以相互替换. 主要区别如表所示: DataReader DataSet 数 ...