splay模板(BZOJ3224)
用splay实现二叉搜索树的模板,支持插入,删除,找前缀后缀,x的排名以及第x名的数。
#include <cstdio>
#define l(x) t[x].s[0]
#define r(x) t[x].s[1]
#define f(x) t[x].p
#define lc(x) (r(f(x)) == x)
#define st(a,b,c) t[a].s[c] = b; f(b) = a const int N = ;
int m,x,op,tt,rt;
struct nd {int v,p,sz,s[];}t[N]; void pu(int x) {t[x].sz = t[l(x)].sz+t[r(x)].sz+;}
void rot(int x) {
int y = f(x), z = f(y), lx = lc(x), ly = lc(y);
st(y,t[x].s[!lx],lx); st(z,x,ly); st(x,y,!lx); pu(y);
}
void sp(int x) {
for(int y; y = f(x); rot(x)) if(f(y)) {
if(lc(x)^lc(y)) rot(x); else rot(y);
}
pu(x), rt = x;
}
void in(int x) {
int c = rt;
while(t[c].s[t[c].v<x]) t[c].sz++, c = t[c].s[t[c].v<x];
if(c) t[c].sz++; t[c].s[t[c].v<x] = ++tt, f(tt) = c, t[tt].v = x, t[tt].sz = , sp(tt);
}
void del(int x) {
int c = rt;
while() {
if(t[c].v == x) {x = c; break;}
if(t[c].v < x) c = r(c); else c = l(c);
}
sp(x);
if(!l(x)) f(r(x)) = , rt = r(x);
else if(!r(x)) f(l(x)) = , rt = l(x);
else {
f(l(x)) = , c = l(x);
while(r(c)) c = r(c);
sp(c), r(c) = r(x), f(r(x)) = c;
}
}
int pre(int x) {
int c = rt, ans = 0xcfcfcfcf;
while(c) if(t[c].v < x) ans = t[c].v, c = r(c); else c = l(c);
return ans;
}
int nxt(int x) {
int c = rt, ans = 0x3f3f3f3f;
while(c) if(t[c].v > x) ans = t[c].v, c = l(c); else c = r(c);
return ans;
}
int rk(int x) {
int c = rt, k = , ans = ;
while(c) if(t[c].v < x) ans = k+t[l(c)].sz+, k += t[l(c)].sz+, c = r(c); else c = l(c);
return ans+;
}
int sa(int x, int k) {
if(t[l(x)].sz == k-) return t[x].v;
if(t[l(x)].sz >= k) return sa(l(x), k);
return sa(r(x), k-t[l(x)].sz-);
} int main() {
scanf("%d", &m);
while(m--) {
scanf("%d%d", &op, &x);
if(op == ) in(x);
else if(op == ) del(x);
else if(op == ) printf("%d\n", rk(x));
else if(op == ) printf("%d\n", sa(rt,x));
else if(op == ) printf("%d\n", pre(x));
else printf("%d\n", nxt(x));
}
return ;
}
splay模板(BZOJ3224)的更多相关文章
- [luogu3369/bzoj3224]普通平衡树(splay模板、平衡树初探)
解题关键:splay模板题整理. 如何不加入极大极小值?(待思考) #include<cstdio> #include<cstring> #include<algorit ...
- bzoj 1588 splay模板题
用晚自习学了一下splay模板,没想象中那么难,主要是左旋和右旋可以简化到一个函数里边,减少代码长度... #include<iostream> #include<cstdio> ...
- COJ 1002 WZJ的数据结构(二)(splay模板)
我的LCC,LCT,Splay格式终于统一起来了... 另外..这个形式的Splay是标准的Splay(怎么鉴别呢?看Splay函数是否只传了一个变量node就行),刘汝佳小白书的Splay写的真是不 ...
- Splay 模板
Splay 模板 struct SplayTree{ const static int maxn = 1e5 + 15; int ch[maxn][2] , key[maxn] , s[maxn] , ...
- BZOJ1588 [HNOI2002]营业额统计 splay模板
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MB Submit: 16189 Solved: 6482 [Submit][S ...
- 文艺平衡树(splay模板)
题干:splay模板,要求维护区间反转. splay是一种码量小于treap,但支持排名,前驱后继等treap可求的东西,也支持区间反转的平衡树. 但是有两个坏处: 1.splay常数远远大于trea ...
- [洛谷P3391] 文艺平衡树 (Splay模板)
初识splay 学splay有一段时间了,一直没写...... 本题是splay模板题,维护一个1~n的序列,支持区间翻转(比如1 2 3 4 5 6变成1 2 3 6 5 4),最后输出结果序列. ...
- bzoj3224 普通平衡树 splay模板
题目传送门 题目大意:完成一颗splay树. 思路:模板题,学着还是很有意思的. 学习splay树:蒟蒻yyb 该题模板:汪立超 #include<bits/stdc++.h> #defi ...
- [bzoj3224]Tyvj 1728 普通平衡树——splay模板
题目 你需要写一种数据结构支援以下操作. 插入元素. 删除元素. 查询元素的排名. 查询第k小的元素. 查询元素前趋. 查询元素后继. 题解 BBST裸题. 代码 #include <cstdi ...
随机推荐
- webView调用系统地图,电话,和跳转链接的方法
webView.dataDetectorTypes = UIDataDetectorTypePhoneNumber | UIDataDetectorTypeLink | UIDataDetectorT ...
- JAVA_SE基础——12.运算符的优先级
优先级 操作符 含义 关联性 用法 ---------------------------------------------------------------- 1 [ ] 数组下标 左 arra ...
- H5 音频标签自定义样式修改以及添加播放控制事件
说明: 需求要求这个音频标签首先要是可适配移动端浏览器的,音频样式就是参考微信做的. 最终效果如下: 具体实现 思路: H5 的 <audio> 标签是由浏览器负责实现默认样式的.所以不同 ...
- 第一章 jQuery基础
第一章jQuery基础 一.jQuert简介 1.什么是jQuery jQuery是javaScript的程序库之一,它是javaScript对象和实用函数的封装. jQuery是继Prototype ...
- javascript 中的类型
javascript 中的类型 js 是一门弱语言,各式各样的错误多种多样,特别是确定返回值有问题的时候,你会用什么来进行表示错误? 我一般有三个选择: null '' error {} 第一个选择 ...
- (转载) Mysql 时间操作(当天,昨天,7天,30天,半年,全年,季度)
1 . 查看当天日期 select current_date(); 2. 查看当天时间 select current_time(); 3.查看当天时间日期 select current_timesta ...
- Python内置函数(22)——list
英文文档: class list([iterable]) Rather than being a function, list is actually a mutable sequence type, ...
- docker安装+测试环境的搭建---
漏洞演练环境docker地址:http://vulhub.org/#/environments/ 环境:kali-linux-2017.2-amd64.iso 一.docker安装 1.先更新一波源: ...
- C语言学习(一)
C语言易学难精,如果在平时的编程中,加入一些小技巧,可以提供程序运行的效率,何乐而不为呢? 本小白初学C语言准备记录自己的学C之路,经常贴一些自己觉得优化的小程序代码,希望大神们不吝 赐教. 宏定义下 ...
- Python 中格式化字符串 % 和 format 两种方法之间的区别
Python2.6引入了 format 格式化字符串的方法,现在格式化字符串有两种方法,就是 % 和 format ,具体这两种方法有什么区别呢?请看以下解析. # 定义一个坐标值 c = (250, ...