p2596 书架(Treap)
写平衡树修锅快修到死系列
我太蠢了
其实是平衡树裸体裸题
插入,删除,交换前驱或后继,查询rank和kth
维护一个pos数组,表示第i个书的编号
然后注意许许多多的细节,没了
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
struct Node{
int lson,rson,ran,val,w,sz;
}Treap[80000<<2];
int pos[80100],Lx,Rx,Nodecnt,n,m,root;
void pushup(int o){
Treap[o].sz=Treap[Treap[o].lson].sz+Treap[Treap[o].rson].sz+1;
}
void rotateR(int &o){
int k=Treap[o].lson;
Treap[o].lson=Treap[k].rson;
Treap[k].rson=o;
Treap[k].sz=Treap[o].sz;
pushup(o);
o=k;
}
void rotateL(int &o){
int k=Treap[o].rson;
Treap[o].rson=Treap[k].lson;
Treap[k].lson=o;
Treap[k].sz=Treap[o].sz;
pushup(o);
o=k;
}
int NewNode(int cx,int wl){
++Nodecnt;
Treap[Nodecnt].sz=1;
Treap[Nodecnt].lson=Treap[Nodecnt].rson=0;
Treap[Nodecnt].ran=rand();
Treap[Nodecnt].val=wl;
Treap[Nodecnt].w=cx;
return Nodecnt;
}
void insert(int &o,int cx,int wl){
if(!o){
o=NewNode(cx,wl);
return;
}
Treap[o].sz++;
if(wl<=Treap[o].val){
insert(Treap[o].lson,cx,wl);
if(Treap[Treap[o].lson].ran<Treap[o].ran)
rotateR(o);
// pushup(o);
}
else{
insert(Treap[o].rson,cx,wl);
if(Treap[Treap[o].rson].ran<Treap[o].ran)
rotateL(o);
// pushup(o);
}
}
void erase(int &o,int wl){
if(!o)
return;
if(Treap[o].val==wl){
if(Treap[o].lson==0||Treap[o].rson==0){
o=Treap[o].lson+Treap[o].rson;
return;
}
if(Treap[Treap[o].lson].ran<Treap[Treap[o].rson].ran){
rotateR(o);
erase(o,wl);
}
else{
rotateL(o);
erase(o,wl);
}
pushup(o);
return;
}
Treap[o].sz--;
if(wl<=Treap[o].val){
erase(Treap[o].lson,wl);
pushup(o);
}
else{
erase(Treap[o].rson,wl);
pushup(o);
}
}
int ranks(int o,int wl){
if(!o)
return 0;
if(wl==Treap[o].val){
return Treap[Treap[o].lson].sz+1;
}
if(wl<=Treap[o].val){
return ranks(Treap[o].lson,wl);
}
else{
return ranks(Treap[o].rson,wl)+Treap[Treap[o].lson].sz+1;
}
}
int kth(int k,int o){
if(k==0)
return 0;
if(k==Treap[Treap[o].lson].sz+1)
return Treap[o].w;
if(k<=Treap[Treap[o].lson].sz)
return kth(k,Treap[o].lson);
else
return kth(k-Treap[Treap[o].lson].sz-1,Treap[o].rson);
}
int pre(int wl){
return kth(ranks(root,wl)-1,root);
}
int back(int wl){
return kth(ranks(root,wl)+1,root);
}
int main(){
Lx=1;
// freopen("7.in","r",stdin);
// freopen("test.out","w",stdout);
srand(19260817);
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++){
int x;
scanf("%d",&x);
++Rx;
pos[x]=Rx;
insert(root,x,Rx);
}
char opt[20];
int sx,tx;
for(int i=1;i<=m;i++){
scanf("%s",opt);
if(opt[0]=='T'){
scanf("%d",&sx);
erase(root,pos[sx]);
Lx--;
pos[sx]=Lx;
insert(root,sx,Lx);
}
else if(opt[0]=='B'){
scanf("%d",&sx);
erase(root,pos[sx]);
Rx++;
pos[sx]=Rx;
insert(root,sx,Rx);
}
else if(opt[0]=='I'){
scanf("%d %d",&sx,&tx);
if(tx==0)
continue;
if(tx==1){
int need=back(pos[sx]);
erase(root,pos[sx]);
erase(root,pos[need]);
swap(pos[sx],pos[need]);
insert(root,sx,pos[sx]);
insert(root,need,pos[need]);
}
if(tx==-1){
int need=pre(pos[sx]);
erase(root,pos[sx]);
erase(root,pos[need]);
swap(pos[sx],pos[need]);
insert(root,sx,pos[sx]);
insert(root,need,pos[need]);
}
}
else if(opt[0]=='A'){
scanf("%d",&sx);
printf("%d\n",ranks(root,pos[sx])-1);
}
else if(opt[0]=='Q'){
scanf("%d",&sx);
printf("%d\n",kth(sx,root));
}
}
return 0;
}
p2596 书架(Treap)的更多相关文章
- 【Luogu】P2596书架(Splay)
题目链接 通过这题我加深了对Splay的理解,原来Splay的子树也是可以接来接去接到别的点上的,而不是只能旋转qwq 具体接的办法就是swap大法. 对于Top操作我们把当前节点Splay到根,然后 ...
- splay tree 学习笔记
首先感谢litble的精彩讲解,原文博客: litble的小天地 在学完二叉平衡树后,发现这是只是一个不稳定的垃圾玩意,真正实用的应有Treap.AVL.Splay这样的查找树.于是最近刚学了学了点S ...
- 洛谷 P2596 [ZJOI2006]书架 解题报告
P2596 [ZJOI2006]书架 题目描述 小T有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号. 小T在看书的时候,每次取出一本书, ...
- P2596 [ZJOI2006]书架 && Splay 区间操作(三)
P2596 [ZJOI2006]书架 题目描述 小T有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号. 小T在看书的时候,每次取出一本书, ...
- [洛谷P2596] [ZJOI2006]书架
洛谷题目链接:书架 题目描述 小T有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号. 小T在看书的时候,每次取出一本书,看完后放回书柜然后 ...
- [bzoj1861][Zjoi2006]Book 书架_非旋转Treap
Book 书架 bzoj-1861 Zjoi-2006 题目大意:给你一个序列,支持:将指定编号的元素抽出,放到序列顶(底):将指定编号元素左右篡位:查询指定编号元素位置:查询指定数量位置元素编号. ...
- fhq_treap || BZOJ1861: [Zjoi2006]Book 书架 || Luogu P2596 [ZJOI2006]书架
题面:P2596 [ZJOI2006]书架 题解:记录每本书对应的节点编号 普通fhq_treap无法查询一个权值的排名,所以在普通fhq_treap上多记录每个节点的父亲(可加在pushup函数中) ...
- P2596 [ZJOI2006]书架
思路 一开始写fhq-treap 感觉越写越感觉splay好些,就去splay 然后维护序列 注意前驱后继的不存在的情况 但不用插入虚拟节点(那插入岂不太麻烦) 跑的真慢的一批,splay太多了 错误 ...
- P2596 [ZJOI2006]书架(splay)
[题目链接] https://www.luogu.org/problemnew/show/P2596 平衡树,需支持五个操作: 1. 将某元素置顶:将元素旋到根,然后将左子树合并到该元素的后继 2. ...
随机推荐
- Hive 的排名和跨行 窗口函数及其使用
一.排序&去重分析 row_number() over(partititon by col1 order by col2) as rn 也可以用 row_number() over(distr ...
- Saiku + Kylin 多维分析平台探索
背景 为了应对各种数据需求,通常,我们的做法是这样的: 对于临时性的数据需求:写HQL到Hive里去查一遍,然后将结果转为excel发送给需求人员. 对于周期性的.长期性的数据需求:编写脚本,结合Hi ...
- 微信小程序制作家庭记账本之六
第六天,自己根据样本代码做的记账本可实现添加账目,删除账目和消费总和,实机可以测试,但是没有域名,别人无法访问.
- js定时器优化
在js中如果打算使用setInterval进行倒数,计时等功能,往往是不准确的,因为setInterval的回调函数并不是到时后立即执行,而是等系统计算资源空闲下来后才会执行.而下一次触发时间则是在s ...
- 洛谷 P1015 回文数
#include<iostream> #include<cstdio> #include<cmath> #include<string> #includ ...
- to refactor for refactor
v1.1 if all algorithm are in one function, it will expand. so each operate type should be separated. ...
- Eloquent JavaScript #11# The Document Object Model
索引 Notes js与html DOM 在DOM树中移动 在DOM中寻找元素 改变Document 创建节点 html元素属性 布局 style CSS选择器 动画 Exercises Build ...
- linux中权限对文件和目录的意义
1.权限对文件的意义: 读:可查看文件的内容 写:可修改文件的内容(但不能删除文件) 执行:可执行文件 2.权限对目录的意义: 读:可以查看目录下的内容,即可以读取该目录下的结构列表 写:可修改目录下 ...
- Java线程安全容器
一.Java同步容器 同步容器是用来解决并发情况下的容器线程安全问题的.给多线程环境准备一个线程安全的容器对象. 线程安全的容器对象: Vector, Hashtable.线程安全容器对象,都是使用s ...
- 07: mysql锁和事物隔离
MySQL其他篇 目录: 1.1 MySQL中的事物 1.2 mysql中锁 1.1 MySQL中的事物返回顶部 1.InnoDB事务原理 1. 事务(Transaction)是数据库区别于文件系统的 ...