普通平衡树(指针splay)
最早的板子,学自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)的更多相关文章
- P3391 【模板】文艺平衡树(Splay)新板子
P3391 [模板]文艺平衡树(Splay) 题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转 ...
- fhq_treap || BZOJ 3223: Tyvj 1729 文艺平衡树 || Luogu P3391 【模板】文艺平衡树(Splay)
题面: [模板]文艺平衡树(Splay) 题解:无 代码: #include<cstdio> #include<cstring> #include<iostream> ...
- 洛谷 P3391 【模板】文艺平衡树(Splay)
题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...
- [知识点]平衡树之Splay
// 此博文为迁移而来,写于2015年7月18日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6rg.html 1.前 ...
- 【BZOJ】3223: Tyvj 1729 文艺平衡树(splay)
http://www.lydsy.com/JudgeOnline/problem.php?id=3223 默默的.. #include <cstdio> #include <cstr ...
- 平衡树(Splay):Splaytree POJ 3580 SuperMemo
SuperMemo Description Your friend, Jackson is invited to a TV show called SuperMemo in which ...
- bzoj3223 Tyvj 1729 文艺平衡树(Splay Tree+区间翻转)
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2202 Solved: 1226[Submit][Sta ...
- BZOJ3224普通平衡树【Splay】
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 11751 Solved: 5013 Descriptio ...
- 【BZOJ3223】文艺平衡树(Splay)
题面 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 ...
随机推荐
- 【BZOJ3106】[CQOI2013] 棋盘游戏(对抗搜索)
点此看题面 大致题意: 在一张\(n*n\)的棋盘上有一枚黑棋子和一枚白棋子.白棋子先移动,然后是黑棋子.白棋子每次可以向上下左右四个方向中任一方向移动一步,黑棋子每次则可以向上下左右四个方向中任一方 ...
- 【转】iOS 上常用的两个功能:点击屏幕和return退出隐藏键盘和解决虚拟键盘挡住UITextField的方法
iOS上面对键盘的处理很不人性化,所以这些功能都需要自己来实现, 首先是点击return和屏幕隐藏键盘 这个首先引用双子座的博客 http://my.oschina.net/plumsoft/blog ...
- 3218: 字符串字符统计—C语言
3218: 字符串字符统计—C语言 时间限制: 1 Sec 内存限制: 128 MB提交: 270 解决: 129[提交][状态][讨论版][命题人:smallgyy] 题目描述 编写一函数,由实 ...
- 【转】CentOS 7.0 安装Redis 3.2.1详细过程和使用常见问题
http://www.linuxidc.com/Linux/2016-09/135071.htm 环境:CentOS 7.0 Redis 3.2.1 Redis的安装与启动 这里我把Redis放在/h ...
- Github的基本功能
著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:Fadeoc Khaos链接:http://www.zhihu.com/question/20070065/answer/30 ...
- Linux学习记录(三)
1.Linux的软件安装 1.1.jdk安装 注意:rpm与软件相关命令 相当于window下的软件助手 管理软件 步骤: 1)查看当前Linux系统是否已经安装java 输入 rpm -qa | g ...
- Nodejs:Node.js模块机制小结
今天读了<深入浅出Nodejs>的第二章:模块机制.现在做一个简单的小结. 序:模块机制大致从这几个部分来讲:JS模块机制的由来.CommonJS AMD CMD.Node模块机制和包和n ...
- 使用IP地址方法登录MySQL数据库Can't connect to MySQL server的原因。mysql -h 192.168.1.104 -P3306 -uroot -p 失败
mysql -h 192.168.1.104 -P3306 -uroot -p 然后输入你安装时设置的MySQL密码 发现Can't connect to MySQL server 你的IP 解决方法 ...
- win10搭建FTP服务器
下面就给大家讲解Win10搭建FTP服务器的详细操作方法. 1.首先,我们在Cortana中搜索控制面板并进入: 2.在控制面板-程序中,点击启用或关闭Windows功能: 3.在FTP服务器.Web ...
- Symmetric Difference-freecodecamp算法题目
Symmetric Difference 1.要求 创建一个函数,接受两个或多个数组,返回所给数组的对等差分(symmetric difference) 例子:给出两个集合 (如集合 A = {1, ...