洛谷P3369 普通平衡树
刚学平衡树,分别用了Splay和fhq-treap交了一遍。
这是Splay的板子,貌似比较短?
Splay
``` cpp
#include
#include
#include
#include
using namespace std;
const int N = 100005, INF = 2e9;
int n;
int nid, root;
int fa[N], ch[N][2];
int val[N];
int sz[N], rep[N];
void update(int x) {
sz[x] = sz[ch[x][0]]+sz[ch[x][1]]+rep[x];
}
void rotate(int x) {
int y = fa[x], z = fa[y];
int d = ch[fa[x]][1]x;
ch[z][ch[fa[y]][1]y] = x; fa[x] = z;
ch[y][d] = ch[x][d^1]; fa[ch[x][d^1]] = y;
ch[x][d^1] = y; fa[y] = x;
update(y); update(x);
}
void splay(int x, int goal) {
for(; fa[x] != goal; rotate(x)) if(fa[fa[x]] != goal)
(ch[fa[x]][0]x)^(ch[fa[fa[x]]][0]fa[x]) ? rotate(x) : rotate(fa[x]);
if(!goal) root = x;
}
void find(int v) {
int u = root;
if(!u) return ;
while(ch[u][val[u]<v] && val[u] != v) u = ch[u][val[u]<v];
splay(u, 0);
}
int findPre(int v) {
find(v);
int u = root;
if(val[u] < v) return u;
u = ch[u][0];
while(ch[u][1]) u = ch[u][1];
return u;
}
int findNxt(int v) {
find(v);
int u = root;
if(val[u] > v) return u;
u = ch[u][1];
while(ch[u][0]) u = ch[u][0];
return u;
}
void insert(int v) {
int u = root, f = 0;
while(u && val[u] != v) f = u, u = ch[u][val[u]<v];
if(u) rep[u]++;
else {
nid++;
fa[nid] = f;
ch[f][val[f]<v] = nid;
val[nid] = v;
sz[nid] = rep[nid] = 1;
u = nid;
}
splay(u, 0);
}
void del(int v) {
int pre = findPre(v), nxt = findNxt(v);
splay(pre, 0); splay(nxt, pre);
int tar = ch[nxt][0];
if(rep[tar] > 1) {
rep[tar]--;
splay(tar, 0);
}
else ch[nxt][0] = 0;
}
int kth(int x) {
if(sz[root] < x) return 0;
int u = root;
while(1) {
int lson = ch[u][0];
if(x <= sz[lson]) u = lson;
else if(x > sz[lson]+rep[u]) x -= sz[lson]+rep[u], u = ch[u][1];
else return val[u];
}
}
int main() {
insert(-(1<<31)+2); //添加两个哨兵节点,防止越界
insert((1<<31)-2);
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 1, c, x; i <= n; i++) {
cin >> c >> x;
if(c == 1) insert(x);
else if(c == 2) del(x);
else if(c == 3) find(x), cout << sz[ch[root][0]] << endl;
else if(c == 4) cout << kth(x+1) << endl;
else if(c == 5) cout << val[findPre(x)] << endl;
else if(c == 6) cout << val[findNxt(x)] << endl;
}
return 0;
}
这是fhq-treap的板子(为了美观,我已经不能再无脑压行了)
<h3>fhq-treap</h3>
``` cpp
#include <iostream>
#include <cstring>
#include <algorithm>
#include <ctime>
using namespace std;
const int N = 100005;
int n;
int ch[N][2], val[N], sz[N], rnd[N];
int nid, root;
void update(int x) {
sz[x] = sz[ch[x][0]]+sz[ch[x][1]]+1;
}
int merge(int x, int y) {
if(!x || !y) return x+y;
if(rnd[x] < rnd[y]) {
ch[x][1] = merge(ch[x][1], y);
update(x);
return x;
} else {
ch[y][0] = merge(x, ch[y][0]);
update(y);
return y;
}
}
void split(int cur, int k, int &x, int &y) {
if(!cur) x = y = 0;
else {
if(val[cur] <= k) x = cur, split(ch[cur][1], k, ch[cur][1], y);
else y = cur, split(ch[cur][0], k, x, ch[cur][0]);
update(cur);
}
}
int newNode(int v) {
val[++nid] = v;
sz[nid] = 1;
rnd[nid] = rand();
return nid;
}
void insert(int v) {
int x, y;
split(root, v, x, y);
root = merge(merge(x, newNode(v)), y);
}
void del(int v) {
int x, y, z;
split(root, v, x, z); split(x, v-1, x, y);
y = merge(ch[y][0], ch[y][1]);
root = merge(merge(x, y), z);
}
int rankOf(int v) {
int x, y, ret;
split(root, v-1, x, y);
ret = sz[x]+1;
root = merge(x, y);
return ret;
}
int kth(int src, int x) {
if(sz[src] < x) return 0;
int u = src;
while (1) {
if(x <= sz[ch[u][0]]) u = ch[u][0];
else if(sz[ch[u][0]]+1 == x) return u;
else if(sz[ch[u][0]]+1 < x) x -= sz[ch[u][0]]+1, u = ch[u][1];
}
}
int precursor(int v) { //前驱
int x, y, ret;
split(root, v-1, x, y);
ret = val[kth(x, sz[x])];
root = merge(x, y);
return ret;
}
int successor(int v) { //后继
int x, y, ret;
split(root, v, x, y);
ret = val[kth(y, 1)];
root = merge(x, y);
return ret;
}
int main() {
srand((unsigned)time(NULL));
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 1, c, x; i <= n; i++) {
cin >> c >> x;
if(c == 1) insert(x);
else if(c == 2) del(x);
else if(c == 3) cout << rankOf(x) << endl;
else if(c == 4) cout << val[kth(root, x)] << endl;
else if(c == 5) cout << precursor(x) << endl;
else if(c == 6) cout << successor(x) << endl;
}
return 0;
}
洛谷P3369 普通平衡树的更多相关文章
- 洛谷P3369普通平衡树(Treap)
题目传送门 转载自https://www.cnblogs.com/fengzhiyuan/articles/7994428.html,转载请注明出处 Treap 简介 Treap 是一种二叉查找树.它 ...
- [洛谷P3369] 普通平衡树 Treap & Splay
这个就是存一下板子...... 题目传送门 Treap的实现应该是比较正经的. 插入删除前驱后继排名什么的都是平衡树的基本操作. #include<cstdio> #include< ...
- 洛谷P3369 【模板】普通平衡树(Treap/SBT)
洛谷P3369 [模板]普通平衡树(Treap/SBT) 平衡树,一种其妙的数据结构 题目传送门 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除 ...
- 【洛谷P3369】【模板】普通平衡树题解
[洛谷P3369][模板]普通平衡树题解 题目链接 题意: 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3 ...
- [洛谷P3391] 文艺平衡树 (Splay模板)
初识splay 学splay有一段时间了,一直没写...... 本题是splay模板题,维护一个1~n的序列,支持区间翻转(比如1 2 3 4 5 6变成1 2 3 6 5 4),最后输出结果序列. ...
- 绝对是全网最好的Splay 入门详解——洛谷P3369&BZOJ3224: Tyvj 1728 普通平衡树 包教包会
平衡树是什么东西想必我就不用说太多了吧. 百度百科: 一个月之前的某天晚上,yuli巨佬为我们初步讲解了Splay,当时接触到了平衡树里的旋转等各种骚操作,感觉非常厉害.而第二天我调Splay的模板竟 ...
- BZOJ3223/洛谷P3391 - 文艺平衡树
BZOJ链接 洛谷链接 题意 模板题啦~2 代码 //文艺平衡树 #include <cstdio> #include <algorithm> using namespace ...
- BZOJ3224/洛谷P3391 - 普通平衡树(Splay)
BZOJ链接 洛谷链接 题意简述 模板题啦~ 代码 //普通平衡树(Splay) #include <cstdio> int const N=1e5+10; int rt,ndCnt; i ...
- 洛谷 P3391 文艺平衡树
题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 --b ...
随机推荐
- 数据库CRUD操作以及MyBatis的配置使用
• 业务字段设计 • 数据库创建 • CRUD操作 • MyBatis集成 • 注解和XML定义 • ViewObject和DateTool • 首页开发 • 业务字段设计 实体: name: ...
- Server 2008 R2多用户远程桌面连接授权,解决120天过期问题
在工作中,我们往往需要远程服务器,经常会遇到以下这两个麻烦事. 一.远程桌面的连接数限制,超出系统就会提示超过连接数. 二.远程桌面连接时,同一个用户不能同时远程2个桌面连接. ----------- ...
- SQLServer之删除索引
使用SSMS数据库管理工具删除索引 使用表设计器删除索引 表设计器可以删除任何类型的索引,本示例演示删除XML辅助索引,删除其他索引步骤相同. 1.连接数据库,选择数据库,展开数据库->选择数据 ...
- Error:Cannot run program "svn" (in directory "E:demo\Hello"): CreateProcess error=2,
file-->settings-->version controller --> subversion
- 面向对象_item项目
详细见老师博客:http://www.cnblogs.com/Eva-J/articles/7351812.html#_label9 __getitem__\__setitem__\__delitem ...
- 【Linux基础】查看硬件信息-硬盘
一.基础知识 1.磁盘分区 磁盘的分区主要分为基本分区(primary partion)和扩充分区(extension partion)两种,基本分区和扩充分区的数目之和不能大于四个.且基本分区可以 ...
- 第十届蓝桥杯省赛JavaB组个人题解
前言 以下的第十届蓝桥杯Java B组省赛的题目题解只是我个人的题解,提供一些解题思路,仅作参考,如有错误,望大家指出,不甚感激,我会及时更改. 试题 A: 组队 ----- 答案:490 [问题描述 ...
- Eclipse中的快捷键
Ctrl+1:快捷修复(数字 1 不是字母 l) 将鼠标悬停到出错区域,按 Ctrl+1,出现快捷修复的菜单, 按上下方向键选择一种修复方式即可. 也可以将光标移动到出错区域,按 F2 + Enter ...
- iOS开发基础-UITableView基本属性
设置 UITableView 中 cell 的背景颜色. 示例1:通过 backgroundView 设置. UIView *view1 = [[UIView alloc] init]; view1. ...
- 1 Introduction
1. Introduction 1.1. License Flowable is distributed under the Apache V2 license. 1.2. Download http ...