P3369 【模板】普通平衡树

平衡树大法好,蒟蒻(博主)最近正在收集高级数据结构的碎片,企图合成数据结构的元素之力来使自己的RP++。。。

您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:

插入xx数
删除xx数(若有多个相同的数,因只删除一个)
查询xx数的排名(排名定义为比当前数小的数的个数+1+1。若有多个相同的数,因输出最小的排名)
查询排名为xx的数
求xx的前驱(前驱定义为小于xx,且最大的数)
求xx的后继(后继定义为大于xx,且最小的数)

教程地址

博主太蒟,只能抄模板,曾经抄了个假的模板,有点=_=

让我们来看一看splay是如何进行神奇维护的吧!

int ch[N][],cnt[N],val[N],size[N],par[N],root,ncnt;
//左右儿子,重复值,数的值,子树大小,par父节点,根,出现过的节点数
int chk(int x){
return ch[par[x]][]==x;
}//返回x是其父节点的左儿子,还是右儿子
void pushup(int x){
size[x]=size[ch[x][]]+size[ch[x][]]+cnt[x];
}//维护x的size值,除了子树还要加上自身的重复值
//著名的rotate操作
void rotate(int x){
int y=par[x],z=par[y],k=chk(x),w=ch[x][k^];
ch[y][k]=w,par[w]=y;//父亲
ch[z][chk(y)]=x,par[x]=z;//祖父
ch[x][k^]=y,par[y]=x;//自己
pushup(y),pushup(x);
}
//交换儿子和父节点的位置,随之ch,par要改变
//splay吗,伸展操作
void splay(int x,int goal=){
while(par[x]!=goal){
int y=par[x],z=par[y];
if(z!=goal){
if(chk(y)==chk(x)) rotate(y);
else rotate(x);
}
rotate(x);
}
//分情况讨论,这样才能使树的深度尽量小
if(!goal) root=x;
}//将x旋转到根,或goal的儿子
//find操作,将最大的小于等于x的子树旋转到根
void find(int x){
// if(!root) return;
int cur=root;
while(ch[cur][x>val[cur]]&&x!=val[cur]){
cur=ch[cur][x>val[cur]];
}
splay(cur);
}
//插入操作
void insert(int x){
int cur=root,p=;
while(cur&&val[cur]!=x){
p=cur;
cur=ch[cur][x>val[cur]];
}
if(cur){
cnt[cur]++;
}else{
cur=++ncnt;
if(p) ch[p][x>val[p]]=cur;
ch[cur][]=ch[cur][]=;
val[cur]=x,par[cur]=p;
cnt[cur]=size[cur]=;
}
splay(cur);
}
查找第k大的数
int kth(int k){
int cur=root;
while(){
if(ch[cur][]&&k<=size[ch[cur][]]){//前提是它拥有左子树
cur=ch[cur][];
}else if(k>size[ch[cur][]]+cnt[cur]){
k-=size[ch[cur][]]+cnt[cur];//注意还要删去重复值
cur=ch[cur][];
}else return cur;
}
}
int pre(int x){//查找前驱
find(x);
if(val[root]<x) return root;
int cur=ch[root][];//返回左子树最右边的节点
while(ch[cur][]){
cur=ch[cur][];
}
return cur;
}
int succ(int x){
find(x);
if(val[root]>x) return root;
int cur=ch[root][];//返回右子树最左边的节点
while(ch[cur][]){
cur=ch[cur][];
}
return cur;
}
int remove(int x){
int last=pre(x),next=succ(x);
splay(last);splay(next,last);
int del=ch[next][];
if(cnt[del]>){
cnt[del]--;
splay(del);
}else ch[next][]=;
}

因为一个点前驱和后继之间只有自己本身

那么可以考虑把前驱splay到根,再把后继splay到前驱的右子树,那么后继的左儿子就是要删除的点。

