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\)的基础知识和模板. 阅读此文 ...
随机推荐
- “新浪UC”的后江湖时代------易名新浪SHOW重出江湖
说到新浪UC,相信很多老网民应该并不陌生,当年QQ放号收费让新浪UC火爆了好一阵子,而随着QQ的崛起,UC也就渐渐退出了即时通信市场,不过,这并不意味着新浪UC退出了历史舞台,因为目前炙手可热 ...
- Firemonkey 自定义Button的Style
这篇文章模仿HTML中基于CSS的Button,通过Style实现自定义样式的Button. 前言 主要模仿的CSS代码如下: CSS Code 123456789101112131415161718 ...
- 【ASP.NET Web API教程】4.3 ASP.NET Web API中的异常处理
原文:[ASP.NET Web API教程]4.3 ASP.NET Web API中的异常处理 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本系列教程,请先看前面的内 ...
- 与众不同 windows phone (6) - Isolated Storage(独立存储)
原文:与众不同 windows phone (6) - Isolated Storage(独立存储) [索引页][源码下载] 与众不同 windows phone (6) - Isolated Sto ...
- SVN权限解析规则详解(转)
首先创建一个版本库后,会生成最初的目录结构和基本的配置文件,本文主要分析“authz”文件的内容:我们先抛开alias和groups不谈,将重点放在路径的权限配置上. 一. 权限格式 svn权限的基本 ...
- 同步linux服务器的时间
1:看系统时间的时候发现有点儿误差,大概在2分钟左右. 于是想自动同步系统的时间. 2:找到ntpdate命令,没有就安装.debian(apt-get install ntpdate) 3:获取时间 ...
- Spring Security Source Code -- 验证标准流程
除了初始阶段: 主干验证流程链: MyInvocationSecurityMetadataSource.getAttributes(Object) line: 43 MyFilterSecur ...
- go编程基础
可见性规则: go语言,根据函数名首字母大小写区分private,和pulic. 函数名首字母小写为private 函数名首字母大写为public
- C++历史
C++历史 早期C++ •1979: 首次实现引入类的C(C with Classes first implemented) 1.新特性:类.成员函数.继承类.独立编译.公共和私有访问控制.友元.函数 ...
- [docker]coreOS与atomic对照
声明: 本博客欢迎转发,但请保留原作者信息! 博客地址:http://blog.csdn.net/halcyonbaby 内容系本人学习.研究和总结,如有雷同,实属荣幸! 摘自https://majo ...