最早的板子,学自Ez大佬:

 #include<cstdio>
#include<cstdlib>
using namespace std; class Splay{
public:
Splay(){
root=NULL;
for(top;top<siez;top++)
stk[top]=tree+top;
}
inline void insert(int val){
if(find(val)!=NULL)
++root->cnt,update(root);
else if(root==NULL)
root=newnode(val,NULL);
else splay(insert(root,val),NULL);
}
inline void erase(int val){
if(find(val)!=NULL) erase(root,);
}
inline int rnk(int val){
if(find(val)!=NULL) return size(root->son[])+;
else return ;
}
inline int qry(int kth){
if(size(root)<kth) return ;
for(node *t=root;t;){
if(kth>size(t->son[])){
kth-=size(t->son[]);
if(kth<=t->cnt)
return t->v;
else kth-=t->cnt,t=t->son[];
}
else t=t->son[];
}
}
inline int prv(int val){
int ret=-0x7fffffff;
for(node *t=root;t;){
if(t->v<val){
if(ret<t->v)
ret=t->v;
}
t=t->son[val>t->v];
}
return ret;
}
inline int nxt(int val){
int ret=0x7fffffff;
for(node *t=root;t;){
if(t->v>val){
if(ret>t->v)
ret=t->v;
}
t=t->son[val>=t->v];
}
return ret;
}
private:
struct node{
int v,cnt,siz;
node *son[],*f;
node(){son[]=son[]=f=NULL;v=cnt=siz=;}
}*root;
const static int siez=;
node *stk[siez],tree[siez];int top;
inline node * newnode(int v,node *f){
node *t=stk[--top];
t->siz=t->cnt=;t->v=v;
t->son[]=t->son[]=NULL;
t->f=f;
return t;
}
inline void freenode(node *t){
stk[top++]=t;
}
inline int size(node* t){
return t==NULL?:t->siz;
}
inline void update(node *t){
if(t!=NULL)
t->siz=t->cnt+size(t->son[])+size(t->son[]);
} inline bool son(node* f,node* s){
if(f==NULL) return ;
return f->son[]==s;
}
inline void connect(node *f,node *s,bool k){
if(f!=NULL) f->son[k]=s;
else root=s;
if(s!=NULL) s->f=f;
}
inline void rotate(node* t){
node *f=t->f,*g=f->f;
bool a=son(f,t),b=!a;
connect(f,t->son[b],a);
connect(g,t,son(g,f));
connect(t,f,b);
update(f);
update(t);
}
inline void splay(node *t,node *p){
if(t){
while(t->f!=p){
node *f =t->f,*g=f->f;
if(g==p) rotate(t);
else{
if(son(g,f)^son(f,t))
rotate(t),rotate(t);
else
rotate(f),rotate(t);
}
}
}
}
inline node *find(int val){
node *t=root;
while(t!=NULL &&t->v!=val){
t=t->son[val>=t->v];
}
return splay(t,NULL ),t;
}
node *insert(node *t,int val){
node *ret=t->son[val>=t->v];
if(ret==NULL)
ret=t->son[val>=t->v]=newnode(val,t);
else ret=insert(ret,val);
return update(t),ret;
}
inline void erase(node *t){
if(t->son[]==NULL)
connect(NULL ,t->son[],),update(root);
else if(t->son[]==NULL){
connect(NULL,t->son[],),update(root);
}
else{
node *p=t->son[];
while(p->son[]!=NULL) p=p->son[];
splay(p,t);
connect(NULL,p,);
connect(p,t->son[],);
update(root);
}
freenode(t);
}
inline void erase(node *t ,int k){
t->cnt-=k;
if(t->cnt<=) erase(t);
else update(t);
}
}s;
int main(){
freopen("phs.in","r",stdin);
freopen("phs.out","w",stdout);
int n,op,x;
scanf("%d",&n);
while(n--){
scanf("%d%d",&op,&x);
switch(op){
case : s.insert(x);
break;
case :s.erase(x);
break;
case : printf("%d\n",s.rnk(x));
break;
case :printf("%d\n",s.qry(x));
break;
case :printf("%d\n",s.prv(x));
break;
default:printf("%d\n",s.nxt(x));
break;
}
}
}

