【序列操作V】平衡树(无旋treap)
题目描述
维护一个队列,初始为空。依次加入 n(1≤n≤105)个数 ai(-109≤ai≤109),第 i(1≤i≤n)个数加入到当前序列第 bi(0≤bi≤当前序列长度)个数后面。输出最终队列。
输入格式
输入包含一个数 n(1≤n≤3×105),表示最终序列长度。
接下来 n 行,每行两个数 ai,bi,表示把 ai 放在当前序列第 bi 个数后面。
输出格式
输出 n 行,每行一个数,表示最终序列。
样例数据 1
输入
5
1 0
2 0
3 0
4 0
5 0
输出
5
4
3
2
1
题目分析
平衡树如splay/treap应该都能完成,这里给出无旋treap的做法。用无旋treap的话简直就是裸题,时间复杂度nlogn
code
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std; const int N = 3e5 + ;
int n;
#define SZ(x) (x?x->sze:0)
inline void wr(int);
struct node{
node *lc, *rc;
int pri, sze, val;
inline node* upt(){
sze = SZ(lc) + SZ(rc) + ;
return this;
}
inline void print(){
if(lc) lc->print();
wr(val), putchar('\n');
if(rc) rc->print();
}
}pool[N], *tail = pool, *root = NULL; inline int read(){
int i = , f = ; char ch = getchar();
for(; (ch < '' || ch > '') && ch != '-'; ch = getchar());
if(ch == '-') f = -, ch = getchar();
for(; ch >= '' && ch <= ''; ch = getchar())
i = (i << ) + (i << ) + (ch - '');
return i * f;
} inline void wr(int x){
if(x < ) putchar('-'), x = -x;
if(x > ) wr(x / );
putchar(x % + '');
} inline int Rand(){
static int RAND_VAL = ;
return RAND_VAL += RAND_VAL << | ;
} inline node* newNode(int v){
node *x = tail++;
x->lc = x->rc = NULL;
x->pri = Rand();
x->val = v;
x->sze = ;
return x;
} inline node* Merge(node *u, node *v){
if(!u) return v->upt();
if(!v) return u->upt();
if(u->pri < v->pri){
u->rc = Merge(u->rc, v);
return u->upt();
}
else{
v->lc = Merge(u, v->lc);
return v->upt();
}
} inline void Split_k(node *u, int k, node *&x, node *&y){
if(!u){
x = y = NULL;
return;
}
if(SZ(u->lc) < k){
Split_k(u->rc, k - SZ(u->lc) - , x, y);
u->rc = NULL, u->upt();
x = Merge(u, x);
}
else{
Split_k(u->lc, k, x, y);
u->lc = NULL, u->upt();
y = Merge(y, u);
}
} inline node* Insert(node *u, int k, int v){
node *L, *R;
Split_k(u, k, L, R);
node *res = newNode(v);
return Merge(Merge(L, res), R);
} int main(){
n = read();
for(int i = ; i <= n; i++){
int a = read(), b = read();
root = Insert(root, b, a);
}
root->print();
return ;
}
【序列操作V】平衡树(无旋treap)的更多相关文章
- [BZOJ3223]文艺平衡树 无旋Treap
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MB Description 您需要写一种数据结构(可参考题目标题),来维护一个 ...
- BZOJ3678 wangxz与OJ (平衡树 无旋treap)
题面 维护一个序列,支持以下操作: 1.在某个位置插入一段值连续的数. 2.删除在当前序列位置连续的一段数. 3.查询某个位置的数是多少. 题解 显然平衡树,一个点维护一段值连续的数,如果插入或者删除 ...
- Luogu 3369 / BZOJ 3224 - 普通平衡树 - [无旋Treap]
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 https://www.luogu.org/problemnew/show/P3 ...
- BZOJ3223: Tyvj 1729 文艺平衡树 无旋Treap
一开始光知道pushdown却忘了pushup......... #include<cstdio> #include<iostream> #include<cstring ...
- 无旋Treap - BZOJ1014火星人 & 可持久化版文艺平衡树
!前置技能&概念! 二叉搜索树 一棵二叉树,对于任意子树,满足左子树中的任意节点对应元素小于根的对应元素,右子树中的任意节点对应元素大于根对应元素.换言之,就是满足中序遍历为依次访问节点对应元 ...
- [转载]无旋treap:从好奇到入门(例题:bzoj3224 普通平衡树)
转载自ZZH大佬,原文:http://www.cnblogs.com/LadyLex/p/7182491.html 今天我们来学习一种新的数据结构:无旋treap.它和splay一样支持区间操作,和t ...
- [您有新的未分配科技点]无旋treap:从好奇到入门(例题:bzoj3224 普通平衡树)
今天我们来学习一种新的数据结构:无旋treap.它和splay一样支持区间操作,和treap一样简单易懂,同时还支持可持久化. 无旋treap的节点定义和treap一样,都要同时满足树性质和堆性质,我 ...
- [Bzoj3224][Tyvj1728] 普通平衡树(splay/无旋Treap)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3224 平衡树入门题,学习学习. splay(学习yyb巨佬) #include<b ...
- 洛谷 - P3391 【模板】文艺平衡树(Splay) - 无旋Treap
https://www.luogu.org/problem/P3391 使用无旋Treap维护序列,注意的是按顺序插入的序列,所以Insert实际上简化成直接root和Merge合并,但是假如要在序列 ...
随机推荐
- [React] Remove React PropTypes by using Flow Annotations (in CRA)
Starting from v15.5 if we wanted to use React's PropTypes we had to change our code to use a separat ...
- jmeter--十三种断言方式介绍
jmeter中有个元件叫做断言(Assertion),它的作用和loadrunner中的检查点类似: 用于检查测试中得到的响应数据等是否符合预期,用以保证性能测试过程中的数据交互与预期一致. 使用断言 ...
- 使用BeautifulSoup爬取“0daydown”站点的信息(2)——字符编码问题解决
上篇中的程序实现了抓取0daydown最新的10页信息.输出是直接输出到控制台里面.再次改进代码时我准备把它们写入到一个TXT文档中.这是问题就出来了. 最初我的代码例如以下: #-*- coding ...
- 使用Samba在Linux服务器上搭建共享文件服务
最近我们的小团队需要在服务器上共分出一个共享文件夹用于大家存放公共的资源文档, 大家想啊,这肯定很简单呀,在Windows下面只要创建相关的windows account,共享某个文件夹,把读/写权限 ...
- netty epoll调用示例
1.服务器端 import io.netty.bootstrap.ServerBootstrap; import io.netty.buffer.PooledByteBufAllocator; imp ...
- python3中numpy函数的argsort()
摘自:https://www.cnblogs.com/yushuo1990/p/5880041.html argsort函数argsort函数返回的是数组值从小到大的索引值 Examples----- ...
- svn 清理失败 (clean up 失败) 的解决方法
解决方法: step1: 到 sqlite官网 (http://www.sqlite.org/download.html) 下载 sqlite3.exe 找到 Precompiled Binaries ...
- WM_CAP_DRIVER_CONNECT
WM_CAP_DRIVER_CONNECT //ActiveX ---->OnCreate m_pit.Create(IDD_CAM_DIALOG,this); CRect rc; this ...
- [Yarn] Use Yarn to Create an Alternative Import Name of an Installed Library
In this lesson we'll show how to use yarn to alias the names of same npm libraries but install diffe ...
- Linux有问必答:Linux上如何查看某个进程的线程
原创:LCTT https://linux.cn/article-5633-1.html 译者: GOLinux本文地址:https://linux.cn/article-5633-1.html201 ...