数组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< ...
随机推荐
- js获取项目名称
//获取路径 var pathName=window.document.location.pathname; //截取,得到项目名称 var projectName=pathName.substrin ...
- WPF 不要给 Window 类设置变换矩阵(应用篇)
原文:WPF 不要给 Window 类设置变换矩阵(应用篇) WPF 的 Window 类是不允许设置变换矩阵的.不过,总会有小伙伴为了能够设置一下试图绕过一些验证机制. 不要试图绕过,因为你会遇到更 ...
- docker的学习总结
一 docker的8个使用场景1.简化配置虚拟机的最大好处是能在你的硬件设施上运行各种配置不一样的平台(软件, 系统), Docker在降低额外开销的情况下提供了同样的功能. 它能让你将运行环境和配置 ...
- Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient报错,问题排查
背景 最近在整合pyspark与hive,新安装spark-2.3.3以客户端的方式访问hive数据,运行方式使用spark on yarn,但是在配置spark读取hive数据的时候,这里直接把hi ...
- Ubuntu-18.04 LTS UEFI 安装U盘制作
要把U盘作为UEFI启动盘,第一个分区要为FAT32分区,EFI程序放在/EFI/Boot/bootx64.efi.为了制作Ubuntu-18.04 LTS安装U盘,可以把一个U盘格式化为FAT32格 ...
- centos 7.6 安装php70
1.首先查看是否有老版本 yum list installed | grep php 2.如果安装的有,清除老版本 yum remove php.x86_64 php-cli.x86_64 php-c ...
- layui.js---layer;;前端预览pdf
layui.js---layer;;前端预览pdf 1.必须引入layui.js 2.uul是pdf文件地址 3.触发function函数:小于号button onclick="pdfsee ...
- 部署python项目到linux服务器
最近用Python写了个外挂,需要部署到Linux环境的服务器上,由于之前本地开发时使用virtualenv,使用这个虚拟环境有个好处是项目中依赖的库不会是全局的,只在当前项目的目录下有效,因为我是M ...
- Python_列表操作2
1.使用sort()方法对列表进行永久性排序: colorsList=['hong','cheng','huang','lv'] colorsList.sort() #正序排序 print(color ...
- ISCC之Re2
硬核rust逆向 首先去学了一天rust...我TMD IDA打开,跟踪主函数 看一下伪代码,发现有一串密文 跟进去发现一串数据,猜测有可能是flag的加密数据,于是回头去分析算法 发现一个关键点 i ...