题目链接: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. Windows 7安装PlayReady出现“任务被禁用”错误信息

    问题描述: Windows 7的Windows media center中安装PlayReady时出现:错误信息:任务被禁用.(异常来自 HRESULT:0x80041326) 解决办法: 先请确认是 ...

  2. 工作采坑札记:4. Hadoop获取InputSplit文件信息

    1. 场景 基于客户的数据处理需求,客户分发诸多小数据文件,文件每行代表一条记录信息,且每个文件以"类型_yyyyMMdd_批次号"命名.由于同一条记录可能存在于多个文件中,且处于 ...

  3. Js常用的设计模式(1)——单例模式

    <Practical Common Lisp>的作者 Peter Seibel 曾说,如果你需要一种模式,那一定是哪里出了问题.他所说的问题是指因为语言的天生缺陷,不得不去寻求和总结一种通 ...

  4. java消息中间件 RocketMQ Linux安装与运行

    阿里巴巴宣布捐赠RocketMQ到Apache软件基金会孵化项目,最近闲下来便去部署了一个试验版本玩玩. 至于RockeMQ是什么,原理架构什么的这里就不赘述了,这里只记录安装过程. 一.系统环境 s ...

  5. asp 日期操作

    <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%> <% Response.Buffer=True Sessi ...

  6. 2、eclipse中使用Maven

    1.导入Maven项目 1.1从spring官网下载示例工程 访问Spring官网 点击[Browse the Guides]即可看到Spring官方为我们提供的很多Demo.

  7. vim右键粘贴 等杂

    putty连上linux,vim编辑个文件,我去,右键不能用用上下面的命令,就好了. set mouse-=a 今天发现mysql倒入utf-8的文件网站显示出来都是乱码,不过用utf-8的控制台看是 ...

  8. YCRefreshView-自定义支持上拉加载更多,下拉刷新。。。

    自定义支持上拉加载更多,下拉刷新,支持自由切换状态[加载中,加载成功,加载失败,没网络等状态]的控件,拓展功能[支持长按拖拽,侧滑删除]可以选择性添加 .具体使用方法,可以直接参考demo. 轻量级侧 ...

  9. 跨平台移动开发_PhoneGap 警告,通知,鸣叫,振动4 种通知类型

    创建鸣叫  使用 confirmation.beep 创建鸣叫 function playBeep() {     navigator.notification.beep(1); } 创建振动  使用 ...

  10. git clone 指定的单个目录或文件夹

    git clone 指定的单个目录或文件夹 针对自己的项目 方法一 基于sparse clone变通方法 创建一个空仓库 拉取远程仓库信息 开启 sparse clone 设置过滤 更新仓库 创建空仓 ...