洛谷——P3369 【模板】普通平衡树(splay)(基础splay,维护一些神奇的东东)
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
洛谷——P3369 【模板】普通平衡树(splay)(基础splay,维护一些神奇的东东)的更多相关文章
- 【洛谷P3369】普通平衡树——Splay学习笔记(一)
二叉搜索树(二叉排序树) 概念:一棵树,若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值: 它的左.右子树也分别为二叉搜索树 ...
- 洛谷.3369.[模板]普通平衡树(Splay)
题目链接 第一次写(2017.11.7): #include<cstdio> #include<cctype> using namespace std; const int N ...
- 洛谷.3391.[模板]文艺平衡树(Splay)
题目链接 //注意建树 #include<cstdio> #include<algorithm> const int N=1e5+5; //using std::swap; i ...
- 洛谷.3369.[模板]普通平衡树(fhq Treap)
题目链接 第一次(2017.12.24): #include<cstdio> #include<cctype> #include<algorithm> //#def ...
- 洛谷P3369 【模板】普通平衡树(Treap/SBT)
洛谷P3369 [模板]普通平衡树(Treap/SBT) 平衡树,一种其妙的数据结构 题目传送门 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除 ...
- 【洛谷P3369】【模板】普通平衡树题解
[洛谷P3369][模板]普通平衡树题解 题目链接 题意: 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3 ...
- 绝对是全网最好的Splay 入门详解——洛谷P3369&BZOJ3224: Tyvj 1728 普通平衡树 包教包会
平衡树是什么东西想必我就不用说太多了吧. 百度百科: 一个月之前的某天晚上,yuli巨佬为我们初步讲解了Splay,当时接触到了平衡树里的旋转等各种骚操作,感觉非常厉害.而第二天我调Splay的模板竟 ...
- 洛谷P3369普通平衡树(Treap)
题目传送门 转载自https://www.cnblogs.com/fengzhiyuan/articles/7994428.html,转载请注明出处 Treap 简介 Treap 是一种二叉查找树.它 ...
- 洛谷P3373 [模板]线段树 2(区间增减.乘 区间求和)
To 洛谷.3373 [模板]线段树2 题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.将某区间每一个数乘上x 3.求出某区间每一个数的和 输入输出格式 输入格 ...
随机推荐
- AndroidStudio更改Gradle的版本
1.首先需要在Gradle官网上下载需要的gradle版本,对于imac需要放置到AndroidStudio的安装目录下的gradle目录下面 2.更改项目的build.gradle的gradle的版 ...
- iOS如何查看静态库.a文件支持的cpu类型
打开终端: 输入 lipo -info 然后将你要查看的静态库.a 文件找到,拖入 -info 后边.假设路径为A,即为 lipo -info A 回车键,然后就会看到静态库是否支持 armv7,ar ...
- 【POI】T1 特工 szp
T1 特工szp [问题描述] Byteotian 中央情报局 (BIA) 雇佣了许多特工. 他们每个人的工作就是监视另一名特工.Byteasar 国王需要进行一次秘密行动,所以他要挑选尽量多的信得过 ...
- Dinic(模板 再错是不可能的 这辈子都不可能了)
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #inclu ...
- SVN工具使用技巧
SVN打tag SVN打tag是一个很常用的功能,要谈打tag,还得从SVN官方推荐的目录结构说起.SVN官方推荐在一个版本库的根目录下先建立trunk.branches.tags这三个文件夹,其中t ...
- 错误: 实例 "ruiy" 执行所请求操作失败,实例处于错误状态。: 请稍后再试 [错误: 'ascii' codec can't decode byte 0xe6 in position 0: ordinal not in range(128)].
错误: 实例 "ruiy" 执行所请求操作失败,实例处于错误状态.: 请稍后再试 [错误: 'ascii' codec can't decode byte 0xe6 in posi ...
- 1642: [Usaco2007 Nov]Milking Time 挤奶时间(dp)
1642: [Usaco2007 Nov]Milking Time 挤奶时间 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 935 Solved: 55 ...
- LoadRunner监控Linux配置教程
LoadRunner监控Linux资源时弹出如下错误: Monitor name :UNIX Resources. Cannot initialize the monitoring on 192.16 ...
- GG_Logs 日志类库封装使用说明
3.6.GG_Logs 日志类库封装使用说明 GG_Logs类库项目,Nuget安装log4net 添加代码配置代码: [assembly: log4net.Config.XmlConfigurato ...
- 贪心 CodeForces 137B Permutation
题目传送门 /* 贪心:因为可以任意修改,所以答案是没有出现过的数字的个数 */ #include <cstdio> #include <algorithm> #include ...