自己yy的Splay
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std; const int maxn=1e5+;
struct Node{
int val,rev,max,min;
Node(){}
Node(int _rev,int _max,int _min){
this->rev=_rev;
this->max=_max;
this->min=_min;
}
};
Node node[maxn];
int son[maxn][];//0 表示左儿子,1表示右儿子
int father[maxn];
bool Root[maxn];
int ROOT;
int tot;
int n,q,op;
struct bnode{
int v,dep;
bnode(int _v,int _dep){
this->v=_v;
this->dep=_dep;
}
};
vector<int> DEEP[maxn];
void init(){
tot=;
ROOT=-;
memset(Root,,sizeof(Root));
}
void BFS(){
int i;
for(i=;i<maxn;++i){
DEEP[i].clear();
}
if(ROOT==-) return;
queue<bnode> que;
que.push(bnode(ROOT,));
while(!que.empty()){
bnode t=que.front();
que.pop();
int v=t.v;
int dep=t.dep;
DEEP[dep].push_back(v);
if(son[v][]!=) que.push(bnode(son[v][],dep+));
if(son[v][]!=) que.push(bnode(son[v][],dep+));
}
}
void print(){
BFS();
int cnt=;
for(int j=;j<;++j){
if(DEEP[j].size()==) break;
cnt++;
printf("DEEP:%d==>",j);
for(int k=;k<DEEP[j].size();++k){
printf("%d ",DEEP[j][k]);
}
printf("\n");
}
if(!cnt) printf("The Tree is Empty\n");
}
void printVAL(){
int i;
for(i=;i<tot;++i){
printf("v%d val%d leftson%d rightson%d fa%d\n",i,node[i].val,son[i][],son[i][],father[i]);
}
}
void zig(int x){
int y=father[x];
int z=father[y];
son[y][]=son[x][];
if(son[x][]!=)
father[son[x][]]=y;
son[x][]=y;
if(y!=)
father[y]=x;
if(x!=)
father[x]=z;
if(z!=-&&z!=){
if(son[z][]==y){
son[z][]=x;
}
else {
son[z][]=x;
}
}
if(Root[y]){
Root[x]=true;
Root[y]=false;
ROOT=x;
}
}
void zag(int x){
int y=father[x];
int z=father[y];
son[y][]=son[x][];
if(son[x][])
father[son[x][]]=y;
son[x][]=y;
if(y)
father[y]=x;
if(x)
father[x]=z;
if(z!=-&&z!=){
if(son[z][]==y){
son[z][]=x;
}
else{
son[z][]=x;
}
}
if(Root[y]){
Root[x]=true;
Root[y]=false;
ROOT=x;
}
}
void zigzig(int x){
int y=father[x];
zig(y);
zig(x);
}
void zagzag(int x){
int y=father[x];
zag(y);
zag(x);
}
void zigzag(int x){
zig(x);zag(x);
}
void zagzig(int x){
zag(x);zig(x);
}
void Splay(int x){
while(!Root[x]){
int y=father[x];
int z=father[y];
if(z!=-&&!Root[z]){
if(son[z][]==y){
if(son[y][]==x){
zigzig(x);
}
else{
zag(x);zig(x);
}
}
if(son[z][]==y){
if(son[y][]==x){
zagzag(x);
}
else{
zig(x);zag(x);
}
}
}
else if(!Root[y]){
if(son[y][]==x){
zig(x);
}
else{
zag(x);
}
}
else {
if(son[y][]==x){
zig(x);
}
else {
zag(x);
}
}
// printf("==========\n");
// print();
// printVAL();
}
} int insert(int rt,int x){
if(rt==-){
rt=tot;
node[rt].val=x;
father[rt]=-;
Root[rt]=true;
ROOT=rt;
++tot;
return tot-;
}
if(x>node[rt].val){
if(son[rt][]==){
node[tot].val=x;
father[tot]=rt;
son[rt][]=tot;
tot++;
return tot-;
}
else insert(son[rt][],x);
}
else if(x<node[rt].val){
if(son[rt][]==){
node[tot].val=x;
father[tot]=rt;
son[rt][]=tot;
tot++;
return tot-;
}
else insert(son[rt][],x);
}
} int Search(int rt,int x){
// printf("rt%d x%d\n",rt,x);
if(rt==-){
//Exception
}
if(x==node[rt].val){
Splay(rt);
return rt;
}
if(x>node[rt].val){
if(son[rt][]==) return -;
return Search(son[rt][],x);
}
else if(x<node[rt].val){
if(son[rt][]==) return -;
return Search(son[rt][],x);
}
}
int SearchMax(int rt){
if(son[rt][]!=){
int x=son[rt][];
if(son[x][]==) return x;
return SearchMax(son[x][]);
}
return rt;
}
bool Delete(int x){
int id=Search(ROOT,x);
if(id==-) return false;
Splay(id);
// print();
// printf("=======\n");
int left=son[id][];
int right=son[id][];
son[id][]=;
son[id][]=;
if(left)
father[left]=-;
if(right)
father[right]=-;
Root[id]=false;
if(left)
Root[left]=true;
if(left)
ROOT=left;
if(left){
int lmx=SearchMax(left);
// printf("left%d lmx%d\n",left,lmx);
// print();
Splay(lmx);
son[lmx][]=right;
father[right]=lmx;
father[lmx]=-;
Root[id]=false;
Root[lmx]=true;
ROOT=lmx;
}
else if(right){
father[right]=-;
Root[id]=false;
Root[right]=true;
ROOT=right;
}
else{
init();
}
return true;
// print();
}
int findRoot(){ } void INSERT(int rt,int x){
int id=insert(rt,x);
printf("After insert\n");
print();
Splay(id);
printf("After Splay\n");
print();
}
int main(){
init();
scanf("%d%d",&n,&q);
//少写一个%d
int i,x;
for(i=;i<n;++i){
scanf("%d",&x);
INSERT(ROOT,x);
}
// printVAL();
for(i=;i<q;++i){
scanf("%d",&op);
if(op==){
scanf("%d",&x);
INSERT(ROOT,x);
}
else if(op==){
scanf("%d",&x);
int r=Delete(x);
if(r){
printf("Delete %d successful\n",x);
}
else printf("NOT FIND %d\n",x);
}
else if(op==){
scanf("%d",&x);
int id;
// print();
if((id=Search(ROOT,x))!=-){
printf("%d FIND v is %d\n",x,id);
}
else{
printf("%d NOT FIND\n",x);
}
}
else if(op==){
// printf("there22\n");
// printVAL();
print();
}
}
return ;
}
自己yy的Splay的更多相关文章
- BZOJ 3323 splay维护序列
就第三个操作比较新颖 转化成 在l前插一个点 把r和r+1合并 //By SiriusRen #include <cstdio> #include <cstring> #inc ...
- BZOJ 3729 splay维护DFS序+博弈论
思路: 这像是 阶梯Nim之类的东西 我们 直接把sg函数 设成mod(L+1)的 一棵子树 向下的奇数层上的石子xor起来 就是答案 有加点和改值的操作 就splay维护一下 //By Sirius ...
- bzoj 3506 && bzoj 1552 splay
查最小值,删除,翻转... 显然splay啊... #include<iostream> #include<cstdio> #include<algorithm> ...
- bzoj 1208 splay模板题2
自己yy了找前驱和后继,学了学怎么删除...(反正就是练模板) #include<iostream> #include<cstdio> #include<cstring& ...
- 【BZOJ-3196】二逼平衡树 线段树 + Splay (线段树套平衡树)
3196: Tyvj 1730 二逼平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2271 Solved: 935[Submit][Stat ...
- 【BZOJ-2733】永无乡 Splay+启发式合并
2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2048 Solved: 1078[Submit][Statu ...
- 【BZOJ】2209: [Jsoi2011]括号序列(splay)
http://www.lydsy.com/JudgeOnline/problem.php?id=2209 splay又犯逗........upd1那里的sum忘记赋值反............. 本题 ...
- Splay初步【bzoj1503】
做了一道水题,把bzoj1503用Splay重新写了一下. #include <bits/stdc++.h> #define rep(i, a, b) for (int i = a; i ...
- [bzoj1500][NOI2005]维修数列——splay
题目 题解 这道题可以说是数列问题的大BOSS,也算是这一周来学习splay等数据结构的一个总结. 我们一个一个地看这些操作. 对于操作1,我们首先建一棵子树,直接接上原树即可. 对于操作2,我们找到 ...
随机推荐
- perl 从文件里读出变量无法使用解决办法
最近在写一个perl函数,把test case 放到配置文件里,读出来然后使用system运行. 我的本意是: 配置文件conf ping -c $count $ip #在主程序中定义$ip和$cou ...
- 初识Python(一)
Python简介 Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解 ...
- java基础学习05(面向对象基础01--类实例分析)
面向对象基础01(类实例分析) 实现的目标 1.如何分析一个类(类的基本分析思路) 分析的思路 1.根据要求写出类所包含的属性2.所有的属性都必须进行封装(private)3.封装之后的属性通过set ...
- Effective C++ -----条款40:明智而审慎地使用多重继承
多重继承比单一继承复杂.它可能导致新的歧义性,以及对virtual继承的需要. virtual继承会增加大小.速度.初始化(及赋值)复杂度等等成本.如果virtual base classes不带任何 ...
- BestCoder19 1001.Alexandra and Prime Numbers(hdu 5108) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5108 题目意思:给出一个数正整数 N,N <= 1e9,现在需要找出一个最少的正整数 M,使得 ...
- jquery 中的一写常用方法
$('form').submit(); // 表单提交 window.parent.location.reload(); // 子窗口刷新父页面 window.location.reload(); / ...
- 25个增强iOS应用程序性能的提示和技巧(高级篇)(2)
25个增强iOS应用程序性能的提示和技巧(高级篇)(2) 2013-04-16 14:56 破船之家 beyondvincent 字号:T | T 在开发iOS应用程序时,让程序具有良好的性能是非常关 ...
- W3C对DOM2.0定义的标准事件
DOM2.0模型将事件处理流程分为三个阶段: 一.事件捕获阶段, 二.事件目标阶段, 三.事件起泡阶段. 具体如图(图片来源于网络,侵删) 事件捕获:当某个元素触发某个事件(如onclick),顶层对 ...
- JS时间自动更新
js部分: <!--自动更新时间--><script>function show(){var date = new Date(); //日期对象var now = " ...
- JavaScript基础——理解变量作用域
一旦你开始在JavaScript应用程序中添加条件.函数和循环,就需要理解变量作用域.变量作用域规定了如何确定正在执行的代码行上的一个特定变量名的值. JavaScript允许你既定义全局版本又定义局 ...