二次联通门 : 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. CF778D Parquet Re-laying 构造

    传送门 如果\(2 \not\mid M\),就把两个图折一下,把\(N\ M\)互换,这样就可以保证\(2 \mid M\). 因为操作可逆,所以我们可以选择一个中间状态,把起始和终点状态都变成这个 ...

  2. git下,输入git log 进入log 怎么退出

    解决方案: 英文状态下按Q就可以了 ctrl + c (应该是Linux命令中断的意思,很多中断都是这个命令). Paste_Image.png

  3. 安卓开发之常见Handler API和 定时器的使用

    package com.lidaochen.test; import android.os.Bundle; import android.os.Handler; import android.supp ...

  4. python 操作excel实现替换特定内容

    本文介绍使用python语言,借助openyxl库来实现操作excel(xlsx)文件,实现替换特定内容的需求. 目前实现了3个小功能: 1. 全字匹配替换(mode1):(如:全字匹配 yocich ...

  5. 几个不错的echarts +百度地图 案例

    https://echarts.baidu.com/examples/editor.html?c=map-polygon https://echarts.baidu.com/examples/edit ...

  6. python(字符串函数)

    一.字符串函数 1.首字母大小写 capitalize() title() name = "xinfangshuo" print (name.capitalize()) print ...

  7. gitlab自带的Nginx与原Nginx冲突的解决方案

    gitlab 推荐方案2 默认情况下,gitlab使用自带的Nginx,占用80端口,这样就与系统原本安装的Nginx冲突.导致其中一个nginx无法启动 我的gitlab可以正常启动,当再部署一个接 ...

  8. python之变量的数据类型(1)int 、bool 、str 及for循环运用

    一.变量的数据类型(1) 1.int 类型 int类型是整数,常用的有bit_length() 方法 用来返回一个数的二进制长度 2.bool类型 布尔型只有两个值 True,False 有关类型转换 ...

  9. 【转】关于TCP/IP,必须知道的十个知识点

    本文整理了一些TCP/IP协议簇中需要必知必会的十大问题,既是面试高频问题,又是程序员必备基础素养. 一.TCP/IP模型 TCP/IP协议模型(Transmission Control Protoc ...

  10. Redis锁机制的几种实现方式

    1. redis加锁分类 redis能用的的加锁命令分表是INCR.SETNX.SET 2. 第一种锁命令INCR 这种加锁的思路是, key 不存在,那么 key 的值会先被初始化为 0 ,然后再执 ...