二次联通门 : 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)的更多相关文章

  1. luoguP3369[模板]普通平衡树(Treap/SBT) 题解

    链接一下题目:luoguP3369[模板]普通平衡树(Treap/SBT) 平衡树解析 #include<iostream> #include<cstdlib> #includ ...

  2. 【模板】平衡树——Treap和Splay

    二叉搜索树($BST$):一棵带权二叉树,满足左子树的权值均小于根节点的权值,右子树的权值均大于根节点的权值.且左右子树也分别是二叉搜索树.(如下) $BST$的作用:维护一个有序数列,支持插入$x$ ...

  3. [luogu P3369]【模板】普通平衡树(Treap/SBT)

    [luogu P3369][模板]普通平衡树(Treap/SBT) 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除x数(若有多个相同的数,因只删 ...

  4. 红黑树 ------ luogu P3369 【模板】普通平衡树(Treap/SBT)

    二次联通门 : luogu P3369 [模板]普通平衡树(Treap/SBT) 近几天闲来无事...就把各种平衡树都写了一下... 下面是红黑树(Red Black Tree) 喜闻乐见拿到了luo ...

  5. 替罪羊树 ------ luogu P3369 【模板】普通平衡树(Treap/SBT)

    二次联通门 : luogu P3369 [模板]普通平衡树(Treap/SBT) 闲的没事,把各种平衡树都写写 比较比较... 下面是替罪羊树 #include <cstdio> #inc ...

  6. 洛谷P3369 【模板】普通平衡树(Treap/SBT)

    洛谷P3369 [模板]普通平衡树(Treap/SBT) 平衡树,一种其妙的数据结构 题目传送门 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除 ...

  7. AC日记——【模板】普通平衡树(Treap/SBT) 洛谷 P3369

    [模板]普通平衡树(Treap/SBT) 思路: 劳资敲了一个多星期: 劳资终于a了: 劳资一直不a是因为一个小错误: 劳资最后看的模板: 劳资现在很愤怒: 劳资不想谈思路!!! 来,上代码: #in ...

  8. P3369 【模板】普通平衡树 Treap

    P3369 [模板]普通平衡树(Treap/SBT) 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除x数(若有多个相同的数,因只删除一个) 查询 ...

  9. luogu P3919 [模板]可持久化数组(可持久化线段树/平衡树)(主席树)

    luogu P3919 [模板]可持久化数组(可持久化线段树/平衡树) 题目 #include<iostream> #include<cstdlib> #include< ...

随机推荐

  1. Pycharm专业版配置远程服务器并自动同步代码

    一.使用场景 如果每次都在本机上面写代码,然后传到服务器上面,在服务器上面运行就太麻烦了.这样的方式十分繁琐,效率很低. 因此,希望可以像下面一样操作: 可以直接在本机上码代码 自动将代码同步到远程服 ...

  2. 关于梯度下降之前需要进行feature scale的记录

    先上吴恩达老师的课件图 1.对于梯度下降而言,学习率很大程度上影响了模型收敛的速度.对于不同规模的特征,如果采用相同的学习率,那么如果学习率适应于scale大的数据,scale较小的数据由于学习率过大 ...

  3. (三) Docker 常用操作与CentOS7 防火墙命令

    参考并感谢 Docker 常用命令 https://docs.docker.com/engine/reference/commandline/docker/ Docker 登录docker账户 doc ...

  4. 利用脚本一键执行脚本,创建SharePoint文档库列表

    SharePoint基于文档库和列表上进行二次开发,生成新的文档库和新的列表模板 通过新的模板,创建新的文档库与列表 --定义site对象$site = SPSite http://dvt176/si ...

  5. python day4 元组/字典/集合类知识点补充

    目录 python day4 元组/字典/集合类知识点补充 1. 元组tuple知识点补充 2. 字典dict的知识点补充 3. 基本数据类型set 4. 三元运算,又叫三目运算 5. 深复制浅复制 ...

  6. C# 后台获取GridView列表的值

    int rowIndex = ((GridViewRow)((Button)sender).NamingContainer).RowIndex;//获取gridview中的行号            ...

  7. django路由的反向解析

    什么是路由的反向解析 我们的路由都是一个匹配关系,对应一个处理的视图函数, 如果我们的匹配关系发生了变化,那么与之对应的访问地址(可能前端直接url链接, 也可能是后端的redrict跳转)都需要跟着 ...

  8. [LeetCode] 5. 最长回文子串 ☆☆☆(最长子串、动态规划)

    最长回文子串 (动态规划法.中心扩展算法) https://leetcode-cn.com/problems/longest-palindromic-substring/solution/xiang- ...

  9. Python学习日记(二十五) 接口类、抽象类、多态

    接口类 继承有两种用途:继承基类的方法,并且做出自己的改变或扩展(代码重用)和声明某个子类兼容于某基类,定义一个接口类interface,接口类中定义了一些接口名(就是函数名)且并未实现接口的功能,子 ...

  10. SQL SERVER-Alwayson原理

    流程 1.异步提交模式 主副本无须确认该副本已经完成日志固化,就可提交事务. 主副本不受辅助副本的影响 辅助副本上的DB处于SYNCHRONIZING 2.同步提交模式 主副本要确认副本已经完成日志固 ...