二次联通门 : luogu P3369 【模板】普通平衡树(Treap/SBT)

闲的没事,把各种平衡树都写写

比较比较。。。

下面是替罪羊树

#include <cstdio>
#include <vector> #define Max_ 100010 #define Inline __attri\
bute__( ( optimize( "-O2" ) ) ) Inline void read (int &now)
{
register char word = getchar ();
bool temp = false;
for (now = ; word < '' || word > ''; word = getchar ())
if (word == '-')
temp = true;
for (; word >= '' && word <= ''; now = now * + word - '', word = getchar ());
if (temp)
now = -now;
} const double alpha = 0.63; int N; struct G_D
{
G_D *child[]; int key;
int size, total;
bool is_exist; inline void Up ()
{
this->size = this->child[]->size + this->child[]->size + is_exist;
this->total = this->child[]->total + this->child[]->total + ;
} inline bool is_rebuild ()
{
return ((this->child[]->total > this->total * alpha + ) || (this->child[]->total > this->total * alpha + ));
}
}; class Scapegoat_Tree_Type
{ private : G_D poor_mem[Max_];
G_D *Root, *Tail, *null; G_D *reuse[Max_];
int Reuse_top; Inline G_D *New_Node (int key)
{
G_D *now = Reuse_top ? reuse[-- Reuse_top] : Tail ++;
now->child[] = now->child[] = null;
now->size = now->total = ;
now->is_exist = true;
now->key = key;
return now;
} Inline void Travel (G_D *now, std :: vector <G_D *> &line)
{
if (now == null)
return ;
Travel (now->child[], line);
if (now->is_exist)
line.push_back (now);
else
reuse[Reuse_top ++] = now;
Travel (now->child[], line);
} Inline G_D *Divide (std :: vector <G_D *> &line, int l, int r)
{
if (l >= r)
return null;
int Mid = (l + r) >> ;
G_D *now = line[Mid];
now->child[] = Divide (line, l, Mid);
now->child[] = Divide (line, Mid + , r);
now->Up ();
return now;
} Inline void Re_Build (G_D *&now)
{
static std :: vector <G_D *> line;
line.clear ();
Travel (now, line);
now = Divide (line, , line.size ());
} Inline G_D **Insert (G_D *&now, int key)
{
if (now == null)
{
now = New_Node (key);
return &null;
}
else
{
now->size ++;
now->total ++;
G_D **res = Insert (now->child[key >= now->key], key);
if (now->is_rebuild ())
res = &now;
return res;
}
} Inline void Erase (G_D *now, int pos)
{
now->size --;
int res = now->child[]->size + now->is_exist;
if (now->is_exist && pos == res)
{
now->is_exist = false;
return ;
}
else
{
if (pos <= res)
Erase (now->child[], pos);
else
Erase (now->child[], pos - res);
}
} public : Scapegoat_Tree_Type ()
{
Tail = poor_mem;
null = Tail ++;
null->child[] = null->child[] = null;
null->total = null->size = null->key = ; Root = null;
Reuse_top = ;
} Inline void Insert (int key)
{
G_D **now = this->Insert (Root, key);
if (*now != null)
Re_Build (*now);
} Inline int Get_Rank (int key)
{
G_D *now = Root;
register int Answer = ;
for (; now != null; )
{
if (now->key >= key)
now = now->child[];
else
{
Answer += now->child[]->size + now->is_exist;
now = now->child[];
}
}
return Answer;
} Inline int Get_kth_number (int k)
{
int Count = ;
for (G_D *now = Root; now != null; )
{
if (now->child[]->size + == k && now->is_exist)
return now->key;
else if (now->child[]->size >= k)
now = now->child[];
else
{
k -= now->child[]->size + now->is_exist;
now = now->child[];
}
}
} Inline void Erase (int pos)
{
Erase (Root, Get_Rank (pos));
if (Root->size < alpha * Root->total)
Re_Build (Root);
} }; Scapegoat_Tree_Type Tree; int M;
int main (int argc, char *argv[])
{ read (M); for (int type, x; M --; )
{
read (type);
read (x); switch (type)
{
case :
Tree.Insert (x);
break;
case :
Tree.Erase (x);
break;
case :
printf ("%d\n", Tree.Get_Rank (x));
break;
case :
printf ("%d\n", Tree.Get_kth_number (x));
break;
case :
printf ("%d\n", Tree.Get_kth_number (Tree.Get_Rank (x) - ));
break;
case :
printf ("%d\n", Tree.Get_kth_number (Tree.Get_Rank (x + )));
break;
}
} return ;
}

