二次联通门 : 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. 使用DOS命令登录管理员并添加账号管理员权限

    runas /user:administrator cmd Password: compmgmt.msc

  2. devextreme组装数据导出excel

    $.get("", function (grid_dataSource) { var grid_config = dxConfig.grid(grid_dataSource); g ...

  3. ADO.Net和SqlHelper封装

    1.什么是ADO.Net 简单来讲,ADO.NET是用于和数据源打交道的.Net结束,是一组向.NET程序员公开数据访问服务的类   2.ADO.NET的组成部分和对象模型 (1)ADO.NET的两个 ...

  4. Mybatis基于xml的动态sql实现

    动态sql可以很方便的拼接sql语句,主要用于复合条件查询: 主要通过这几个标签实现: if 标签: where 标签 choose标签: foreach标签: if 标签: <select i ...

  5. echart 人头

    <template> <div :class="className"> <div :id="id" class="spi ...

  6. [AIR] NativeExtension在IOS下的开发实例 --- Flex库项目的创建(二)

    来源:http://bbs.9ria.com/thread-102038-1-1.html 上一章,我已经介绍了如果创建IOS库文件,并定义了两个方法ShowIconBadageNumber和Init ...

  7. SAP云平台里的三叉戟应用

    大家第一次看到SAP MTA这个词组,会联想到什么? Jerry第一次看到的时候,联想到的是那一个个足坛著名的三叉戟攻击组合. 海皇波塞冬(Poseidon),奥林匹斯十二神中地位仅次于宙斯的大神,海 ...

  8. javascript_18-Array 数组

    数组 数组-引用类型,JavaScript中的内置对象 Array对象的属性 length 获取数组的长度(元素个数) 检测数组 instanceof Array.isArray() //h5新增 常 ...

  9. Scrum会议博客以及测试报告(β阶段)

    3组Alpha冲刺阶段博客目录 一.Scrum Meeting1. [第十周会议记录](链接地址:https://www.cnblogs.com/Cherrison-Time/articles/120 ...

  10. 小程序生命周期(onLaunch、onShow、onHide、onReady、onLoad、onUnloa)

    (1)onlaunch:当小程序初始化完成时,会触发 onLaunch(全局只触发一次)(app.js):(2)onLoad: 页面加载小程序注册完成后,加载页面,触发onLoad方法.一个页面只会调 ...