【序列操作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合并,但是假如要在序列 ...
随机推荐
- Node知识总结
一. 伪装URL-SEO 伪URL重写 把一个动态页面的地址重写为静态页面的地址,为了方便网站的SEO优化 真实地址:http://item.jd.com/detail.php?id=12261336 ...
- 2013腾讯编程马拉松初赛第〇场(HDU 4503) 湫湫系列故事——植树节
http://acm.hdu.edu.cn/showproblem.php?pid=4503 题目: 已知湫湫的班里共有n个孩子,每个孩子有Bi个朋友(i从1到n),且朋友关系是相互的,如果a小朋友和 ...
- oracle主机名修改
转自:http://www.cnblogs.com/tippoint/archive/2013/04/07/3003810.html 有的情况下,我们需要修改已经安装oracle数据库的主机名.以下是 ...
- 原生js大总结二
011.if语句的优化 1.把次数多的条件和执行结果放到最前面 2.减少第一次无用的判断,可以用嵌套判断 3.判断语句禁止出现三次嵌套 012.谈谈你对switch的理解 1. ...
- 管理aix的密码策略
aix 中 /etc/security/user 存放用户的概要 常用参数参数如下 1.account_locked defines whether the account is locke ...
- ZOJ 1136 Longest Ordered Subsequence DP
传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1136 题目大意:给定一串序列,求最长的升序列长度,如1, 7, 3, ...
- [Jade] Use Mixins in Pug
Mixin works as a function. extends layout include mixins/storeForm block content .inner h2 #{title} ...
- <p><span style="font-size:14px">近期须要批量将PNM格式的文件转换成GIF文件。我尝试了例如以下的图像转换工具:</span></p>
近期须要批量将PNM格式的文件转换成GIF文件.我尝试了例如以下的图像转换工具: ImageBatch:全然免费,但只支持PNG JPEG BMP GIF四种格式 OfficeConverter:在线 ...
- 《编程导论(Java)·4.1数据抽象的含义》
You have no choice about the necessity to integrateyour observations, your experiences, your knowled ...
- linux下Oracle11g RAC搭建(一)
linux下Oracle11g RAC搭建(一) 文档说明 作者 深蓝 项目 Visualbox下模拟RAC搭建(双节点)(Redhat5+Oracle11G) 环境 RedHat Enterp ...