普通平衡树(指针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 ...
随机推荐
- 调试工具DEBUG的使用(8086)
有关CPU和存储单元的概念在前一节我们已经了解,那么如何观察实际机器内部的情况呢?能不能看到具体的寄存器.标志.存储单元的内容呢?可不可以修改和控制它们呢? DEBUG这个有力工具,就可以深入到机器内 ...
- bootstrap table 自定义checkbox样式
//css <style> .checkbox-custom { position: relative; padding: 0 15px 0 25px; margin-bottom: 7p ...
- shell脚本,awk实现跳过文件里面的空行。
1.用awk '{if(!NF ){next}}1' file11 实现对文件里面的空行进行跳过操作,并输出结果. 2. awk '{if(!NF || /^#/){next}}1' file11 实 ...
- 第四篇、Swift_Podfile文件配置格式
# Uncomment this line to define a global platform for your project platform :ios, '9.0' # Comment th ...
- shell中字符串基本用法
前言 今天在写脚本时,发现前阶段使用过的一些用法还是需要去百度查找,并且找到的答案还需要自己去筛选,耽误写脚本时间,这一篇对字符串之间的比较和逻辑判断都非常详细,借鉴他人之作,资源共享. 本片主要说明 ...
- zabbix mysql 迁移 增加分区
1.zabbix mysql 目录清单 --basedir=/usr/local/web/mysql --datadir=/data/mysql --log-error=/data/mysql/sys ...
- java POI往word文档中指定位置插入表格
1.Service demo import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.a ...
- 认识mysql(1)
---恢复内容开始--- 1.MySQL概述 1.什么是数据库? 存储数据的仓库 2.都有哪些公司在用数据库? 金融机构.游戏公司.购物网站.论坛网站... 3.提供数据库服务的软件? 1.软件分类 ...
- jquery 省市区联动插件
使用方式: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- 服务器常说的U是什么意思?
U是英文单词:unit 所说的1U和2U,是服务器的厚度,1U大概是相当于机柜的两个小格子,2U是四个格子.1U大概是4.45厘米(1U=1.75英寸,1英寸=2.54CM).以下这个是图片: