题干:

您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:
1. 插入x数
2. 删除x数(若有多个相同的数,因只删除一个)
3. 查询x数的排名(若有多个相同的数,因输出最小的排名)
4. 查询排名为x的数
5. 求x的前驱(前驱定义为小于x,且最大的数)
6. 求x的后继(后继定义为大于x,且最小的数)

Input

第一行为n,表示操作的个数,下面n行每行有两个数opt和x,opt表示操作的序号(1<=opt<=6)

Output

对于操作3,4,5,6每行输出一个数,表示对应答案

Sample Input

10 1 106465 4 1 1 317721 1 460929 1 644985 1 84185 1 89851 6 81968 1 492737 5 493598

Sample Output

106465 84185 492737

Hint

1.n的数据范围:n<=100000

2.每个数的数据范围:[-2e9,2e9]

解题报告:

Splay的大模板题。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX =100007;//= 2e5 + 5;
int fa[MAX],cnt[MAX],tr[MAX][2],size[MAX],val[MAX],tot,root;
inline bool get(int x) {return tr[fa[x]][1]==x;
}
inline void clear(int x) {fa[x]=cnt[x]=tr[x][0]=tr[x][1]=size[x]=val[x]=0;
}
inline void pushup(int x) {//更新x节点的信息 if(x == 0) return;//其实不加也行,因为不会去维护根节点的父节点的关系。size[x] = cnt[x];
//  if(tr[x][0]) size[x] += size[tr[x][0]] ;
//  if(tr[x][1]) size[x] += size[tr[x][1]] ;没啥用、、size[x] += size[tr[x][0]] + size[tr[x][1]];
}
inline void rotate(int x) {//以下注释默认我是我爸的左儿子 int old = fa[x],oldf = fa[old],w=get(x),ww=get(old);//修改我爸和我右儿子的关系 tr[old][w] = tr[x][w^1]; fa[tr[old][w]]=old;//修改我和我爸的关系 tr[x][w^1] = old;fa[old] = x;//修改我和我爷爷的关系 fa[x] = oldf;if(oldf) tr[oldf][ww] = x;pushup(old);pushup(x);
} inline void splay(int x) {for(int old=fa[x]; old = fa[x]; rotate(x)) {//默认根节点是0的情况下才可以这么用判断条件 if(fa[old]) {rotate(get(x) == get(old) ? old : x);}}root = x;
}
inline void insert(int x) {//x为权值 if(root == 0) {val[++tot] = x;root=tot;cnt[tot]=size[tot]=1;fa[tot] = tr[tot][0]=tr[tot][1]=0;return; }int cur = root,old = 0;while(1) {if(x == val[cur]) {cnt[cur]++;pushup(cur);pushup(old);splay(cur);return;}old = cur;cur = tr[cur][val[cur] < x];if(cur == 0) {val[++tot] = x;fa[tot] = old; tr[tot][0]=tr[tot][1]=0; tr[old][x>val[old]] = tot;//维护父节点和孩子节点 cnt[tot] = size[tot] = 1;pushup(old); splay(tot);return;}}
}
inline int pre(int x) {int cur = root,old=0;while(cur) {if(val[cur] < x) old = cur,cur = tr[cur][1];else cur = tr[cur][0]; }return old;
}
inline int nxt(int x) {int cur = root,old = 0;while(cur) {if(val[cur] > x) old = cur,cur = tr[cur][0];else cur = tr[cur][1];}return old;
}
//inline int pre() {
//  int cur = tr[root][0];
//  while(tr[cur][1]) cur = tr[cur][1];
//  return cur;
//}
//inline int nxt() {
//  int cur = tr[root][1];
//  while(tr[cur][0]) cur = tr[cur][0];
//  return cur;
//}inline int Rank(int x) {//查询x的Rank int cur = root,res = 0;while(1) {
//      if(cur == 0) return -1;//说明数据非法 if(x < val[cur]) cur = tr[cur][0];else {res += size[tr[cur][0]];if(x == val[cur]) {splay(cur); return res+1;}//此时x和树中的点重合,树中不允许有两个相同的点res += cnt[cur]; cur = tr[cur][1];}}
}
inline int kth(int x) {//查询排名为x的数的val int cur = root;while(1) {if(tr[cur][0] && x <= size[tr[cur][0]]) cur = tr[cur][0];else {int tmp = size[tr[cur][0]] + cnt[cur];if(x <= tmp) {splay(cur);return val[cur];}x -= tmp;cur = tr[cur][1];}}
}
inline void del(int x) {Rank(x);//找到x的排名并把它旋转上来if(cnt[root] > 1) {cnt[root]--;return;}if(!tr[root][0] && !tr[root][1]) root=0;//可加个clear();函数else if(!tr[root][0]) {root = tr[root][1];fa[root]=0;} //pushup(root);else if(!tr[root][1]) {root = tr[root][0];fa[root]=0;}//pushup(root);else {int leftbig = tr[root][0],oldrt=root;while(tr[leftbig][1]) leftbig = tr[leftbig][1];splay(leftbig);tr[root][1]=tr[oldrt][1];fa[tr[oldrt][1]]=root;pushup(root);}
}
//全程表示根节点不是0,但是根节点的父节点,我们认为是0 (fa[rt]==0)
int main()
{int n;cin>>n;for(int i = 1; i<=n; i++) {int op,x;scanf("%d%d",&op,&x);if(op == 1) insert(x);if(op == 2) del(x);if(op == 3) printf("%d\n",Rank(x));if(op == 4) printf("%d\n",kth(x));if(op == 5) printf("%d\n",val[pre(x)]);if(op == 6) printf("%d\n",val[nxt(x)]);}return 0 ;
}

