替罪羊树 ------ luogu P3369 【模板】普通平衡树(Treap/SBT)
二次联通门 : 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)的更多相关文章
- luoguP3369[模板]普通平衡树(Treap/SBT) 题解
链接一下题目:luoguP3369[模板]普通平衡树(Treap/SBT) 平衡树解析 #include<iostream> #include<cstdlib> #includ ...
- 替罪羊树—BZOJ3224: Tyvj 1728 普通平衡树
冬令营被平衡树坑了之后,打算苦练一番数据结构(QAQ). 先是打了一下想学好久的替罪羊树. 替罪羊树实现方法很简单,就是在不满足平衡条件的时候暴力重构子树. 调试小结: 1.删除操作分两类情况:如果某 ...
- 【模板】平衡树——Treap和Splay
二叉搜索树($BST$):一棵带权二叉树,满足左子树的权值均小于根节点的权值,右子树的权值均大于根节点的权值.且左右子树也分别是二叉搜索树.(如下) $BST$的作用:维护一个有序数列,支持插入$x$ ...
- 简析平衡树(一)——替罪羊树 Scapegoat Tree
前言 平衡树在我的心目中,一直都是一个很高深莫测的数据结构.不过,由于最近做的题目的题解中经常出现"平衡树"这三个字,我决定从最简单的替罪羊树开始,好好学习平衡树. 简介 替罪羊树 ...
- BZOJ - 3224 Tyvj 1728 普通平衡树 (treap/树状数组)
题目链接 treap及树状数组模板题. treap版: #include<bits/stdc++.h> using namespace std; typedef long long ll; ...
- 「BZOJ3600」没有人的算术 替罪羊树+线段树
题目描述 过长--不想发图也不想发文字,所以就发链接吧-- 没有人的算术 题解 \(orz\)神题一枚 我们考虑如果插入的数不是数对,而是普通的数,这就是一道傻题了--直接线段树一顿乱上就可以了. 于 ...
- [luogu P3369]【模板】普通平衡树(Treap/SBT)
[luogu P3369][模板]普通平衡树(Treap/SBT) 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除x数(若有多个相同的数,因只删 ...
- 红黑树 ------ luogu P3369 【模板】普通平衡树(Treap/SBT)
二次联通门 : luogu P3369 [模板]普通平衡树(Treap/SBT) 近几天闲来无事...就把各种平衡树都写了一下... 下面是红黑树(Red Black Tree) 喜闻乐见拿到了luo ...
- 平衡树简单教程及模板(splay, 替罪羊树, 非旋treap)
原文链接https://www.cnblogs.com/zhouzhendong/p/Balanced-Binary-Tree.html 注意是简单教程,不是入门教程. splay 1. 旋转: 假设 ...
随机推荐
- 深入浅出CAS
后端开发中大家肯定遇到过实现一个线程安全的计数器这种需求,根据经验你应该知道我们要在多线程中实现 共享变量 的原子性和可见性问题,于是锁成为一个不可避免的话题,今天我们讨论的是与之对应的无锁 CAS. ...
- jmeter_分布式测试
背景: 由于Jmeter本身的瓶颈,当需要模拟数以千计的并发用户时,使用单台机器模拟所有的并发用户就有些力不从心,甚至还会引起JAVA内存溢出的错误.要解决这个问题,可以使用分布式实测 ...
- Hadoop2-认识Hadoop大数据处理架构-单机部署
一.Hadoop原理介绍 1.请参考原理篇:Hadoop1-认识Hadoop大数据处理架构 二.centos7单机部署hadoop 前期准备 1.创建用户 [root@web3 ~]# useradd ...
- springboot中常用的依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...
- PHP导出XML格式的EXCEL
<?php function Export(){ set_time_limit(0); ob_start(); $biz = new ZaikuBiz(); $biz->setSearch ...
- jenkins配置Webhook-gitlab
1.Jenkins 安装完成以后,首先我们在Jenkins中需要安装一下,Gitlab Hook Plugin,GitLab Plugin,Gitlab Authentication plugin插件 ...
- Html form表单大全(一)
在前后端交互的过程中,除了ajax请求之外,最常见的就是表单请求了. 由于form表单属性多,表单标签内容多且复杂,不深究的话很难全面的弄明白. 接下来就来详细的说一说整个form表单都有些什么,并且 ...
- 将H5页面打包成安卓原生app
第一步:下载HBuilderX,新建项目选择5+App新建一个空项目如下图 新建后项目目录结构如下图 第二步,将你要打包成安卓app的文件打包,最后生成的文件目录如下图 1.打包完成后,将对应文件内容 ...
- 增强学习--TRPO算法
理论部分参考 推导 数学上的分析 代码
- iOS静态库相关-封装lib
来源:http://blog.csdn.net/zsomsom/article/details/9163635 Library介绍 基本知识 在实际的编程过程中,通常会把一些公用函数制成函数库,供其它 ...