洛谷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 ...
随机推荐
- python smtplib发email
#!/usr/bin/env python #coding: utf-8 import smtplib from email.mime.text import MIMEText from email. ...
- mysql使用索引的注意事项
使用索引的注意事项 使用索引时,有以下一些技巧和注意事项: 1.索引不会包含有NULL值的列 只要列中包含有NULL值都将不会被包含在索引中,复合索引中只要有一列含有NULL值,那么这一列对于此复合索 ...
- SQL server常用函数使用示例
select convert(nvarchar(10),id)+name from t //convert():数据类型转换,将“id”列转换为“nvarchar”. select cast(id a ...
- python粗谈面向对象(一)
1.面向过程编程vs函数式编程 面向过程编程 以计算对象的元素个数为例. str_1 = 'abcdefg' count = 0 for i in str_1: # 统计字符串元素个数 count + ...
- c/c++ 多线程 boost的读写(reader-writer)锁
多线程 boost的读写(reader-writer)锁 背景:保护很少更新的数据结构时,c++标准库没有提供相应的功能. 例如:有个DNS条目缓存的map,基本上很少有更新,大部分都是读取,但是偶尔 ...
- 英语口语练习系列-C22-吃东西
基础词汇 1. bill [bɪl] n. 账单:钞票:法案:鸟嘴 Bill (人名)比尔 pay the bill 付账单 telephone bill 话费单 electricity bill 电 ...
- 二叉搜索树的最近公共祖先的golang实现
给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先. 百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p.q,最近公共祖先表示为一个结点 x,满足 x 是 p.q 的祖先且 x ...
- django-debug-toolbar使用指南
好久没发新博客,凑个数... django-debug-toolbar 介绍 django-debug-toolbar 是一组可配置的面板,可显示有关当前请求/响应的各种调试信息,并在单击时显示有关面 ...
- 【Teradata SQL】创建数据库和表
1.数据库perm大小为10G Create database testbase as perm=10E9,spool=10E9; 2.创建物理表 create multiset table stg( ...
- python实现数据结构单链表
#python实现数据结构单链表 # -*- coding: utf-8 -*- class Node(object): """节点""" ...