【题目链接】

点击打开链接

【算法】

本题是Splay模板题,值得一做!

【代码】

#include<bits/stdc++.h>
using namespace std;
#define MAXN 100000 int N,opt,x; template <typename T> inline void read(T &x) {
int f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
} template <typename T> inline void write(T x) {
if (x < ) { putchar('-'); x = -x; }
if (x > ) write(x/);
putchar(x%+'');
} template <typename T> inline void writeln(T x) {
write(x);
puts("");
} struct Splay {
int root,total;
struct Node {
int fa,son[],val,cnt,size;
} Tree[MAXN+];
bool get(int x) {
return Tree[Tree[x].fa].son[] == x;
}
inline void new_node(int index,int f,int x) {
Tree[index].fa = f;
Tree[index].son[] = Tree[index].son[] = ;
Tree[index].val = x;
Tree[index].cnt = Tree[index].size = ;
}
inline void update(int index) {
Tree[index].size = Tree[index].cnt;
Tree[index].size += Tree[Tree[index].son[]].size;
Tree[index].size += Tree[Tree[index].son[]].size;
}
inline void rotate(int x) {
int f = Tree[x].fa,g = Tree[f].fa,
tmpx = get(x),tmpf = get(f);
if (!f) return;
Tree[f].son[tmpx] = Tree[x].son[tmpx^];
if (Tree[x].son[tmpx^]) Tree[Tree[x].son[tmpx^]].fa = f;
Tree[x].son[tmpx^] = f;
Tree[f].fa = x;
Tree[x].fa = g;
if (g) Tree[g].son[tmpf] = x;
update(f);
update(x);
}
inline void splay(int x) {
int f;
for (f = Tree[x].fa; (f = Tree[x].fa); rotate(x))
rotate((get(x) == get(f)) ? (f) : (x));
root = x;
}
inline void Insert(int x) {
int index = root;
bool tmp;
if (!root) {
new_node(++total,,x);
root = total;
return;
}
while (true) {
if (Tree[index].val == x) {
++Tree[index].cnt;
splay(index);
return;
}
tmp = Tree[index].val < x;
if (!Tree[index].son[tmp]) {
new_node(++total,index,x);
Tree[index].son[tmp] = total;
splay(total);
return;
} else index = Tree[index].son[tmp];
}
}
inline int query_max(int index) {
while (true) {
if (!Tree[index].son[]) return index;
index = Tree[index].son[];
}
}
inline int query_min(int index) {
while (true) {
if (!Tree[index].son[]) return index;
index = Tree[index].son[];
}
}
inline void join(int x,int y) {
int pos = query_max(x);
splay(pos);
Tree[pos].son[] = y;
Tree[y].fa = pos;
}
inline void erase(int x) {
int index = root;
bool tmp;
while (true) {
if (Tree[index].val == x) {
if (Tree[index].cnt > ) {
--Tree[index].cnt;
splay(index);
return;
}
splay(index);
break;
}
tmp = Tree[index].val < x;
index = Tree[index].son[tmp];
}
if ((!Tree[index].son[]) && (!Tree[index].son[])) {
root = ;
return;
}
if (!Tree[index].son[]) {
root = Tree[index].son[];
Tree[root].fa = ;
return;
}
if (!Tree[index].son[]) {
root = Tree[index].son[];
Tree[root].fa = ;
return;
}
join(Tree[index].son[],Tree[index].son[]);
}
inline int query_rank(int x) {
int index = root,ans=;
while (true) {
if (Tree[index].val <= x) {
ans += Tree[Tree[index].son[]].size;
if (Tree[index].val == x) {
splay(index);
return ans;
}
ans += Tree[index].cnt;
index = Tree[index].son[];
} else index = Tree[index].son[];
}
}
inline int rank(int x) {
int index = root;
while (true) {
if (x <= Tree[Tree[index].son[]].size) index = Tree[index].son[];
else {
x -= Tree[Tree[index].son[]].size;
if (x <= Tree[index].cnt) {
splay(index);
return Tree[index].val;
}
x -= Tree[index].cnt;
index = Tree[index].son[];
}
}
}
inline int pred(int x) {
int ans;
Insert(x);
ans = Tree[query_max(Tree[root].son[])].val;
erase(x);
return ans;
}
inline int succ(int x) {
int ans;
Insert(x);
ans = Tree[query_min(Tree[root].son[])].val;
erase(x);
return ans;
}
} T; int main() {
read(N);
while (N--) {
read(opt); read(x);
if (opt == ) T.Insert(x);
else if (opt == ) T.erase(x);
else if (opt == ) writeln(T.query_rank(x));
else if (opt == ) writeln(T.rank(x));
else if (opt == ) writeln(T.pred(x));
else if (opt == ) writeln(T.succ(x));
} return ; }

