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 ...
随机推荐
- PM2使用心得
PM2是node进程管理工具,可以利用它来简化很多node应用管理的繁琐任务,如性能监控.自动重启.负载均衡等,而且使用非常简单. 安装 npm install -g pm2 常用命令 $ npm i ...
- 流程控制语句(MySQL/MariaDB )
本文目录:1.BEGIN...END2.true和false3.if结构4.case结构5.loop.leave和iterate6.repeat循环7.while循环 MySQL/MariaDB中的符 ...
- 读论文系列:Object Detection ECCV2016 SSD
转载请注明作者:梦里茶 Single Shot MultiBox Detector Introduction 一句话概括:SSD就是关于类别的多尺度RPN网络 基本思路: 基础网络后接多层featur ...
- GZip 压缩及解压缩
/// <summary> /// GZipHelper /// </summary> public class GZipHelper { /// <summary> ...
- 在WebStorm中启动Angular项目
点击配置 创建 选择命令 package.json 运行 查看运行结果
- java获取本类路径
(1).Test.class.getResource("") 得到的是当前类FileTest.class文件的URI目录.不包括自己! (2).Test.class.getReso ...
- angular2 学习笔记 ( animation 动画 )
refer : https://angular.io/guide/animations https://github.com/angular/angular/blob/master/packages/ ...
- Linux下的Shell编程(2)环境变量和局部变量
Shell Script是一种弱类型语言,使用变量的时候无需首先声明其类型. 局部变量在本地数据区分配内存进行存储,这个变量归当前的Shell所有,任何子进 程都不能访问本地变量.这些变量与环境变量不 ...
- 英语词汇(5)followed by / sung by / written by
Sung by 演唱者; [例句]In the recording I have today, it is sung by a male alto.我今天带的唱片是由一位男高音歌手唱的. follow ...
- C# JavaScriptSerializer找不到引用
遇到一个问题,还是第一次遇到,虽然比较简单,还是记录一下 一.写了一个小工具,为了方便就建了个Form窗体,结果用到了JavaScriptSerializer类,可是怎么都找不到System.Web. ...