数组splay ------ luogu P3369 【模板】普通平衡树(Treap/SBT)
二次联通门 : luogu P3369 【模板】普通平衡树(Treap/SBT)
#include <cstdio> #define Max 100005 #define Inline __attri\
bute__( ( optimize( "-O2" ) ) ) Inline void read (int &now)
{
now = ;
register char word = getchar ();
bool temp = false;
while (word < '' || word > '')
{
if (word == '-')
temp = true;
word = getchar ();
}
while (word <= '' && word >= '')
{
now = now * + word - '';
word = getchar ();
}
if (temp)
now = -now;
} class Splay_Tree_Type
{
private: struct Tree_Date
{
int weigth;
int size;
int key;
int child[];
int father;
}
tree[Max]; Inline int Get_Son (int now)
{
return tree[tree[now].father].child[] == now;
} int Count;
int Root; public : Inline void Update (int now)
{
tree[now].size = tree[now].weigth;
if (tree[now].child[])
tree[now].size += tree[tree[now].child[]].size;
if (tree[now].child[])
tree[now].size += tree[tree[now].child[]].size;
} Inline void Rotate (int now)
{
int father = tree[now].father;
int Grand = tree[father].father;
int pos = Get_Son (now);
tree[father].child[pos] = tree[now].child[pos ^ ];
tree[tree[father].child[pos]].father = father;
tree[now].child[pos ^ ] = father;
tree[father].father = now;
tree[now].father = Grand;
if (Grand)
tree[Grand].child[tree[Grand].child[] == father] = now;
Update (father);
Update (now);
} Inline void Splay (int now)
{
for (int father; father = tree[now].father; Rotate (now))
if (tree[father].father)
Rotate (Get_Son (now) == Get_Son (father) ? father : now);
Root = now;
} Inline int Find_Prefix ()
{
int now = tree[Root].child[];
while (tree[now].child[])
now = tree[now].child[];
return now;
} Inline int Find_Suffix ()
{
int now = tree[Root].child[];
while (tree[now].child[])
now = tree[now].child[];
return now;
} Inline void Clear (int now)
{
tree[now].child[] = ;
tree[now].child[] = ;
tree[now].size = ;
tree[now].weigth = ;
tree[now].key = ;
tree[now].father = ;
} Inline int Find_x_rank (int x)
{
int now = Root;
int Answer = ;
while (true)
{
if (x < tree[now].key)
{
now = tree[now].child[];
continue;
}
Answer += tree[now].child[] ? tree[tree[now].child[]].size : ;
if (tree[now].key == x)
{
Splay (now);
return Answer + ;
}
Answer += tree[now].weigth;
now = tree[now].child[];
}
} Inline int Find_rank_x (int x)
{
int now = Root;
while (true)
{
if (tree[now].child[] && x <= tree[tree[now].child[]].size)
{
now = tree[now].child[];
continue;
}
int temp = (tree[now].child[] ? tree[tree[now].child[]].size : ) + tree[now].weigth;
if (x <= temp)
return tree[now].key;
x -= temp;
now = tree[now].child[];
}
} Inline void Insert (int x)
{
if (!Root)
{
Count++;
tree[Count].key = x;
tree[Count].size = ;
tree[Count].weigth = ;
Root = Count;
return;
}
int father = , now = Root;
while (true)
{
if (tree[now].key == x)
{
tree[now].size++;
tree[now].weigth++;
Splay (now);
return ;
}
father = now;
now = tree[now].child[x > tree[father].key];
if (!now)
{
Count++;
tree[father].child[x > tree[father].key] = Count;
tree[Count].father = father;
tree[Count].key = x;
tree[Count].size = ;
tree[Count].weigth = ;
Splay (Count);
return ;
}
}
} Inline void Delete (int x)
{
Find_x_rank (x);
if (tree[Root].weigth > )
{
tree[Root].weigth--;
tree[Root].size--;
return ;
}
if (!tree[Root].child[] && !tree[Root].child[])
{
Clear (Root);
Root = ;
return ;
}
if (!tree[Root].child[])
{
int temp = Root;
Root = tree[Root].child[];
tree[Root].father = ;
Clear (temp);
return ;
}
if (!tree[Root].child[])
{
int temp = Root;
Root = tree[Root].child[];
tree[Root].father = ;
Clear (temp);
return ;
}
int Prefix = Find_Prefix ();
int temp = Root;
Splay (Prefix);
tree[Root].child[] = tree[temp].child[];
tree[tree[temp].child[]].father = Root;
Clear (temp);
Update (Root);
} Inline int Get_tree_value (int now)
{
return tree[now].key;
}
}; Splay_Tree_Type Splay_Tree; int main (int argc, char *argv[])
{
int N; read (N);
int type, x; for (; N--; )
{
read (type);
read (x);
if (type == )
Splay_Tree.Insert (x);
else if (type == )
Splay_Tree.Delete (x);
else if (type == )
printf ("%d\n", Splay_Tree.Find_x_rank (x));
else if (type == )
printf ("%d\n", Splay_Tree.Find_rank_x (x));
else if (type == )
{
Splay_Tree.Insert (x);
printf ("%d\n", Splay_Tree.Get_tree_value (Splay_Tree.Find_Prefix ()));
Splay_Tree.Delete (x);
}
else
{
Splay_Tree.Insert (x);
printf ("%d\n", Splay_Tree.Get_tree_value (Splay_Tree.Find_Suffix ()));
Splay_Tree.Delete (x);
}
} return ;
}
数组splay ------ luogu P3369 【模板】普通平衡树(Treap/SBT)的更多相关文章
- luoguP3369[模板]普通平衡树(Treap/SBT) 题解
链接一下题目:luoguP3369[模板]普通平衡树(Treap/SBT) 平衡树解析 #include<iostream> #include<cstdlib> #includ ...
- 【模板】平衡树——Treap和Splay
二叉搜索树($BST$):一棵带权二叉树,满足左子树的权值均小于根节点的权值,右子树的权值均大于根节点的权值.且左右子树也分别是二叉搜索树.(如下) $BST$的作用:维护一个有序数列,支持插入$x$ ...
- [luogu P3369]【模板】普通平衡树(Treap/SBT)
[luogu P3369][模板]普通平衡树(Treap/SBT) 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除x数(若有多个相同的数,因只删 ...
- 红黑树 ------ luogu P3369 【模板】普通平衡树(Treap/SBT)
二次联通门 : luogu P3369 [模板]普通平衡树(Treap/SBT) 近几天闲来无事...就把各种平衡树都写了一下... 下面是红黑树(Red Black Tree) 喜闻乐见拿到了luo ...
- 替罪羊树 ------ luogu P3369 【模板】普通平衡树(Treap/SBT)
二次联通门 : luogu P3369 [模板]普通平衡树(Treap/SBT) 闲的没事,把各种平衡树都写写 比较比较... 下面是替罪羊树 #include <cstdio> #inc ...
- 洛谷P3369 【模板】普通平衡树(Treap/SBT)
洛谷P3369 [模板]普通平衡树(Treap/SBT) 平衡树,一种其妙的数据结构 题目传送门 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除 ...
- AC日记——【模板】普通平衡树(Treap/SBT) 洛谷 P3369
[模板]普通平衡树(Treap/SBT) 思路: 劳资敲了一个多星期: 劳资终于a了: 劳资一直不a是因为一个小错误: 劳资最后看的模板: 劳资现在很愤怒: 劳资不想谈思路!!! 来,上代码: #in ...
- P3369 【模板】普通平衡树 Treap
P3369 [模板]普通平衡树(Treap/SBT) 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除x数(若有多个相同的数,因只删除一个) 查询 ...
- luogu P3919 [模板]可持久化数组(可持久化线段树/平衡树)(主席树)
luogu P3919 [模板]可持久化数组(可持久化线段树/平衡树) 题目 #include<iostream> #include<cstdlib> #include< ...
随机推荐
- Linux 系统中如何进入退出 vim 编辑器
在 Linux 中,vim 编辑器是系统自带的文本编辑器,但要修改某个文本文件,可不是像 Windows 那样操作,更有新手,进入 vi 编辑器后,无法退出以致于强制关机,其实,这个vim(vi)也是 ...
- MVC-10HTML助手
HTML帮助器用于修改HTML输出. HTML帮助器 通过MVC,HTML帮助器类似于传统的ASP.NET Web Form控件. 类似ASP.NET中的web form控件,HTML帮助器用于修改H ...
- 实时监听 JavaScript改变 input 值 input输入框内容 value 变化实时监听
思路:通过setInterval()方法去定时对比新旧值 当时候JavaScript的onchange 和onpropertychange(注意ie版本) 监听输入框input时间,人为改变值是可以触 ...
- SQL注入绕过技巧
1.绕过空格(注释符/* */,%a0): 两个空格代替一个空格,用Tab代替空格,%a0=空格: % % %0a %0b %0c %0d %a0 %00 /**/ /*!*/ 最基本的绕过方法,用注 ...
- Referer和空Referer
参考CSDN 原文:https://blog.csdn.net/hxl188/article/details/38964743 Referer和空Referer 最近公司有个接口需要针对几个域名加白名 ...
- Android Jetpack组件 - ViewModel,LiveData使用以及原理
本文涉及的源码版本如下: com.android.support:appcompat-v7:27.1.1 android.arch.lifecycle:extensions:1.1.1 android ...
- 修正DejalActivityView在iOS8之前系统上存在的Bug
DejalActivityView是国外的第三方库,可自定义文本内容和文本长度的菊花转加载指示器效果.该第三方库与其它hud存在不同,能够遮盖键盘:可以自定义遮盖NavigationBar或不遮盖Na ...
- MySQL Innodb--共享临时表空间和临时文件
在MySQL 5.7版本中引入Online DDL特性和共享临时表空间特性,临时数据主要存放形式为: 1.DML命令执行过程中文件排序(file sore)操作生成的临时文件,存储目录由参数tmpdi ...
- MySQL Transaction--网络丢包导致长时间未提交事务
TCP三次握手(Three-Way Handshake) 建立一个TCP连接时,需要客户端和服务端总共发送3个包以确认连接的建立. TCP三次握手:第一次握手:Client将标志位SYN置为1,随机产 ...
- Centos部属前后端项目
一.安装python3 # 下载并解压 cd /opt wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz tar -zxf P ...