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\)的基础知识和模板. 阅读此文 ...
随机推荐
- java python oracle推断字符串是否为数字的函数
java public boolean isNumeric(String str){ Pattern pattern = Pattern.compile("^-?[0-9]+\\.?[0-9 ...
- Jenkins: 使用Jenkins搭建持续集成(CI)环境
http://www.cnitblog.com/luckydmz/archive/2012/01/03/77007.html 首先从官网http://jenkins-ci.org/下载 Java We ...
- Android应用开发学习笔记之绘图
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 一.绘图常用类介绍 在Android中绘图时,常用到的几个类是Paint.Canvas.Bitmap和Bitmapt ...
- Js正则表达式学习之test和compile的简单介绍
RegExp 对象用于规定在文本中检索的内容. 定义 RegExp RegExp 对象用于存储检索模式. 通过 new 关键词来定义 RegExp 对象.以下代码定义了名为 patt1 的 RegEx ...
- iPhone开发【一】从HelloWorld開始
转载请注明出处,原文网址:http://blog.csdn.net/m_changgong/article/details/8013553 作者:张燕广 从经典的HelloWorld開始踏入iPhon ...
- delphi 自我删除和线程池(1000行代码,需要仔细研究)
unit Unit4; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- Selenium来抓取动态加载的页面
一般的爬虫都是直接使用http协议,下载指定url的html内容,并对内容进行分析和抽取.在我写的爬虫框架webmagic里也使用了HttpClient来完成这样的任务. 但是有些页面是通过js以及a ...
- OCP读书笔记(8) - 监控和调优RMAN
监视RMAN作业 1. 创建rman备份: RMAN> run { allocate channel ch1 type disk; allocate channel ch2 type disk; ...
- GNU C的使用
基本语法 gcc [options] [filenames] 说明: 在gcc后面可以有多个编译选项,同时进行多个编译操作.很多 的gcc选项包括一个以上的字符.因此你必须为每个选项指定各 自 ...
- margin 等高布局
<div id="main"> <div id="left"> 我是左边的内容的啦啦啦啦... .<br> 我是左边的内容的 ...