替罪羊树 ------ luogu P3369 【模板】普通平衡树(Treap/SBT)的更多相关文章

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

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

  2. 替罪羊树—BZOJ3224: Tyvj 1728 普通平衡树

    冬令营被平衡树坑了之后,打算苦练一番数据结构(QAQ). 先是打了一下想学好久的替罪羊树. 替罪羊树实现方法很简单,就是在不满足平衡条件的时候暴力重构子树. 调试小结: 1.删除操作分两类情况:如果某 ...

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

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

  4. 简析平衡树(一)——替罪羊树 Scapegoat Tree

    前言 平衡树在我的心目中,一直都是一个很高深莫测的数据结构.不过,由于最近做的题目的题解中经常出现"平衡树"这三个字,我决定从最简单的替罪羊树开始,好好学习平衡树. 简介 替罪羊树 ...

  5. BZOJ - 3224 Tyvj 1728 普通平衡树 (treap/树状数组)

    题目链接 treap及树状数组模板题. treap版: #include<bits/stdc++.h> using namespace std; typedef long long ll; ...

  6. 「BZOJ3600」没有人的算术 替罪羊树+线段树

    题目描述 过长--不想发图也不想发文字,所以就发链接吧-- 没有人的算术 题解 \(orz\)神题一枚 我们考虑如果插入的数不是数对,而是普通的数,这就是一道傻题了--直接线段树一顿乱上就可以了. 于 ...

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

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

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

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

  9. 平衡树简单教程及模板(splay, 替罪羊树, 非旋treap)

    原文链接https://www.cnblogs.com/zhouzhendong/p/Balanced-Binary-Tree.html 注意是简单教程,不是入门教程. splay 1. 旋转: 假设 ...

随机推荐

  1. vue3 父组件给子组件传值 provide & inject

    介绍 provide() 和 inject() 可以实现嵌套组件之间的数据传递. 这两个函数只能在 setup() 函数中使用. 父级组件中使用 provide() 函数向下传递数据. 子级组件中使用 ...

  2. [LOJ3048] [十二省联考2019] 异或粽子

    题目链接 LOJ:https://loj.ac/problem/3048 洛谷:https://www.luogu.org/problemnew/show/P5283 Solution 考虑每个子串都 ...

  3. C#的WebApi 与 EasyUi的DataGrid结合生成的可分页界面

    1.从数据库每次取出的数据为当前分页的数据. 2.分页用的是EasyUI 的 Pagination控件,与DataGrid是相对独立的. 3.后台数据获取是通过WebApi去获取. 4.传入参数是:p ...

  4. springboot打成jar包后无法解压

    springboot打成jar包后无法解压 Springboot打出来的jar,用压缩工具解压报错.Why? 先说解决办法. 1.解决办法 executable属性导致的,属性改成false后重新打包 ...

  5. C#学习笔记------参数

    一.形参 形参是本地变量,它声明在方法的参数列表中,而不是方法体中.

  6. UML类图的几种关系总结

    本文摘自:UML类图关系总结 在UML类图中,常见的有以下几种关系: 泛化(Generalization),  实现(Realization),关联(Association),聚合(Aggregati ...

  7. [LeetCode] 25. K 个一组翻转链表 ☆☆☆☆☆(链表)

    https://leetcode-cn.com/problems/reverse-nodes-in-k-group/solution/javadi-gui-fang-fa-100-by-chadriy ...

  8. ES5_对象 与 继承

    1. 对象的定义 //定义对象 function User(){ //在构造方法中定义属性 this.name = '张三'; this.age = 12; //在构造方法中定义方法: this.ru ...

  9. Linux系统下RAID5和RAID10的磁盘阵列配置

    前提了解:1988年由加利福尼亚大学伯克利分校发表的文章首次提到并定义了RAID,当今CPU性能每年可提升30%-50%但硬盘仅提升7%,渐渐的已经成为计算机整体性能的瓶颈,并且为了避免硬盘的突然损坏 ...

  10. Linux学习笔记之一

    基本命令 关机/重启 [root@allen ~]# [当前登录用户@主机名 当前所在目录]# 当前用户身份 #号表示管理员root $表示普通用户登录 如何关机 如何重启 系统硬件信息查看 关机命令 ...