[模板] Splay
欠了好久的Splay,以后就它了。
默写真不容易,过几天估计就忘了..
整个Splay真的精妙,不拖泥带水那种..
前驱后继之所以不能用rk转到根,是因为这个数不一定存在。。
kth中<=老忘记=
#include<iostream>
#include<cstdio>
#define check(x) (x==ch[fa[x]][1])
#define clear(x) val[x]=cnt[x]=siz[x]=ch[x][0]=ch[x][1]=fa[x]=0
#define pushup(x) siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+cnt[x]
using namespace std;
const int MAXN=100005;
int val[MAXN],cnt[MAXN],siz[MAXN];
int ch[MAXN][2],fa[MAXN];
int tot,root;
inline int newnode(int x){cnt[++tot]++;siz[tot]=1;val[tot]=x;return tot;}
void rotate(int x){
int y=fa[x],z=fa[fa[x]];
bool ck=check(x);
fa[ch[x][ck^1]]=y;ch[y][ck]=ch[x][ck^1];
ch[x][ck^1]=y;fa[y]=x;fa[x]=z;
if(z) ch[z][ch[z][1]==y]=x;
pushup(y);pushup(x);
}
void splay(int x){
for(int f=fa[x];f;rotate(x),f=fa[x])
if(fa[f]) rotate(check(f)==check(x)?f:x);
root=x;
}
void insert(int x){
if(!root){root=newnode(x);return;}
int cur=root,f=0;
while(1){
if(val[cur]==x){cnt[cur]++;pushup(cur);pushup(f);splay(cur);return;}
f=cur;cur=ch[cur][x>val[cur]];
if(!cur){cur=newnode(x);fa[cur]=f;ch[f][x>val[f]]=cur;pushup(f);splay(cur);return;}
}
}
int rk(int x){
int cur=root,ret=0;
while(1){
if(x<val[cur]) cur=ch[cur][0];
else{
ret+=siz[ch[cur][0]];
if(val[cur]==x) return splay(cur),ret+1;//
ret+=cnt[cur];cur=ch[cur][1];
}
}
}
int kth(int x){
int cur=root;
while(1){
if(ch[cur][0]&&x<=siz[ch[cur][0]]) cur=ch[cur][0];
else{
x-=siz[ch[cur][0]]+cnt[cur];
if(x<=0) return cur;
cur=ch[cur][1];
}
}
}
int prev(){
int cur=ch[root][0];
while(ch[cur][1]) cur=ch[cur][1];
return cur;
}
int nxtv(){
int cur=ch[root][1];
while(ch[cur][0]) cur=ch[cur][0];
return cur;
}
void del(int x){
rk(x);
if(cnt[root]>1){cnt[root]--;pushup(root);return;}
if(!ch[root][0]&&!ch[root][1]){clear(root);root=0;return;}
if(!ch[root][0]){int sav=root;root=ch[sav][1];fa[root]=0;clear(sav);return;}
if(!ch[root][1]){int sav=root;root=ch[sav][0];fa[root]=0;clear(sav);return;}
int sav=root;
splay(prev());
ch[root][1]=ch[sav][1];
fa[ch[sav][1]]=root;
clear(sav);
pushup(root);
}
int n;
inline int rd(){
int ret=0,f=1;char c;
while(c=getchar(),!isdigit(c))f=c=='-'?-1:1;
while(isdigit(c))ret=ret*10+c-'0',c=getchar();
return ret*f;
}
int main(){
n=rd();
int x,y;
for(int i=1;i<=n;i++){
x=rd();y=rd();
switch(x){
case 1:{insert(y);break;}
case 2:{del(y);break;}
case 3:{printf("%d\n",rk(y));break;}
case 4:{printf("%d\n",val[kth(y)]);break;}
case 5:{insert(y);printf("%d\n",val[prev()]);del(y);break;}
case 6:{insert(y);printf("%d\n",val[nxtv()]);del(y);break;}
}
}
return 0;
}
[模板] Splay的更多相关文章
- 算法模板——splay区间反转 2
实现功能:同splay区间反转 1(基于BZOJ3223 文艺平衡树) 这次改用了一个全新的模板(HansBug:琢磨了我大半天啊有木有),大大简化了程序,同时对于splay的功能也有所完善 这里面没 ...
- 算法模板——splay区间反转 1
实现的功能:将序列区间反转,并维护 详见BZOJ3223 var i,j,k,l,m,n,head,a1,a2:longint; s1:ansistring; a,b,c,d,fat,lef,rig: ...
- 【luogu P3369 普通平衡树(Treap/SBT)】 模板 Splay
题目链接:https://www.luogu.org/problemnew/show/P3369 #include <cstdio> #include <algorithm> ...
- 洛谷 P3391 模板Splay
#include<bits/stdc++.h> using namespace std; #define maxn 200000 int read() { ,w=; ;ch=getchar ...
- 模板—splay
#include<iostream> #include<cstdio> #define cin(x) scanf("%d",&x) using na ...
- 模板——Splay
$Splay$ #include <bits/stdc++.h> #define inf (int)1e9 using namespace std; const int N=1e5+100 ...
- Splay 伸展树
废话不说,有篇论文可供参考:杨思雨:<伸展树的基本操作与应用> Splay的好处可以快速分裂和合并. ===============================14.07.26更新== ...
- [NOI2003][bzoj1507] 文本编辑器 editor [splay]
其实看明白了就是一道水题 毕竟模板 splay敲一发,插入一个串的时候先把它构建成一棵平衡树,再挂到原来的splay上面去即可 没别的了,上代码 #include<iostream> #i ...
- splay最终模板
来自wjmzbmr的splay模板 #include<cstdio> #include<iostream> #include<algorithm> using na ...
随机推荐
- (5)css盒子模型(基础上)
CSS 盒子模型概述 ***什么是CSS的盒子模型呢?为什么叫它是盒子?先说说我们在网页设计中常听的属性名:内容(content).边框(border).内边距(padding).外边距(margin ...
- 在IDEA里gradle配置和使用
在IDEA里gradle配置和使用 在IDEA里gradle配置和使用 前言 Windows环境IDEA配置gradle 配置系统环境变量 下载 配置环境变量 测试 idea配置 gradle仓库设置 ...
- javascript 冒泡与捕获的原理及操作实例
所谓的javascript冒泡与捕获不是数据结构中的冒泡算法,而是javascript针对dom事件处理的先后顺序,所谓的先后顺序是指针对父标签与其嵌套子标签,如果父标签与嵌套子标签均有相同的事件时, ...
- SQL 初级教程学习(五)
1.DEFAULT 约束用于向列中插入默认值. CREATE TABLE Orders(Id_O int NOT NULL,OrderNo int NOT NULL,Id_P int,OrderDat ...
- Linux的防火墙概念
#linux的防火墙概念#因为如果你不关防火墙,很可能运行 django.nginx.mysql出错#防火墙可能会阻挡端口流量的 出口#也会阻挡外来请求的 入口 #selinux iptables f ...
- C. Mahmoud and a Message dp + 暴力
http://codeforces.com/contest/766/problem/C 关键在于dp,如何计数. 设dp[i]表示前i个字母中,能分成多少份合法的情况.那么答案就是dp[n],其中dp ...
- composer Failed to decode zlib stream 无法解码zlib流
Win7 中安装 Composer (PHP) 国内有些网络不能访问美国的Composer官网,可访问 Composer 中文网 学习. 目标 可以在任何目录下的项目中执行 PHP composer. ...
- easy ui combotree的操作
1.获取combotree的选中值 $("#id").combotree("getValue"); 2.设置combotree的选中值 $('#id').com ...
- 抽象工厂模式和php实现
抽象工厂模式: 抽象工厂模式(Abstract Factory Pattern):提供一个创建一系列相关或相互依赖对象的接口,而无须指定它们具体的类.抽象工厂模式又称为Kit模式,属于对象创建型模式. ...
- Farseer.net轻量级ORM开源框架 V1.0 开发目标
本篇主要给大家说明下在V1.0中,计划开发的任务的状态.按照国际惯例.上大表格 开发计划状态 编号 模块 状态 说明 1 分离Utils.Extend.UI √ 在V0.2版本中,是集成在一个项 ...