题目链接: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. 安装cloudermanager时出现Acquiring installation lock问题(图文详解)

    不多说,直接上干货! 问题详情 解决办法 哪一个节点被锁就删除哪一个. 解决办法:进入/tmp 目录,ls -a查看,删除scm_prepare_node.*的文件,以及.scm_prepare_no ...

  2. python实现excel转json的例子

    python实现excel转json的例子(改进版) 由于数值策划给出数值是excel表格,但前台flash程序用的又是json格式.服务器也用了json格式,而json又是utf-8编码的,用C++ ...

  3. 使用awstat分析Nginx的访问日志

    在我的上一篇文章<使用 Nginx 提升网站访问速度>中介绍了 Nginx 这个 HTTP 服务器以及如何通过它来加速网站的访问速度.在实际的网站运营中,我们经常需要了解到网站的访问情况, ...

  4. java去掉String里面的空格、换行符等

    package com.ynet.utils; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * Create ...

  5. ES6 克隆对象

    浅克隆:只能克隆原始对象自身的值,不能克隆它继承的值 方法一: function clone(origin) { return Object.assign({}, origin); } 方法二: fu ...

  6. Redis的Lists数据类型

    Lists 就是链表,相信略有数据结构知识的人都应该能理解其结构.使用Lists结构,我们可以轻松地实现最新消息排行等功能.Lists的另一个应用就是消息队列,可以利用Lists的PUSH操作,将任务 ...

  7. 深入理解读写锁—ReadWriteLock源码分析

    转载:https://blog.csdn.net/qq_19431333/article/details/70568478 ReadWriteLock管理一组锁,一个是只读的锁,一个是写锁.读锁可以在 ...

  8. PAT 1066 Root of AVL Tree

    #include <cstdio> #include <cstdlib> class Node { public: Node* L; Node* R; int height; ...

  9. 采用 ITextPDF 类库测试向 PDF 中加入图片的示例

    package com.smbea.image; import com.artup.util.image.ImageUtil; import com.itextpdf.text.*; import c ...

  10. r.js压缩打包

    AMD模块化开发中的代码压缩打包工具——r.js 环境搭建基于nodejs:用于AMD模块化开发中的项目文件压缩打包,不是AMD模式也是可以的 javascript部分 压缩javascript项目开 ...