普通平衡树(指针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 ...
随机推荐
- swift和oc之间的相互调用(block,代理)
第一:swift->oc 这个相对简单一点, 在自动生成的桥接文件中导入你要掉的oc文件名就可以了, 如果没有生成桥接文件也可以自己手动创建(Header File) 第二:oc->swi ...
- 2018.6.4 Oracle数据库预定义的异常列表
declare v_ename emp.ename%type; begin select ename into v_ename from emp where empno=&gno; dbms_ ...
- CentOS启动时自动加载内核模块
要想在CentOS中自动加载内核模块,需要在/etc/sysconfig/modules/目录中增加一个脚本,在此脚本中加载所需的模块. 下面是我所用的一个名为8021q.modules的脚本,用来在 ...
- oc字典放入到数组里,根据字典里的属性排序(重点)
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { ...
- SummerVocation_Learning--java的自动打包与解包
Auto Boxing: 自动将基础类型转换成对象(JDK1.5之后支持) Auto UnBoxing:自动将对象转换成基础类型 如 Map中的put方法,如果要传入键值对<a,1>,&l ...
- Uva 填充正方形
暴力出奇迹 #include<iostream> #include<cstdio> using namespace std; +; int T,n; char S[maxn][ ...
- JS的四舍五入问题
最近踩了一个坑,mark一下toFixed四舍五入问题,详见代码: var myFixed = function(num, fix) { num = (parseFloat(num) * + ) / ...
- win7在某个盘或文件夹中出现右键只能新建文件夹的情况 (2012-12-28-bd 写的日志迁移
至于只能新建文件夹的情况如图: 解决方法是在运行中输入msconfig进入如图: 在系统设置选工具项在选中更改UAC设置点击启动如图: 如图: 直接把通知栏拉到最低确定即可(如果已经是最低了那就随便改 ...
- JZOJ 4742. 单峰
Description Input Output Sample Input 2 Sample Output 2 Data Constraint 做法:打标可以发现这道题是结论题,答案为2^(n-1), ...
- vue之神奇的动态按钮
今天我们将利用vue的条件指令来完成一个简易的动态变色功能按钮 首先我们还是要对vue进行配置,在上篇随笔中有相关下载教学. 然后我们要在html页面上搭建三个简易的按钮,颜色分别为紫,绿和蓝(颜色随 ...