普通平衡树(指针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 ...
随机推荐
- 【BZOJ3172】[TJOI2013] 单词(AC自动机的小应用)
点此看题面 大致题意: 给你\(N\)个单词,请你求出每一个单词在这\(N\)个单词中出现的次数. 相关题目 这道题应该是洛谷上一道板子题的升级版. \(AC\)自动机 这是一道\(AC\)自动机的简 ...
- 记Tea使用中遇到的问题及解决过程
学习Markdown时,在小众软件看到一个叫Tea的软件.UI设计是简约风格:"所见即所得"的Markdown:支持插件等原因让我选择去尝试这杯"茶". 最近一 ...
- mysql添加、移除服务
sc delete 服务名 路径/bin/mysqld --install 服务名
- 黑马基础阶段测试题:创建Phone(手机)类,Phone类中包含以下内容:
package com.swift; public class Phone { private String pinpai; private int dianliang; public String ...
- STL笔记(こ)--删除数组中重复元素
使用STL中的Unique函数: #include<bits/stdc++.h> using namespace std; void fun(int &n) //配套for_eac ...
- c语言中--typeof--关键字用法
C语言中 typeof 关键字是用来定义变量数据类型的.在linux内核源代码中广泛使用. 下面是Linux内核源代码中一个关于typeof实例: #define min(x, y) ({ \ typ ...
- Cisco交换机与路由器命令总结
1.查看信息 show version 查看版本及引导信息 show running-config 查看运行设置 show startup-config 查看开机设置 show ...
- mount: no medium found on /dev/sr0 找不到介质
在VMware虚拟机中配置yum源时,执行 mount /dev/cdrom /mnt/cdrom 出现 mount: no medium found on /dev/sr0. 首先在/mnt 目录下 ...
- JavaScript 日期权威指南
简介 JavaScript通过强大的对象为我们提供日期处理功能:日期. 本文确实_不是_谈论 Moment.js ,我认为它是处理日期的最佳库,你应该在处理日期时几乎总是使用它. Date对象 Dat ...
- windows Server 2008 r2-搭建FTP服务器
FTP协议介绍 FTP协议工作在OSI参考模型的第七层,TCP模型的第四层上(即应用层上).使用FTP传输而不是UDP,与服务端建立连接经过三次握手. FTP端口介绍 FTP默认端口是21,.(21端 ...