#include<iostream>
#include<cstdio> #define N 10000000
using namespace std; int ch[N][],cnt[N],val[N],size[N],par[N],root,ncnt; int chk(int x){
return ch[par[x]][]==x;
} void pushup(int x){
size[x]=size[ch[x][]]+size[ch[x][]]+cnt[x];
} void rotate(int x){
int y=par[x],z=par[y],k=chk(x),w=ch[x][k^];
ch[y][k]=w,par[w]=y;//父亲
ch[z][chk(y)]=x,par[x]=z;//祖父
ch[x][k^]=y,par[y]=x;//自己
pushup(y),pushup(x);
} void splay(int x,int goal=){
while(par[x]!=goal){
int y=par[x],z=par[y];
if(z!=goal){
if(chk(y)==chk(x)) rotate(y);
else rotate(x);
}
rotate(x);
}
if(!goal) root=x;
} void find(int x){
// if(!root) return;
int cur=root;
while(ch[cur][x>val[cur]]&&x!=val[cur]){
cur=ch[cur][x>val[cur]];
}
splay(cur);
} void insert(int x){
int cur=root,p=;
while(cur&&val[cur]!=x){
p=cur;
cur=ch[cur][x>val[cur]];
}
if(cur){
cnt[cur]++;
}else{
cur=++ncnt;
if(p) ch[p][x>val[p]]=cur;
ch[cur][]=ch[cur][]=;
val[cur]=x,par[cur]=p;
cnt[cur]=size[cur]=;
}
splay(cur);
} int kth(int k){
int cur=root;
while(){
if(ch[cur][]&&k<=size[ch[cur][]]){
cur=ch[cur][];
}else if(k>size[ch[cur][]]+cnt[cur]){
k-=size[ch[cur][]]+cnt[cur];
cur=ch[cur][];
}else return cur;
}
} int pre(int x){
find(x);
if(val[root]<x) return root;
int cur=ch[root][];
while(ch[cur][]){
cur=ch[cur][];
}
return cur;
}
int succ(int x){
find(x);
if(val[root]>x) return root;
int cur=ch[root][];
while(ch[cur][]){
cur=ch[cur][];
}
return cur;
} void remove(int x){
int last=pre(x),next=succ(x);
splay(last);splay(next,last);
int del=ch[next][];
if(cnt[del]>){
cnt[del]--;
splay(del);
}else ch[next][]=;
} int n; int main()
{
scanf("%d",&n);
insert(0x3f3f3f3f);
insert(0xcfcfcfcf);
for(int opt,x,i=;i<=n;i++){
scanf("%d%d",&opt,&x);
if(opt==) insert(x);
if(opt==) remove(x);
if(opt==) find(x),printf("%d\n",size[ch[root][]]);
if(opt==) printf("%d\n",val[kth(x+)]);
if(opt==) printf("%d\n",val[pre(x)]);
if(opt==) printf("%d\n",val[succ(x)]);
} return ;
}

2018.10.11

打板日记——错误笔记

#include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm> #define N 10000000
#define IL inline
using namespace std; int ch[N][],par[N],siz[N],ncnt,root,val[N],cnt[N]; IL int chk(int x){
return ch[par[x]][]==x;
}
IL void pushup(int x){
siz[x]=siz[ch[x][]]+siz[ch[x][]]+cnt[x];
}
IL void rotate(int x){
int y=par[x],z=par[y],k=chk(x),w=ch[x][k^];
ch[y][k]=w,par[w]=y;
ch[z][chk(y)]=x,par[x]=z;
ch[x][k^]=y,par[y]=x;
pushup(y),pushup(x);
}
IL void splay(int x,int goal=){
while(par[x]!=goal){
int y=par[x],z=par[y];
if(z!=goal){
if(chk(x)==chk(y)) rotate(y);
else rotate(x);
}
rotate(x);
}
if(!goal) root=x;
}
IL void find(int x){
int cur=root;
while(x!=val[cur]&&ch[cur][x>val[cur]]){//将最大的<=x的数所在的节点splay到根
cur=ch[cur][x>val[cur]];
}
splay(cur);
}
IL void insert(int x){
int cur=root,p=;
while(cur&&x!=val[cur]){//前提是cur是真,即存在这个节点
p=cur;
cur=ch[cur][x>val[cur]];
}
if(cur){
cnt[cur]++;
}else{
cur=++ncnt;
if(p) ch[p][x>val[p]]=cur;
ch[cur][]=ch[cur][]=;
siz[cur]=cnt[cur]=;
par[cur]=p,val[cur]=x;
}
splay(cur);
}
IL int pre(int x){
find(x);
if(val[root]<x) return root;
int cur=ch[root][];
while(ch[cur][]){//返回左子树最右边的节点
cur=ch[cur][];
}
return cur;
}
IL int succ(int x){
find(x);
if(val[root]>x) return root;
int cur=ch[root][];
while(ch[cur][]){//返回右子树最左边的节点
cur=ch[cur][];
}
return cur;
}
IL int kth(int k){
int cur=root;
while(){
if(ch[cur][]&&k<=siz[ch[cur][]]){//前提是他拥有左子树
cur=ch[cur][];
}else if(k>siz[ch[cur][]]+cnt[cur]){
k-=siz[ch[cur][]]+cnt[cur];//注意细节,还要减去自身的重复值
cur=ch[cur][];
}else return cur;
}
} IL void remove(int x){
int last=pre(x),next=succ(x);
splay(last),splay(next,last);
int del=ch[next][];//并不是根的左子树,删除的是后继的左儿子
if(cnt[del]>){
cnt[del]--;
splay(del);
}else ch[next][]=;
} int n; int main()
{
scanf("%d",&n);
insert(0x3f3f3f3f);
insert(0xcfcfcfcf);
for(int opt,x,i=;i<=n;i++){
scanf("%d%d",&opt,&x);
if(opt==) insert(x);
if(opt==) remove(x);
if(opt==) find(x),printf("%d\n",siz[ch[root][]]);
if(opt==) printf("%d\n",val[kth(x+)]);
if(opt==) printf("%d\n",val[pre(x)]);
if(opt==) printf("%d\n",val[succ(x)]);
} return ;
}

