二次联通门 : 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. [POJ3682]King Arthur's Birthday Celebration[期望DP]

    也许更好的阅读体验 \(\mathcal{Description}\) 每天抛一个硬币,硬币正面朝上的几率是p,直到抛出k次正面为止结束,第\(i\)天抛硬币的花费为\(2i-1\),求出抛硬币的天数 ...

  2. Spring Spring boot 获取IOC中的bean,ApplicationContext

    https://blog.csdn.net/weixin_38361347/article/details/89304414 https://www.jianshu.com/p/9ea13b00b1d ...

  3. 文件流CopyTo

  4. C#混音同时录制采集声卡和麦克风话筒

    在项目中,我们可能需要同时录制声卡的声音和麦克风的声音,比如直播间,在线教学.那么如何实现呢?当然是采用SharpCapture!下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第一步: ...

  5. 父元素设置min-height子元素设置100%问题

    问题:父元素设置min-height子元素高度设置100%取不到值,这是因为子元素 div设置 height:100%: 只有当父级元素满足min-height:1000px;设置的条件才触发: 浏览 ...

  6. 关于maven中版本控制问题

    之前我们说过Maven的版本分为快照和稳定版本,快照版本使用在开发的过程中,方便于团队内部交流学习.而所说的稳定版本,理想状态下是项目到了某个比较稳定的状态,这个稳定包含了源代码和构建都要稳定. ma ...

  7. MySQL Hardware--FIO压测

    FIO参数 .txt 支持文件系统或者裸设备,-filename=/dev/sda2或-filename=/dev/sdb direct= 测试过程绕过机器自带的buffer,使测试结果更真实 rw= ...

  8. wampserver环境配置局域网访问

    安装好wamp后,想用手机通过局域访问电脑上wamp下的网页,结果出现如下提示403错误: 第一步:找到 conf 这个文件: 找到下图中红色方框中的onlineoffline tag - don’t ...

  9. mysql学习之基础篇02

    我们来说一下表的增删改查的基本语法: 首先建立一个简单的薪资表: create table salary(id int primary key auto_increment,sname varchar ...

  10. ThreadStatic特性

    文章:ThreadStatic特性 地址:https://www.cnblogs.com/xuejietong/p/10997385.html 带有threadStaticAttribute标记的静态 ...