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次变换后的结果

输入输出样例

输入样例#1: 复制

5 3
1 3
1 3
1 4
输出样例#1: 复制

4 3 2 1 5

说明

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的更多相关文章

  1. 洛谷.3369.[模板]普通平衡树(fhq Treap)

    题目链接 第一次(2017.12.24): #include<cstdio> #include<cctype> #include<algorithm> //#def ...

  2. 2021.12.08 平衡树——FHQ Treap

    2021.12.08 平衡树--FHQ Treap http://www.yhzq-blog.cc/fhqtreapzongjie/ https://www.cnblogs.com/zwfymqz/p ...

  3. luoguP3391[模板]文艺平衡树(Splay) 题解

    链接一下题目:luoguP3391[模板]文艺平衡树(Splay) 平衡树解析 这里的Splay维护的显然不再是权值排序 现在按照的是序列中的编号排序(不过在这道题目里面就是权值诶...) 那么,继续 ...

  4. Luogu P3835 【模板】可持久化平衡树(fhq Treap)

    P3835 [模板]可持久化平衡树 题意 题目背景 本题为题目普通平衡树的可持久化加强版. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作(对于各个以往的历史版本 ...

  5. 洛谷.3835.[模板]可持久化平衡树(fhq treap)

    题目链接 对每次Merge(),Split()时产生的节点都复制一份(其实和主席树一样).时间空间复杂度都为O(qlogq).(应该更大些 因为rand()?内存真的爆炸..) 对于无修改的操作实际上 ...

  6. 洛谷.3391.[模板]文艺平衡树(Splay)

    题目链接 //注意建树 #include<cstdio> #include<algorithm> const int N=1e5+5; //using std::swap; i ...

  7. 洛谷.3391.文艺平衡树(fhq Traep)

    题目链接 //注意反转时先分裂r,因为l,r是针对整棵树的排名 #include<cstdio> #include<cctype> #include<algorithm& ...

  8. 【洛谷P3391】文艺平衡树——Splay学习笔记(二)

    题目链接 Splay基础操作 \(Splay\)上的区间翻转 首先,这里的\(Splay\)维护的是一个序列的顺序,每个结点即为序列中的一个数,序列的顺序即为\(Splay\)的中序遍历 那么如何实现 ...

  9. FHQ Treap及其可持久化与朝鲜树式重构

    FHQ Treap,又称无旋treap,一种不基于旋转机制的平衡树,可支持所有有旋treap.splay等能支持的操作(只有在LCT中会比splay复杂度多一个log).最重要的是,它是OI中唯一一种 ...

随机推荐

  1. ios中 input 焦点光标不垂直居中

    笔记:在ios,如果同时给input设置这种平时我们使字体垂直居中的css写法. 光标会出现,如下图的问题 . 改正方案: 采取不使用line-height的垂直居中方法即可.

  2. zip (ICSharpCode.SharpZipLib.dll文件需要下载)

    ZipClass zc=new ZipClass (); zc.ZipDir(@"E:\1\新建文件夹", @"E:\1\新建文件夹.zip", 1);//压缩 ...

  3. html学习笔记-DOM

    html学习笔记-DOM Table of Contents 1. 什么是 DOM? 2. DOM 节点 3. DOM 方法 4. DOM 属性 5. DOM 访问 6. DOM 修改 7. DOM ...

  4. layui内置loading等待加载

    点击功能按钮之后 var loading = layer.load(0, { shade: false, time: 2*1000 }); 参数: icon:0,1,2 loading风格 shade ...

  5. windows服务器安装安全狗时服务名如何填写

    安全狗安装时“服务名”这一栏指的是apache进程的服务名称,即进入“任务管理-服务”里显示的名称. phpstudy等软件搭建的环境需要设置运行模式为“系统服务”后才能看到服务名.

  6. 【虚拟机-网络IP】如何开放 Azure 虚拟机 Ping 功能

    前言 文章<使用 PsPing & PaPing 进行 TCP 端口连通性测试>中提到,ICMP 协议的数据包无法通过 Azure 的防火墙和负载均衡器,所以不能直接使用 Ping ...

  7. JVM的内存划分

    1.栈内存:栈内存主要是用来运行函数的,在函数中定义的所有变量,都会在这个内存开辟空间. 在栈内存中定义的变量,不初始化,是不能直接使用的. 注意:所有的函数都必须在栈内存中运行. 而jvm只会运行处 ...

  8. Struts功能详解 ——ActionServlet

    ActionServlet类是Struts框架的内置核心控制器组件,它继承了javax.servlet.http.HttpServlet类.Struts的启动通常从 加载ActionServlet开始 ...

  9. 如何修改Ruby的gem源(gem sources)

    Ruby环境下的gem sources地址默认是国外网络地址,所以在使用gem的过程中经常会出现找不到资源的Error.那么如何解决这种Error?方法很简单:要么就多次尝试执行gem命令,要么就修改 ...

  10. 洛谷 P1433 吃奶酪

    题目描述 房间里放着n块奶酪.一只小老鼠要把它们都吃掉,问至少要跑多少距离?老鼠一开始在(0,0)点处. 输入输出格式 输入格式: 第一行一个数n (n<=15) 接下来每行2个实数,表示第i块 ...