总结:呜呜呜好难写、、、

【BZOJ - 3224】普通平衡树(Splay模板题)相关推荐

  1. [洛谷P3391] 文艺平衡树 (Splay模板)

    初识splay 学splay有一段时间了,一直没写...... 本题是splay模板题,维护一个1~n的序列,支持区间翻转(比如1 2 3 4 5 6变成1 2 3 6 5 4),最后输出结果序列. ...

  2. BZOJ 3224 普通平衡树 treap or vector

    很明显这是一道treap的题,但看了黄学长的博客后,也让我大开了眼界,没想到vector也能用那么短的编码量把这道题AC,着实令我敬佩.这也提醒了我 STL 的重要性. 的确, 对于C++ 选手来说, ...

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

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

  4. BZOJ 3223: Tyvj 1729 文艺平衡树-Splay树(区间翻转)模板题

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 6881  Solved: 4213 [Submit][S ...

  5. 【模板篇】splay(填坑)+模板题(普通平衡树)

    划着划着水一不小心NOIP还考的凑合了- 所以退役的打算要稍微搁置一下了- 要准备准备省选了-. 但是自己已经啥也不会了- 所以只能重新拾起来- 从splay开始吧- splay我以前扔了个板子来着, ...

  6. BZOJ 3224: Tyvj 1728 普通平衡树【Treap】

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MB Description 您需要写一种数据结构(可参考题目标题),来维护一些数 ...

  7. Duan2baka的Splay模板!(二叉搜索树)

    BZOJ[3224] Tyvj 1728 普通平衡树 题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3224 用Splay搞了一发-. (以 ...

  8. Splay(splay模板)

    题目描述 给定一个长度为 n 的整数序列,初始时序列为 {1,2,-,n−1,n}. 序列中的位置从左到右依次标号为 1∼n. 我们用 [l,r] 来表示从位置 l 到位置 r 之间(包括两端点)的所 ...

  9. 平衡树(Splay) 服务:第一弹——旋转的艺术

    平衡树(Splay) 服务:第一弹--旋转的艺术 0.前言 本蒟蒻前不久刚学SPLAY,有了一点心得,想要巩固下来.同时也觉得网上的神犇们实在太强了,有的内容并不能很好的让我这样的蒟蒻理解,因此便有了 ...

最新文章

  1. 2018-3-10论文(网络评论非结构化信息表示与应用研究)笔记-----基于证据理论的综合评价模型建立
  2. Linux下软件安装方法汇总
  3. 正则表达式 字符转义
  4. oracle导入dmp文件报错12154,oracle表空间的创建及dmp 文件的导入(推荐)
  5. HiveSQL常用数据处理语句
  6. java 浮点数精度_Java中浮点数精度问题
  7. 【大话Hibernate】hibernate缓存详解
  8. Mach 微内核的命名趣闻
  9. JZOJ5857 【NOIP提高组模拟A组2018.9.8】没有上司的舞会
  10. 进程间通信之信号he信号量
  11. 基于GraphQL的数据网关实现
  12. Redis的读写分离
  13. java软件工程师 英文简历_java工程师英文简历范文
  14. 打补丁是什么意思?如何快速对云主机批量打补丁?用什么软件?
  15. 目标检测——小目标检测问题
  16. 虚拟机DEDECMS织梦建站
  17. 【算法Algorithm】计数(Count)排序
  18. STC+Andriod+ESP8266制作手机遥控小车
  19. 7,词根 - 倒、流
  20. 注册.io域名有什么好处?

热门文章

  1. ecshop 函数列表大全
  2. JS的手写TRIM函数
  3. [密码学基础][每个信息安全博士生应该知道的52件事][Bristol Cryptography][第8篇]交互式的定义如何帮助计算和IP类问题是什么
  4. 力扣3. 无重复字符的最长子串 two pointer算法|滑动窗口|尺取法
  5. wordpress functions.php 在哪,在functions.php中定义变量并在WordPress中的函数钩子中访问它们...
  6. python条件表达式三门课至少有一门及格_Python/ MySQL练习题(一)
  7. 如何调度spark程序_如何定时,周期性的运行程序?Python APScheduler实现任务灵活调度...
  8. python爬虫知乎图片_Python爬虫入门教程 25-100 知乎文章图片爬取器之一
  9. matlab 矢量化,matlab矢量化编程简要
  10. qt for v210