P3391 【模板】文艺平衡树FHQ treap
P3391 【模板】文艺平衡树(Splay)
题目背景
这是一道经典的Splay模板题——文艺平衡树。
题目描述
您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1
输入输出格式
输入格式:
第一行为n,m n表示初始序列有n个数,这个序列依次是(1,2, \cdots n-1,n)(1,2,⋯n−1,n) m表示翻转操作次数
接下来m行每行两个数 [l,r][l,r] 数据保证 1 \leq l \leq r \leq n1≤l≤r≤n
输出格式:
输出一行n个数字,表示原始序列经过m次变换后的结果
输入输出样例
说明
code
#include<cstdio>
#include<algorithm> using namespace std; const int N = ;
int ch[N][],tag[N],val[N],siz[N],key[N];
int tn,Root,n,m; inline char nc() {
static char buf[],*p1 = buf,*p2 = buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,,,stdin),p1==p2) ? EOF : *p1++;
}
inline int read() {
int x = ,f = ;char ch = nc();
for (; ch<''||ch>''; ch = nc())
if (ch=='-') f = -;
for (; ch>=''&&ch<=''; ch = nc())
x = x*+ch-'';
return x * f;
}
inline void pushup(int x) {
siz[x] = siz[ch[x][]] + siz[ch[x][]] + ;
}
inline void pushdown(int x) {
if (tag[x]) {
tag[ch[x][]] ^= ;tag[ch[x][]] ^= ;
swap(ch[x][],ch[x][]);
tag[x] ^= ;
}
}
inline int makenode(int x) {
++tn;siz[tn] = ;val[tn] = x;key[tn] = rand();return tn;
}
int merge(int x,int y) {
if (!x || !y) return x + y;
pushdown(x);pushdown(y);
if (key[x] < key[y]) {
ch[x][] = merge(ch[x][],y);
pushup(x);return x;
}
else {
ch[y][] = merge(x,ch[y][]);
pushup(y);return y;
}
}
void split(int now,int k,int &x,int &y) {
if (!now) x = y = ;
else {
pushdown(now);
if (k<=siz[ch[now][]])
y = now,split(ch[now][],k,x,ch[now][]);
else
x = now,split(ch[now][],k-siz[ch[now][]]-,ch[now][],y);
pushup(now);
}
}
inline void rever(int l,int r) {
int a,b,c,d;
split(Root,r,a,b);
split(a,l-,c,d);
tag[d] ^= ;
Root = merge(merge(c,d),b);
}
void print(int x) {
if (!x) return ;
pushdown(x);
print(ch[x][]);
printf("%d ",val[x]);
print(ch[x][]);
}
int main() {
n = read(),m = read();
for (int i=; i<=n; ++i) {
Root = merge(Root,makenode(i));
}
while (m--) {
int a = read(),b = read();
rever(a,b);
}
print(Root);
return ;
}
网上学的另一种建树方法:
int build(int l,int r)
{
if (l>r) return ;
int mid=(l+r)>>,v=mid;
int now=makenode(v);
ch[now][]=build(l,mid-);
ch[now][]=build(mid+,r);
pushup(now);
return now;
}
Root = build(,n);
虽然可能不满足堆的性质,但是,堆在这个过程中只是调节树的平衡的,所以还是可以过的
板子的发展史。。。
#include<cstdio>
#include<algorithm> using namespace std; const int N = ;
int ch[N][],tag[N],val[N],siz[N],key[N];
int tn,Root,n,m; inline char nc() {
static char buf[],*p1 = buf,*p2 = buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,,,stdin),p1==p2) ? EOF : *p1++;
}
inline int read() {
int x = ,f = ;char ch = nc();
for (; ch<''||ch>''; ch = nc())
if (ch=='-') f = -;
for (; ch>=''&&ch<=''; ch = nc())
x = x*+ch-'';
return x * f;
}
inline void pushup(int x) {
siz[x] = siz[ch[x][]] + siz[ch[x][]] + ;
}
inline void pushdown(int x) {
if (tag[x]) {
tag[ch[x][]] ^= ;tag[ch[x][]] ^= ;
swap(ch[x][],ch[x][]);
tag[x] ^= ;
}
}
inline int makenode(int x) {
++tn;siz[tn] = ;val[tn] = x;key[tn] = rand();return tn;
}
int merge(int x,int y) {
if (!x || !y) return x + y;
if (key[x] < key[y]) {
pushdown(x);
ch[x][] = merge(ch[x][],y);
pushup(x);return x;
}
else {
pushdown(y);
ch[y][] = merge(x,ch[y][]);
pushup(y);return y;
}
}
void split(int now,int k,int &x,int &y) {
if (!now) x = y = ;
else {
pushdown(now);
if (k<=siz[ch[now][]])
y = now,split(ch[now][],k,x,ch[now][]);
else
x = now,split(ch[now][],k-siz[ch[now][]]-,ch[now][],y);
pushup(now);
}
}
inline void rever(int l,int r) {
int a,b,c,d;
split(Root,r,a,b);
split(a,l-,c,d);
tag[d] ^= ;
Root = merge(merge(c,d),b);
}
void print(int x) {
if (!x) return ;
pushdown(x);
print(ch[x][]);
printf("%d ",val[x]);
print(ch[x][]);
}
int main() {
n = read(),m = read();
for (int i=; i<=n; ++i) {
Root = merge(Root,makenode(i));
}
while (m--) {
int a = read(),b = read();
rever(a,b);
}
print(Root);
return ;
}
P3391 【模板】文艺平衡树FHQ treap的更多相关文章
- 洛谷.3369.[模板]普通平衡树(fhq Treap)
题目链接 第一次(2017.12.24): #include<cstdio> #include<cctype> #include<algorithm> //#def ...
- 2021.12.08 平衡树——FHQ Treap
2021.12.08 平衡树--FHQ Treap http://www.yhzq-blog.cc/fhqtreapzongjie/ https://www.cnblogs.com/zwfymqz/p ...
- luoguP3391[模板]文艺平衡树(Splay) 题解
链接一下题目:luoguP3391[模板]文艺平衡树(Splay) 平衡树解析 这里的Splay维护的显然不再是权值排序 现在按照的是序列中的编号排序(不过在这道题目里面就是权值诶...) 那么,继续 ...
- Luogu P3835 【模板】可持久化平衡树(fhq Treap)
P3835 [模板]可持久化平衡树 题意 题目背景 本题为题目普通平衡树的可持久化加强版. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作(对于各个以往的历史版本 ...
- 洛谷.3835.[模板]可持久化平衡树(fhq treap)
题目链接 对每次Merge(),Split()时产生的节点都复制一份(其实和主席树一样).时间空间复杂度都为O(qlogq).(应该更大些 因为rand()?内存真的爆炸..) 对于无修改的操作实际上 ...
- 洛谷.3391.[模板]文艺平衡树(Splay)
题目链接 //注意建树 #include<cstdio> #include<algorithm> const int N=1e5+5; //using std::swap; i ...
- 洛谷.3391.文艺平衡树(fhq Traep)
题目链接 //注意反转时先分裂r,因为l,r是针对整棵树的排名 #include<cstdio> #include<cctype> #include<algorithm& ...
- 【洛谷P3391】文艺平衡树——Splay学习笔记(二)
题目链接 Splay基础操作 \(Splay\)上的区间翻转 首先,这里的\(Splay\)维护的是一个序列的顺序,每个结点即为序列中的一个数,序列的顺序即为\(Splay\)的中序遍历 那么如何实现 ...
- FHQ Treap及其可持久化与朝鲜树式重构
FHQ Treap,又称无旋treap,一种不基于旋转机制的平衡树,可支持所有有旋treap.splay等能支持的操作(只有在LCT中会比splay复杂度多一个log).最重要的是,它是OI中唯一一种 ...
随机推荐
- 浅析libuv源码-node事件轮询解析(3)
好像博客有观众,那每一篇都画个图吧! 本节简图如下. 上一篇其实啥也没讲,不过node本身就是这么复杂,走流程就要走全套.就像曾经看webpack源码,读了300行代码最后就为了取package.js ...
- swift基础-2
一.基本运算符 let a = 5 var b = 10 b = a if a = b{ swift 中赋值运算符,并不将自身作为一个值进行返回,所以编译不合法,帮开发者避免错误,很人性化的语言 } ...
- Mysql order by 排序 varchar 类型数据
Mysql order by 排序 varchar 类型数据 varchar 类型字段排序, 会將数字当成字符串来处理. 排序规则一般是从左到右一位位来比较. +0之后 就转化成INT 类型排序 ...
- echarts使用中的那些事儿(二)
然而第二天的事情你怎么能猜到客户的心思呢: 客户:右边的是啥? 偶:可放大缩小查看各个时期的数据. 客户:把它去掉吧,全部直观显示. 偶:哦,不过数据过多的话会导致页面过长的. 客户:只有你知道这个可 ...
- Object-C反射读取实体属性和值
举例: 首先定义TestModel如下: @interface TestModel : NSObject @property (nonatomic, strong) NSString *name; @ ...
- 看动画,秒懂人工智能&物联网
- SQL Server 父子迭代查询语句
-- 根据父ID得到所有子ID -- Get childs by parent idWITH TreeAS( SELECT Id,ParentId FROM dbo.Node P WHERE P.Id ...
- UVA 12898 - And Or 与和或 (思路题)
思路就是有零一变化的位Or以后一定是1,And以后一定是0:那么如果b的二进制更长那么就把包含a的部分全部置为1或0,如果一样长那么就把不同的部分置为1或0. 今天被这题坑的地方:1默认是int,如果 ...
- acid (数据库事务正确执行的四个基本要素的缩写)
ACID,指数据库事务正确执行的四个基本要素的缩写.包含:原子性(Atomicity).一致性(Consistency).隔离性(Isolation).持久性(Durability).一个支持事务(T ...
- java 自定义容器,实现foreach
import java.util.Arrays; import java.util.Iterator; public class ArrayList implements Iterable<In ...