题目链接:https://www.luogu.org/problemnew/show/P3369

#include <cstdio>
#include <algorithm>
#include <iostream>
#define ri register
#define il inline
using namespace std;
const int maxn = 1000000;
struct RNG{
int fa, cnt, v, sub, son[2];
}e[maxn];
int root, n, m, whole_size;
il void Clear(int x){
e[x].cnt = e[x].fa = e[x].son[0] = e[x].son[1] = e[x].sub = e[x].v = 0;
}
il bool Get_which(int x){
return e[e[x].fa].son[1] == x;
}
il void update(int x){
if(x){
e[x].sub = e[x].cnt;
if(e[x].son[0]) e[x].sub += e[e[x].son[0]].sub;
if(e[x].son[1]) e[x].sub += e[e[x].son[1]].sub;
}
return;
}
il void rotate(int x){
int father = e[x].fa, get_father = e[father].fa, which_son = Get_which(x);
e[father].son[which_son] = e[x].son[which_son^1];
e[e[father].son[which_son]].fa = father;
e[x].son[which_son^1] = father;
e[father].fa = x;
e[x].fa = get_father;
if(get_father){
e[get_father].son[e[get_father].son[1]==father] = x;
}
update(father);
update(x);
return;
}
il void splay(int x){
for(int fa; fa = e[x].fa; rotate(x))
if(e[fa].fa)
rotate((Get_which(x) == Get_which(fa)) ? fa : x);
root = x;
return;
}
il void insert(int x){
if(!root){
whole_size++;
e[whole_size].son[0] = e[whole_size].son[1] = e[whole_size].fa = 0;
root = whole_size;
e[whole_size].sub = e[whole_size].cnt++;
e[whole_size].v = x;
return;
}
int now = root, fa = 0;
while(1)
{
if(x == e[now].v){
e[now].cnt++;
update(now);
update(fa);
splay(now);
break;
}
fa = now;
now = e[now].son[e[now].v < x];
if(!now){
whole_size++;
e[whole_size].son[0] = e[whole_size].son[1] = 0;
e[whole_size].fa = fa;
e[whole_size].sub = e[whole_size].cnt = 1;
e[fa].son[e[fa].v<x] = whole_size;
e[whole_size].v = x;
update(fa);
splay(whole_size);
break;
}
}
return;
}
il int Find_num(int x){
int now = root;
while(1){
if(e[now].son[0]&&x<=e[e[now].son[0]].sub)
now = e[now].son[0];
else{
int temp = (e[now].son[0]?e[e[now].son[0]].sub:0)+e[now].cnt;
if(x <= temp) return e[now].v;
x-=temp;
now = e[now].son[1];
}
}
}
il int Find_rank(int x){
int now = root, ans = 0;
while(1){
if(x<e[now].v)
now = e[now].son[0];
else{
ans += (e[now].son[0]?e[e[now].son[0]].sub:0);
if(x == e[now].v){
splay(now); return ans+1;
}
ans += e[now].cnt;
now = e[now].son[1];
}
}
}
il int Find_pre(){
int now = e[root].son[0];
while(e[now].son[1]) now = e[now].son[1];
return now;
}
il int Find_suffix(){
int now = e[root].son[1];
while(e[now].son[0]) now = e[now].son[0];
return now;
}
il void Delete(int x)
{
int hhh = Find_rank(x);
if(e[root].cnt>1){
e[root].cnt--;
update(root);
return;
}
if(!e[root].son[0]&&!e[root].son[1]){
Clear(root);
root = 0;
return;
}
if(!e[root].son[0]){
int old_root = root;
root = e[root].son[1];
e[root].fa = 0;
Clear(old_root);
return;
}
else if(!e[root].son[1]){
int old_root = root;
root = e[root].son[0];
e[root].fa = 0;
Clear(old_root);
return;
}
int left_max = Find_pre(), old_root = root;
splay(left_max);
e[root].son[1] = e[old_root].son[1];
e[e[old_root].son[1]].fa = root;
Clear(old_root);
update(root);
return ;
}
int main(){
int m, num, be_dealt;
scanf("%d",&m);
for(int i = 1; i <= m; i++){
scanf("%d%d",&num,&be_dealt);
if(num == 1){insert(be_dealt);}
if(num == 2){Delete(be_dealt);}
if(num == 3){printf("%d\n",Find_rank(be_dealt));}
if(num == 4){printf("%d\n",Find_num(be_dealt));}
if(num == 5){
insert(be_dealt);
printf("%d\n",e[Find_pre()].v);
Delete(be_dealt);}
if(num == 6){
insert(be_dealt);
printf("%d\n",e[Find_suffix()].v);
Delete(be_dealt);
}
}
return 0;
}

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

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

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

  2. BZOJ 3224 TYVJ 1728 普通平衡树 [Treap树模板]

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 7390  Solved: 3122 [Submit][S ...

  3. [洛谷P3369] 普通平衡树 Treap & Splay

    这个就是存一下板子...... 题目传送门 Treap的实现应该是比较正经的. 插入删除前驱后继排名什么的都是平衡树的基本操作. #include<cstdio> #include< ...

  4. 数组splay ------ luogu P3369 【模板】普通平衡树(Treap/SBT)

    二次联通门 : luogu P3369 [模板]普通平衡树(Treap/SBT) #include <cstdio> #define Max 100005 #define Inline _ ...

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

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

  6. 替罪羊树 ------ luogu P3369 【模板】普通平衡树(Treap/SBT)

    二次联通门 : luogu P3369 [模板]普通平衡树(Treap/SBT) 闲的没事,把各种平衡树都写写 比较比较... 下面是替罪羊树 #include <cstdio> #inc ...

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

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

  8. 洛谷P3369 【模板】普通平衡树(Treap/SBT)

    洛谷P3369 [模板]普通平衡树(Treap/SBT) 平衡树,一种其妙的数据结构 题目传送门 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除 ...

  9. AC日记——【模板】普通平衡树(Treap/SBT) 洛谷 P3369

    [模板]普通平衡树(Treap/SBT) 思路: 劳资敲了一个多星期: 劳资终于a了: 劳资一直不a是因为一个小错误: 劳资最后看的模板: 劳资现在很愤怒: 劳资不想谈思路!!! 来,上代码: #in ...

随机推荐

  1. Unity3D游戏轻量级xlua热修复框架

    Unity3D游戏轻量级xlua热修复框架   一 这是什么东西 前阵子刚刚集成xlua到项目,目的只有一个:对线上游戏C#逻辑有Bug的地方执行修复,通过考察xlua和tolua,最终选择了xlua ...

  2. 用R处理不平衡的数据

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文来自云+社区翻译社,作者ArrayZoneYour 在分类问题当中,数据不平衡是指样本中某一类的样本数远大于其他的类别样本数.相比于多分 ...

  3. Cannot connect to VM com.sun.jdi.connect.TransportTimeoutException

    Cannot connect to VM com.sun.jdi.connect.TransportTimeoutException 描述 在用eclipse进行debug的时候弹出了上面的错误,在s ...

  4. 操蛋的Django model------select_related() 主要用于一对一和一对多

    实例: 创建表,表都是一对一,一对多 class Province(models.Model): name = models.CharField(max_length=10) class City(m ...

  5. bzoj 5329: [Sdoi2018]战略游戏

    Description 省选临近,放飞自我的小Q无心刷题,于是怂恿小C和他一起颓废,玩起了一款战略游戏. 这款战略游戏的地图由n个城市以及m条连接这些城市的双向道路构成,并且从任意一个城市出发总能沿着 ...

  6. C#请求http post和get

    首先先要感谢博主小伟地方提供的博客,让我解决了问题. 同样是先提问题,我们要请求http干什么? 通过请求http,传入我的参数,我希望能够获取到项目里面的某些数据,比如这里,我们需要得到SceneL ...

  7. log4net日记文件路径动态配置

    在项目开发过程中,部署的服务器越来越多,查看日记的时候需要每台服务器去找日记看,这对运维人员来说是一个很不友好的方式.在此基础上就提出将所有日记统一到一台服务器上进行存放,并按照产生日记的服务器分文件 ...

  8. Portals

    Portals Portals 提供了一种很好的将子节点渲染到父组件以外的 DOM 节点的方式. const appRoot = document.getElementById('app-root') ...

  9. typeScript入门(三)接口

      接口我感觉是很常用的一块 定义标准: 接口的作用:在面向对象的编程中,接口是一种规范的定义,它定义了行为和动作的规范,在程序设计里面,接口起到一种限制和规范的作用.接口定义了某一批类所需要遵守的规 ...

  10. 使用Anaconda管理环境

    Anaconda指的是一个开源的python发行版本,其包含了conda.Python等180多个科学包及其依赖项. Anaconda是一个开源的包.环境管理器,可以用于在同一个机器上安装不同版本的软 ...