emmmm

splay另一个骚操作请转emmm

STL or 01trie

洛谷——P3369 【模板】普通平衡树(splay)(基础splay,维护一些神奇的东东)的更多相关文章

  1. 【洛谷P3369】普通平衡树——Splay学习笔记(一)

    二叉搜索树(二叉排序树) 概念:一棵树,若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值: 它的左.右子树也分别为二叉搜索树 ...

  2. 洛谷.3369.[模板]普通平衡树(Splay)

    题目链接 第一次写(2017.11.7): #include<cstdio> #include<cctype> using namespace std; const int N ...

  3. 洛谷.3391.[模板]文艺平衡树(Splay)

    题目链接 //注意建树 #include<cstdio> #include<algorithm> const int N=1e5+5; //using std::swap; i ...

  4. 洛谷.3369.[模板]普通平衡树(fhq Treap)

    题目链接 第一次(2017.12.24): #include<cstdio> #include<cctype> #include<algorithm> //#def ...

  5. 洛谷P3369 【模板】普通平衡树(Treap/SBT)

    洛谷P3369 [模板]普通平衡树(Treap/SBT) 平衡树,一种其妙的数据结构 题目传送门 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除 ...

  6. 【洛谷P3369】【模板】普通平衡树题解

    [洛谷P3369][模板]普通平衡树题解 题目链接 题意: 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3 ...

  7. 绝对是全网最好的Splay 入门详解——洛谷P3369&BZOJ3224: Tyvj 1728 普通平衡树 包教包会

    平衡树是什么东西想必我就不用说太多了吧. 百度百科: 一个月之前的某天晚上,yuli巨佬为我们初步讲解了Splay,当时接触到了平衡树里的旋转等各种骚操作,感觉非常厉害.而第二天我调Splay的模板竟 ...

  8. 洛谷P3369普通平衡树(Treap)

    题目传送门 转载自https://www.cnblogs.com/fengzhiyuan/articles/7994428.html,转载请注明出处 Treap 简介 Treap 是一种二叉查找树.它 ...

  9. 洛谷P3373 [模板]线段树 2(区间增减.乘 区间求和)

    To 洛谷.3373 [模板]线段树2 题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.将某区间每一个数乘上x 3.求出某区间每一个数的和 输入输出格式 输入格 ...

随机推荐

  1. 异常机制及throw与throws的区别(转)

    异常机制及throw与throws的区别 分类: Java2008-11-14 16:08 9672人阅读 评论(5) 收藏 举报 exceptionstringjavafunclass编译器 Jav ...

  2. Java IO 输入输出流 详解 (一)***

    首先看个图: 这是Javaio 比较基本的一些处理流,除此之外我们还会提到一些比较深入的基于io的处理类,比如console类,SteamTokenzier,Externalizable接口,Seri ...

  3. 聊聊LuaJIT

    JIT 什么是JITJIT = Just In Time即时编译,是动态编译的一种形式,是一种优化虚拟机运行的技术. 程序运行通常有两种方式,一种是静态编译,一种是动态解释,即时编译混合了这二者.Ja ...

  4. 【NOI2012】魔幻棋盘

    Description 将要读二年级的小 Q 买了一款新型益智玩具——魔幻棋盘,它是一个N行M列的网格棋盘,每个格子中均有一个正整数.棋盘守护者在棋盘的第X行Y列(行与列均从1开始编号) 并且始终不会 ...

  5. 利用 nodeJS 搭建一个简单的Web服务器(转)

    下面的代码演示如何利用 nodeJS 搭建一个简单的Web服务器: 1. 文件 WebServer.js: //-------------------------------------------- ...

  6. 【Python精华】100个Python练手小程序

    100个Python练手小程序,学习python的很好的资料,覆盖了python中的每一部分,可以边学习边练习,更容易掌握python. [程序1] 题目:有1.2.3.4个数字,能组成多少个互不相同 ...

  7. pom.xml详情

    这里借鉴一下csdn中的一个系列的博客: 第一篇:POM文件详解 第二篇:maven中的依赖作用范围 第三篇:maven中的可选依赖和依赖排除 第四篇:maven中的dependencies和depe ...

  8. 卸载掉原有mysql

    [root@xiaoluo ~]# rpm -qa | grep mysql // 这个命令就会查看该操作系统上是否已经安装了mysql数据库 有的话,我们就通过 rpm -e 命令 或者 rpm - ...

  9. (8)string对象上的操作1

     读写操作 //读写string对象的测试.//本程序输入两string类,输出两string类. #include <iostream> #include <string> ...

  10. App保持登录状态的常用方法

    我们在使用App时,一次登录后App如果不主动退出登录或者清除数据,App会在很长一段时间内保持登录状态,或者让用户感觉到登录一次就不用每次都输入用户密码才能进行登录.银行.金融涉及到支付类的App一 ...