Splay树
class SplayNode {
public:
SplayNode *child[];
char value;
int size;
bool flip;
SplayNode(char c) : value(c), size(), flip(false) {
child[] = child[] = NULL;
}
int getPosition()const {
return child[] ? child[]->size + : ;
}
void maintain() {
size = ;
if (child[]) {
size += child[]->size;
}
if (child[]) {
size += child[]->size;
}
}
void pushDown() {
if (flip) {
swap(child[], child[]);
for (int i = ; i < ; i++) {
if (child[i]) {
child[i]->flip ^= ;
}
}
flip = false;
}
}
};
class SplayTree {
public:
SplayNode *root;
SplayTree(char *a, int n);
void build(SplayNode *&node, char *begin, char *end);
void rotate(SplayNode *&node, int direction);
void splay(SplayNode *&node, int position);
void reverse(int begin, int end);
void traverse(SplayNode *u);
void traverse();
};
SplayTree::SplayTree(char *a, int n) {
build(root, a, a + n - );
}
void SplayTree::build(SplayNode *&node, char *begin, char *end) {
if (begin > end) {
return;
}
char *middle = begin + (end - begin >> );
node = new SplayNode(*middle);
build(node->child[], begin, middle - );
build(node->child[], middle + , end);
node->maintain();
}
void SplayTree::rotate(SplayNode *&node, int direction) {
SplayNode *child = node->child[direction ^ ];
node->child[direction ^ ] = child->child[direction];
child->child[direction] = node;
node->maintain();
child->maintain();
node = child;
}
void SplayTree::splay(SplayNode *&node, int position) {
node->pushDown();
if (node->getPosition() != position) {
int d = node->getPosition() < position;
SplayNode *node2 = node->child[d];
position -= d ? node->getPosition() : ;
node2->pushDown();
if (node2->getPosition() != position) {
int d2 = node2->getPosition() < position;
position -= d2 ? node2->getPosition() : ;
splay(node2->child[d2], position);
if (d == d2) {
rotate(node, d ^ );
} else {
rotate(node->child[d], d);
}
}
rotate(node, d ^ );
}
}
void SplayTree::reverse(int begin, int end) {
splay(root, begin);
splay(root->child[], end - begin + );
root->child[]->child[]->flip ^= ;
}
void SplayTree::traverse(SplayNode *u) {
if (!u) {
return;
}
u->pushDown();
traverse(u->child[]);
if (u->value) {
printf("%c", u->value);
}
traverse(u->child[]);
}
void SplayTree::traverse() {
traverse(root);
}
Splay(root,p)表示 把以root为根的子树中前序遍历的第p个元素旋转到root子树的树根位置
这个代码风格跟我不一样,晚上回去改一改。太菜了,改不了。
Splay树的旋转过程按照二叉搜索树的性质操作,保证前序遍历是不变的。但是就节点值来说,未必满足二叉搜索树的性质(大概此时可以把节点值理解为<key,value>的键值对吧,只不过此时value值具体是多少根本没什么影响,就忽略掉了)......
如何应用就有些困惑了,跟序列操作有关系的问题大概能往这个方面考虑一下,感觉就是以另一种思路实现了线段树的功能......但是能做一些线段树做不了的操作,比如区间反转,而且比线段树慢,别的一时就想不到了,还是刷题太少的缘故。
Splay树的更多相关文章
- Splay树-Codevs 1296 营业额统计
Codevs 1296 营业额统计 题目描述 Description Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司 ...
- ZOJ3765 Lights Splay树
非常裸的一棵Splay树,需要询问的是区间gcd,但是区间上每个数分成了两种状态,做的时候分别存在val[2]的数组里就好.区间gcd的时候基本上不支持区间的操作了吧..不然你一个区间里加一个数gcd ...
- Splay树再学习
队友最近可能在学Splay,然后让我敲下HDU1754的题,其实是很裸的一个线段树,不过用下Splay也无妨,他说他双旋超时,单旋过了,所以我就敲来看下.但是之前写的那个Splay越发的觉得不能看,所 ...
- 暑假学习日记:Splay树
从昨天开始我就想学这个伸展树了,今天花了一个上午2个多小时加下午2个多小时,学习了一下伸展树(Splay树),学习的时候主要是看别人博客啦~发现下面这个博客挺不错的http://zakir.is-pr ...
- 1439. Battle with You-Know-Who(splay树)
1439 路漫漫其修远兮~ 手抄一枚splay树 长长的模版.. 关于spaly树的讲解 网上很多随手贴一篇 貌似这题可以用什么bst啦 堆啦 平衡树啦 等等 这些本质都是有共同点的 查找.删除特 ...
- 伸展树(Splay树)的简要操作
伸展树(splay树),是二叉排序树的一种.[两个月之前写过,今天突然想写个博客...] 伸展树和一般的二叉排序树不同的是,在每次执行完插入.查询.删除等操作后,都会自动平衡这棵树.(说是自动,也就是 ...
- [Splay伸展树]splay树入门级教程
首先声明,本教程的对象是完全没有接触过splay的OIer,大牛请右上角.. 首先引入一下splay的概念,他的中文名是伸展树,意思差不多就是可以随意翻转的二叉树 PS:百度百科中伸展树读作:BoGa ...
- hdu 3436 splay树+离散化*
Queue-jumpers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- hdu 1890 splay树
Robotic Sort Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- hdu3487 splay树
Play with Chain Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
随机推荐
- MVC 返回404,返回图片,流到数组,apk信息
return HttpNotFound(); byte[] buffer0 = QRCode(); return File(buffer0, @"image/jpeg"); // ...
- jq 禁用复选框 和输入框
$('input').attr("readonly", ""); $('input').attr("disabled", "fal ...
- Js正则匹配处理时间
<html> <body> <script type="text/javascript"> //将long 型 转换为 日期格式 年-月-日 h ...
- 在Unity中json文件的解析方式
using System.Collections; using System.Collections.Generic; using UnityEngine; using LitJson; using ...
- eas之树
如何设置树的深度(即树总共有几级) // 设置树的深度为3,即树包括0.1.2三级结点 table.getTreeColumn().setDepth(3); 如何设置树的方向 树的方向包括两种:自上向 ...
- 【剑指Offer】64、滑动窗口的最大值
题目描述: 给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值.例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{ ...
- let、var、const用法区别
1.var var 声明的变量为全局变量,并会进行变量提升:也可以只声明变量而不进行赋值,输出为undefined,以下写法都是合法的. var a var a = 123 2.let let 声明 ...
- bpm被攻击事件
bpm登录不上,服务器是windows2008,从深信服上面设置了ddos每秒钟连接超5000次封锁,阻断后面的IP连接,,深信服DDOS日志没有记录 在bpm服务器上面通过netstat -a查看发 ...
- layer 使用教程
http://layer.layui.com/ <!DOCTYPE html><html lang="en"><head> <meta c ...
- 搜索引擎seo优化
<a href="" title="SEO优化"></a> <img src="" alt="SEO ...