【模板】普通平衡树 Splay
题目描述
您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:
- 插入xxx数
- 删除xxx数(若有多个相同的数,因只删除一个)
- 查询xxx数的排名(排名定义为比当前数小的数的个数+1+1+1。若有多个相同的数,因输出最小的排名)
- 查询排名为xxx的数
- 求xxx的前驱(前驱定义为小于xxx,且最大的数)
- 求xxx的后继(后继定义为大于xxx,且最小的数)
输入输出格式
输入格式:
第一行为nnn,表示操作的个数,下面nnn行每行有两个数optoptopt和xxx,optoptopt表示操作的序号( 1≤opt≤6 1 \leq opt \leq 6 1≤opt≤6 )
输出格式:
对于操作3,4,5,63,4,5,63,4,5,6每行输出一个数,表示对应答案
输入输出样例
106465
84185
492737
说明
时空限制:1000ms,128M
1.n的数据范围: n≤100000 n \leq 100000 n≤100000
2.每个数的数据范围: [−107,107][-{10}^7, {10}^7][−107,107]
来源:Tyvj1728 原名:普通平衡树
在此鸣谢
Splay Tree 即可;而且好写;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 400005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ int rt, n, tot = 0; struct node {
int ch[2];
int ff;
int cnt;
int val;
int son;
}e[maxn<<1]; void pushup(int u) {
e[u].son = e[e[u].ch[0]].son + e[e[u].ch[1]].son + e[u].cnt;
} void rotate(int x) {
int y = e[x].ff;
int z = e[y].ff;
int k = (e[y].ch[1] == x);
e[z].ch[e[z].ch[1] == y] = x; e[x].ff = z;
e[y].ch[k] = e[x].ch[k ^ 1];
e[e[x].ch[k ^ 1]].ff = y;
e[x].ch[k ^ 1] = y; e[y].ff = x;
pushup(y); pushup(x);
} void splay(int x, int aim) {
while (e[x].ff != aim) {
int y = e[x].ff;
int z = e[y].ff;
if (z != aim) {
(e[y].ch[0] == x) ^ (e[z].ch[0] == y) ? rotate(x) : rotate(y);
}
rotate(x);
}
if (aim == 0)rt = x;
} void ins(int x) {
int u = rt, ff = 0;
while (u&&e[u].val != x) {
ff = u;
u = e[u].ch[x > e[u].val];
}
if (u)e[u].cnt++;// 已经存在
else {
u = ++tot;// 总的节点数目
if (ff)e[ff].ch[x > e[ff].val] = u;
e[tot].ch[0] = 0; e[tot].ch[1] = 0;
e[tot].ff = ff; e[tot].val = x;
e[tot].cnt = 1; e[tot].son = 1;
}
splay(u, 0);
} void Find(int x) {
// 查找x的位置
int u = rt;
if (u == 0)return;
while (e[u].ch[x > e[u].val] && x != e[u].val) {
u = e[u].ch[x > e[u].val];
}
splay(u, 0);
} int nxt(int x, int f) {
Find(x);
int u = rt;
if ((e[u].val > x&&f) || (e[u].val < x && !f))return u;
u = e[u].ch[f];
while (e[u].ch[f ^ 1])u = e[u].ch[f ^ 1];
return u;
} void del(int x) {
int last = nxt(x, 0);
int Next = nxt(x, 1);
splay(last, 0); splay(Next, last);
int delt = e[Next].ch[0];
if (e[delt].cnt > 1) {
e[delt].cnt--; splay(delt, 0);
}
else e[Next].ch[0] = 0;
} int k_th(int x) {
// rank==x
int u = rt;
if (e[u].son < x)return false;
while (1) {
int y = e[u].ch[0];
if (x > e[y].son + e[u].cnt) {
x -= e[y].son + e[u].cnt;
u = e[u].ch[1];
}
else if (e[y].son >= x)u = y;
else return e[u].val;
}
} int main()
{
//ios::sync_with_stdio(0);
ins(inf); ins(-inf);
rdint(n);
for (int i = 0; i < n; i++) {
int op; rdint(op);
int x;
if (op == 1) {
rdint(x); ins(x);
}
if (op == 2) {
rdint(x); del(x);
}
if (op == 3) {
rdint(x); Find(x);
cout << e[e[rt].ch[0]].son << endl;
}
if (op == 4) {
rdint(x);
cout << k_th(x+1) << endl;
}
if (op == 5) {
rdint(x);
cout << e[nxt(x, 0)].val << endl;
}
if (op == 6) {
rdint(x);
cout << e[nxt(x, 1)].val << endl;
}
}
return 0;
}
【模板】普通平衡树 Splay的更多相关文章
- luoguP3391[模板]文艺平衡树(Splay) 题解
链接一下题目:luoguP3391[模板]文艺平衡树(Splay) 平衡树解析 这里的Splay维护的显然不再是权值排序 现在按照的是序列中的编号排序(不过在这道题目里面就是权值诶...) 那么,继续 ...
- 洛谷.3369.[模板]普通平衡树(Splay)
题目链接 第一次写(2017.11.7): #include<cstdio> #include<cctype> using namespace std; const int N ...
- 洛谷.3391.[模板]文艺平衡树(Splay)
题目链接 //注意建树 #include<cstdio> #include<algorithm> const int N=1e5+5; //using std::swap; i ...
- 【阶梯报告】洛谷P3391【模板】文艺平衡树 splay
[阶梯报告]洛谷P3391[模板]文艺平衡树 splay 题目链接在这里[链接](https://www.luogu.org/problemnew/show/P3391)最近在学习splay,终于做对 ...
- 【模板篇】splay(填坑)+模板题(普通平衡树)
划着划着水一不小心NOIP还考的凑合了… 所以退役的打算要稍微搁置一下了… 要准备准备省选了…. 但是自己已经啥也不会了… 所以只能重新拾起来… 从splay开始吧… splay我以前扔了个板子来着, ...
- 【BZOJ3224】Tyvj 1728 普通平衡树 Splay
Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3. 查询x数的排名(若有多个相同的数 ...
- BZOJ3224/洛谷P3391 - 普通平衡树(Splay)
BZOJ链接 洛谷链接 题意简述 模板题啦~ 代码 //普通平衡树(Splay) #include <cstdio> int const N=1e5+10; int rt,ndCnt; i ...
- luoguP3369[模板]普通平衡树(Treap/SBT) 题解
链接一下题目:luoguP3369[模板]普通平衡树(Treap/SBT) 平衡树解析 #include<iostream> #include<cstdlib> #includ ...
- 平衡树——splay 一
splay 一种平衡树,同时也是二叉排序树,与treap不同,它不需要维护堆的性质,它由Daniel Sleator和Robert Tarjan(没错,tarjan,又是他)创造,伸展树是一种自调整二 ...
- hiho #1329 : 平衡树·Splay
#1329 : 平衡树·Splay 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho:小Hi,上一次你跟我讲了Treap,我也实现了.但是我遇到了一个关键的问题. ...
随机推荐
- IOCP编程原理(转)
在我的博客之前写了很多关于IOCP的“行云流水”似的看了让人发狂的文章,尤其是几篇关于 IOCP加线程池文章,更是让一些功力不够深厚的初学IOCP者,有种吐血的感觉.为了让大家能够立刻提升内力修为,并 ...
- queue队列模块
import Queue myqueue = Queue.Queue(maxsize = 10) Queue.Queue类即是一个队列的同步实现.队列长度可为无限或者有限.可通过Queue的构造函数的 ...
- mongoDB的学习
一:linux下安装mongoDB 1.在linux系统上安装MongoDB 上传安装包mongodb-linux-x86_64-3.0.6.tgz到linux系统的home目录下 tar -zxvf ...
- Ros学习——创建ROS消息和ROS服务
1.rosed rosed 是 rosbash 的一部分.利用它可以直接通过package名来获取到待编辑的文件而无需指定该文件的存储路径了. rosed默认的编辑器是vim.修改其他的,打开~/.b ...
- return()函数
在函数中,执行完return()函数后,下面的语句就不会再执行了.例子: <?php function fn() { echo "you are awsome"; retur ...
- C++面向对象类的实例题目一
在一个程序中,实现如下要求: (1)构造函数重载 (2)成员函数设置默认参数 (3)有一个友元函数 (4)有一个静态函数 (5)使用不同的构造函数创建不同对象 code: #include<io ...
- [转]JQuery 如何选择带有多个class的元素
比如下面代码需要选择同时带有这几个class的元素,怎么写? 1 <div class="modal fade in"></div> A: 1. 依次过滤 ...
- 项目一:第十二天 1、常见权限控制方式 2、基于shiro提供url拦截方式验证权限 3、在realm中授权 5、总结验证权限方式(四种) 6、用户注销7、基于treegrid实现菜单展示
1 课程计划 1. 常见权限控制方式 2. 基于shiro提供url拦截方式验证权限 3. 在realm中授权 4. 基于shiro提供注解方式验证权限 5. 总结验证权限方式(四种) 6. 用户注销 ...
- ubuntu apt指令分析
ubunut安装软件时候需要查看源内可供选择的安装包的一些信息,此处提供一些指令方便以后查阅 apt-get sudo apt-get update #更新源 sudo apt-get upgrade ...
- wpf 窗口打开后默认设置控件焦点
https://blog.csdn.net/Vblegend_2013/article/details/81771872 <Grid FocusManager.FocusedElement=&q ...