【题目链接】

点击打开链接

【算法】

本题是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. T3054 高精度练习-文件操作 codevs

    http://codevs.cn/problem/3054/ 题目描述 Description   输入一组数据,将每个数据加1后输出 输入描述 Input Description 输入数据:两行,第 ...

  2. WebGIS开发之用openlayers加载离线百度地图

    因为项目需要,只有内网环境,没有外网环境,所以需要下载地图瓦片. 一.下载瓦片地图 这个可以自行在网上找一些地图瓦片下载器,下好的瓦片地图是分级的.大概如图这种类型. 二.在地图上显示标记 首先使用o ...

  3. Maven项目配置外部依赖(本地依赖)

    加入有一些公共jar包只限于公司内部使用,不能暴露在外部时,有如下的方案: 1.搭建私有远程仓库(nexus) 2.使用http.ftp.共享地址,github地址等(主要是通过maven-deplo ...

  4. 基于R-Tree的最近邻查询

    转自基于R-Tree的最近邻查询 BAB(Branch.and.Band)算法是由Nick Roussopoulousnl等人于1995年提出的,是最早的基于R.树的静态最近邻查询算法.该算法使用MI ...

  5. Java中的Copy-on-Write容器 & ConcurrentHashMap & HashTable比较

    参考这篇文章:Link 从JDK1.5开始Java并发包里提供了两个使用CopyOnWrite机制实现的并发容器,它们是CopyOnWriteArrayList和CopyOnWriteArraySet ...

  6. C#语言 循环语句

    //Console.Write("请输入关卡数:"); //int a = int.Parse(Console.ReadLine()); //int s = 0; //if (a ...

  7. Elasticsearch 之 慘痛部署(分片移位)

    部署说明 硬件 server两台: 机器A:64G内存 机器B:32G内存 分片 共12个节点 2个查询节点.10个存储节点 8个主分片 1个复制分片(每一个分片都有一个副本分布在不同的节点上面) 每 ...

  8. 作为iOS程序员,最核心的60%能力有哪些?

    作为iOS程序员,最核心的60%能力有哪些?   一个合格的iOS程序员需要掌握多少核心技能?你和专业的开发工程师的差距有多大?你现在的水平能开发一个功能完整性能高效的iOS APP吗?一起来看看下面 ...

  9. 微软开源 Try .NET - 创建交互式.NET文档

    微软近日开源了一个新平台--Try .NET,该平台可以让开发者在线上编写并运行 .NET 代码.微软介绍,Try .NET 是一个可嵌入的代码运行器,不仅可以直接在线上对自己或者他人的代码进行编辑. ...

  10. Arcgis Engine(ae)接口详解(7):地图(map)操作

    IMap map = null; //跟map同一层次的activeView对象,他们都是“地图”的对象,map管理地图内容,activeView管理显示内容 IActiveView activeVi ...