【BZOJ 3224】 普通平衡树的更多相关文章

  1. Luogu 3369 / BZOJ 3224 - 普通平衡树 - [无旋Treap]

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 https://www.luogu.org/problemnew/show/P3 ...

  2. BZOJ 3224 普通平衡树(Treap模板题)

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

  3. Luogu 3369 / BZOJ 3224 - 普通平衡树 - [替罪羊树]

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 https://www.luogu.org/problemnew/show/P3 ...

  4. BZOJ 3224 - 普通平衡树 - [Treap][Splay]

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3224 Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中 ...

  5. bzoj 3224 普通平衡树 vactor的妙用

    3224: Tyvj 1728 普通平衡树 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/ ...

  6. BZOJ 3224 普通平衡树

    这个是第一份完整的treap代码.嗯...虽然抄的百度的版,但还是不错的. !bzoj上不能用srand. #include<iostream>#include<cstdio> ...

  7. BZOJ 3224 普通平衡树(树状数组)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=3224 题意:维护以下操作:(1)插入x:(2)删除x(若有多个相同的数,只删除一个)(3 ...

  8. BZOJ 3224 普通平衡树 | 平衡树模板

    #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> # ...

  9. bzoj 3224 裸平衡树

    裸的平衡树,可以熟悉模板用,写题写不出来的时候可以A以下缓解下心情. /************************************************************** P ...

  10. BZOJ 3224: Tyvj 1728 普通平衡树

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 9629  Solved: 4091[Submit][Sta ...

随机推荐

  1. the import org.springframewok.test cannot be resolved

    在写Spring的单元测试时遇见了问题,注解@ContextConfiguration和SpringJUnit4ClassRunner.class无法导包.手动导包后错误为“the import or ...

  2. 《深入理解mybatis原理》 Mybatis初始化机制详解

    对于任何框架而言,在使用前都要进行一系列的初始化,MyBatis也不例外.本章将通过以下几点详细介绍MyBatis的初始化过程. 1.MyBatis的初始化做了什么 2. MyBatis基于XML配置 ...

  3. Apache Beam 传 大数据杂谈

    1月10日,Apache软件基金会宣布,Apache Beam成功孵化,成为该基金会的一个新的顶级项目,基于Apache V2许可证开源. 2003年,谷歌发布了著名的大数据三篇论文,史称三驾马车:G ...

  4. JS那些事儿——Gulp的入门使用

    前言 新人使用gulp的一个记录. 首先对于第一个新事物,我会问gulp这是什么? 答:gulp是一个自动化构建工具,它可以做一些自动化的任务,比如: 检查Javascript 编译Sass(或Les ...

  5. (type interface {}) to type string

    go 语言开发中,经常会在函数中碰到使用 insterface{} 作为接收任意参数,但是我们接收的数据经常是需要做类型转换,由于是初学者,因此,初次转换我是直接就 func New(paramete ...

  6. 关于android分享(sharedsdk的简单使用)

    老早就使用了.可是如今才写,惰性太大,如今改 如今做产品的话相信大家基本都做分享吧.一个是项目的需求须要,另一个是能够非常好的宣传自己的产品.其它的优点依据情况而论 事实上每一个平台都有它自己的文档, ...

  7. BMP文件的读取与显示

    有三个函数能够完毕这一功能 1.BitBlt    BitBlt 用于从原设备中复制位图到目标设备 void CMFCApplication1View::OnDraw(CDC* pDC) { CMFC ...

  8. Tomcat部署项目时出错java.lang.IllegalStateException: ContainerBase.addChild: start:org.apache.catalina.Life

    Tomcat部署项目时出错java.lang.IllegalStateException: ContainerBase.addChild: start:org.apache.catalina.Life ...

  9. LOCAL_CFLAGS参数说明

    1.-Wall 是打开警告开关 2.-O 代表默认优化,可选:-O0不优化,-O1低级优化,-O2中级优化,-O3高级优化,-Os代码空间优化 3.-g 是生成调试信息,生成的可执行文件具有和源代码关 ...

  10. C#模拟登录Facebook 实现发送消息、评论帖子

    由于目前电脑网页版FB实现模拟登录比较困难,本次选择了FB的手机版页面进行登录 MVC: private static string UserName = "用户名"; priva ...