The old

自己修改的板子(其实没变化):

 #define Troy

 #include "bits/stdc++.h"

 #define inf 0x7fffffff

 using namespace std;

 inline int read(){
int s=,k=;char ch=getchar();
while(ch<''|ch>'') ch=='-'?k=-:,ch=getchar();
while(ch>&ch<='') s=s*+(ch^),ch=getchar();
return s*k;
} const int N=2e5+; #define size(t) (t?t->size:0)
#define son(f,s) (f?f->son[1]==s:0)
#define update(t) (t?t->size=t->cnt+size(t->son[0])+size(t->son[1]):0) class Splay{
public:
Splay(){for(root=NULL;top<N;++top) stk[top]=tree+top;} inline void insert(int val){
if(find(val)) ++root->cnt,update(root);
else if(root) splay(insert(root,val),NULL);
else root=newnode(val,NULL);
} inline void erase(int val){if(find(val)) erase(root,);} inline int rnk(int val){
if(find(val)) return size(root->son[])+;
else return ;
} inline int qry(int kth){
if(size(root)<kth) return ;
for(node *t=root;t;)
if(kth>size(t->son[])){
kth-=size(t->son[])+t->cnt;
if(kth<=) return t->val;
else t=t->son[];
}else t=t->son[];
} inline int prv(int val){
int ret=-inf;
for(node *t=root;t;t=t->son[val>t->val])
if(t->val<val&&ret<t->val) ret=t->val;
return ret;
} inline int nxt(int val){
int ret=inf;
for(node *t=root;t;t=t->son[val>=t->val])
if(t->val>val&&ret>t->val) ret=t->val;
return ret;
}
private:
struct node{
node *f,*son[];
int val,cnt,size;
}tree[N],*root,*stk[N];int top; inline node* newnode(int val,node *f){
node *t=stk[--top];*t=(node){f,NULL,NULL,val,,};
return t;
} inline void freenode(node *t){stk[top++]=t;} inline void connect(node *f,node *s,int k){
if(f) f->son[k]=s;else root=s;
if(s) s->f=f;
} inline void rotate(node *t){
node *f=t->f,*g=f->f;int a=son(f,t),b=!a;
connect(f,t->son[b],a);
connect(g,t,son(g,f));
connect(t,f,b);
update(f),update(t);
} inline void splay(node *t,node *p){
if(t)for(;t->f!=p;rotate(t))
if(t->f->f!=p) rotate(son(t->f,t)==son(t->f->f,t->f)?t->f:t);
} inline node *find(int val){
node *t=root;
for(;t&&t->val!=val;t=t->son[val>=t->val]);
return splay(t,NULL),t;
} inline node *insert(node *t,int val){
node *ret=t->son[val>=t->val];
if(ret) ret=insert(ret,val);else ret=t->son[val>=t->val]=newnode(val,t);
return update(t),ret;
} inline void erase(node *t){
if(t->son[]==NULL) connect(NULL,t->son[],);
else if(t->son[]==NULL) connect(NULL,t->son[],);
else {node *p=t->son[];
while(p->son[]) p=p->son[];
splay(p,t);
connect(NULL,p,),connect(p,t->son[],);
}update(root),freenode(t);
} inline void erase(node *t,int k){
t->cnt-=k;
if(t->cnt<=) erase(t);else update(t);
}
}s;
int main(){
freopen("phs.in","r",stdin);
freopen("phs.out","w",stdout);
int n,op,x;
n=read();
while(n--){
op=read(),x=read();
switch(op){
case : s.insert(x);
break;
case :s.erase(x);
break;
case :printf("%d\n",s.rnk(x));
break;
case :printf("%d\n",s.qry(x));
break;
case :printf("%d\n",s.prv(x));
break;
default:printf("%d\n",s.nxt(x));
break;
}
}
}

