写平衡树修锅快修到死系列

我太蠢了

其实是平衡树裸体裸题

插入,删除,交换前驱或后继,查询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)的更多相关文章

  1. 【Luogu】P2596书架(Splay)

    题目链接 通过这题我加深了对Splay的理解,原来Splay的子树也是可以接来接去接到别的点上的,而不是只能旋转qwq 具体接的办法就是swap大法. 对于Top操作我们把当前节点Splay到根,然后 ...

  2. splay tree 学习笔记

    首先感谢litble的精彩讲解,原文博客: litble的小天地 在学完二叉平衡树后,发现这是只是一个不稳定的垃圾玩意,真正实用的应有Treap.AVL.Splay这样的查找树.于是最近刚学了学了点S ...

  3. 洛谷 P2596 [ZJOI2006]书架 解题报告

    P2596 [ZJOI2006]书架 题目描述 小T有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号. 小T在看书的时候,每次取出一本书, ...

  4. P2596 [ZJOI2006]书架 && Splay 区间操作(三)

    P2596 [ZJOI2006]书架 题目描述 小T有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号. 小T在看书的时候,每次取出一本书, ...

  5. [洛谷P2596] [ZJOI2006]书架

    洛谷题目链接:书架 题目描述 小T有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号. 小T在看书的时候,每次取出一本书,看完后放回书柜然后 ...

  6. [bzoj1861][Zjoi2006]Book 书架_非旋转Treap

    Book 书架 bzoj-1861 Zjoi-2006 题目大意:给你一个序列,支持:将指定编号的元素抽出,放到序列顶(底):将指定编号元素左右篡位:查询指定编号元素位置:查询指定数量位置元素编号. ...

  7. fhq_treap || BZOJ1861: [Zjoi2006]Book 书架 || Luogu P2596 [ZJOI2006]书架

    题面:P2596 [ZJOI2006]书架 题解:记录每本书对应的节点编号 普通fhq_treap无法查询一个权值的排名,所以在普通fhq_treap上多记录每个节点的父亲(可加在pushup函数中) ...

  8. P2596 [ZJOI2006]书架

    思路 一开始写fhq-treap 感觉越写越感觉splay好些,就去splay 然后维护序列 注意前驱后继的不存在的情况 但不用插入虚拟节点(那插入岂不太麻烦) 跑的真慢的一批,splay太多了 错误 ...

  9. P2596 [ZJOI2006]书架(splay)

    [题目链接] https://www.luogu.org/problemnew/show/P2596 平衡树,需支持五个操作: 1. 将某元素置顶:将元素旋到根,然后将左子树合并到该元素的后继 2. ...

随机推荐

  1. EXTENDED LIGHTS OUT (高斯消元)

    In an extended version of the game Lights Out, is a puzzle with 5 rows of 6 buttons each (the actual ...

  2. 【转】robot framework + python实现http接口自动化测试框架

    前言 下周即将展开一个http接口测试的需求,刚刚完成的java类接口测试工作中,由于之前犯懒,没有提前搭建好自动化回归测试框架,以至于后期rd每修改一个bug,经常导致之前没有问题的case又产生了 ...

  3. android dumpsys

    dumpsys dumpsys is a tool that runs on Android devices and provides information about system service ...

  4. flask 处理表单数据

    处理表单数据 表单数据的处理涉及很多内容,从获取数据到保存数据大致有以下步骤: 1.  解析请求,获取表单数据 2.  对数据进行必要的转换,比如讲勾选框的值转换成python的布尔值 3.  验证数 ...

  5. es6基本语法

    //let和const申明变量和常量 //作用域只限于当前代码块 //使用let申明的变量作用域不会提升 //在相同的作用域下不能申明相同的变量 //for循环体现let的父子作用域 二.es6的解构 ...

  6. 51Nod 1069 Nim游戏 (位运算)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1069 有N堆石子.A B两个人轮流拿,A先拿.每次只能从一堆 ...

  7. C#使用SmtpClient发送邮件

    目的:写一个可发送邮件的DLL. 原理: 例如A使用163邮箱发送邮件给B(qq邮箱).首先A会把邮件通过SMTP(Simple Mail Transfer Protocol)协议传输到163的Smt ...

  8. 常用linux命令行

    1.ls命令 ls -a 列出目录所有文件,包含以.开始的隐藏文件 ls -A 列出除.及..的其它文件 ls -r 反序排列 ls -t 以文件修改时间排序 ls -S 以文件大小排序 ls -h ...

  9. 获取微信access_token

    /** * 获取微信access_token * @return mixed */function get_access_token() { $appId = C('APPID'); $secret ...

  10. K8S学习笔记之Flannel解读

    0x00 概述 我们知道docker官方并没有提供多主机的容器通信方案,单机网络的模式主要有host,container,brige,none.none这种模式,顾名思义就是docker本身不去管理网 ...