普通平衡树(指针splay)的更多相关文章

  1. P3391 【模板】文艺平衡树(Splay)新板子

    P3391 [模板]文艺平衡树(Splay) 题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转 ...

  2. fhq_treap || BZOJ 3223: Tyvj 1729 文艺平衡树 || Luogu P3391 【模板】文艺平衡树(Splay)

    题面: [模板]文艺平衡树(Splay) 题解:无 代码: #include<cstdio> #include<cstring> #include<iostream> ...

  3. 洛谷 P3391 【模板】文艺平衡树(Splay)

    题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...

  4. [知识点]平衡树之Splay

    // 此博文为迁移而来,写于2015年7月18日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6rg.html 1.前 ...

  5. 【BZOJ】3223: Tyvj 1729 文艺平衡树(splay)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3223 默默的.. #include <cstdio> #include <cstr ...

  6. 平衡树(Splay):Splaytree POJ 3580 SuperMemo

    SuperMemo         Description Your friend, Jackson is invited to a TV show called SuperMemo in which ...

  7. bzoj3223 Tyvj 1729 文艺平衡树(Splay Tree+区间翻转)

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2202  Solved: 1226[Submit][Sta ...

  8. BZOJ3224普通平衡树【Splay】

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 11751  Solved: 5013 Descriptio ...

  9. 【BZOJ3223】文艺平衡树(Splay)

    题面 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 ...

随机推荐

  1. tk.mybatis Example 多个or条件拼接

    //需要的查询条件为 a and (b or c or d) 可以转换为 (a and b) or (a and c) or (a and d) private Example madeExample ...

  2. ERR_FAILED 浏览器访问

    我是针对上一篇的问题 继续的探究  ,我百度了 看了这 https://zhidao.baidu.com/question/1175643597811783659.html 之后我就用 电脑管家进行系 ...

  3. PAT (Advanced Level) Practise - 1094. The Largest Generation (25)

    http://www.patest.cn/contests/pat-a-practise/1094 A family hierarchy is usually presented by a pedig ...

  4. Win8如何默认以管理员运行程序

    在Win7的时候,关闭UAC,使用自己的用户名,所有程序都是默认以管理员身份运行的. 但是在Win8,关闭UAC,程序不是默认以管理员身份运行的. 在论坛看到的解决方法是:1.用Administrat ...

  5. 0.5px的边框

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 十六、MySQL LIKE 子句

    MySQL LIKE 子句 我们知道在 MySQL 中使用 SQL SELECT 命令来读取数据, 同时我们可以在 SELECT 语句中使用 WHERE 子句来获取指定的记录. WHERE 子句中可以 ...

  7. 通用后台管理系统源码,响应式布局,Java管理系统源码,零门槛安装部署

    本项目是一个通用响应式管理后台,导入开发环境安装就能直接运行,界面也非诚漂亮,在PC端和移动端也是自适应的.非常适合企业或者个人搭建各种商城后台,博客后台,网站管理后台等. 源码启动后的截图 需要这套 ...

  8. php扩展开发-哈希表

    什么是哈希表呢?哈希表在数据结构中也叫散列表.是根据键名经过hash函数计算后,映射到表中的一个位置,来直接访问记录,加快了访问速度.在理想情况下,哈希表的操作时间复杂度为O(1).数据项可以在一个与 ...

  9. psutil——获取系统信息的Python第三方模块

    本文摘自廖雪峰大神个人网站:https://www.liaoxuefeng.com/wiki/1016959663602400/1183565811281984 用Python来编写脚本简化日常的运维 ...

  10. POJ:2185-Milking Grid(KMP找矩阵循环节)

    Milking Grid Time Limit: 3000MS Memory Limit: 65536K Description Every morning when they are milked, ...