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中唯一一种 ...
随机推荐
- Java设计模式之单例设计模式总结
package singleton; /**单例设计模式 饿汉式 * * @author gx *这种方式基于classloder机制避免了多线程的同步问题,不过,instance在类装载时就实例化, ...
- EF批量插入数据耗时对比
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Java程序运行参数
Java主函数形式:public static void main(String[] args){......} 也就是说可以向Java程序传递一个String[]. 1.在IDEA中debug.ru ...
- Strut2 Action的生命周期
一般而言,Action都是放在Spring容器中管理的,我会把属性设为prototype,这样,每一个请求,都会创建一个action对象. 今天碰到一个问题,当我用从一个jsp页面中输入一个属性,比如 ...
- java中循环的不同终止方式
1.break:直接强行跳出当前循环,不再执行剩余代码.但在多重循环的情况下,若break在内层循环中,则仅仅终止了内层循环,外循环照常执行. 2.continue:仅仅终止此次循环. 3.retur ...
- MapReduce的编程思想(1)
MapReduce的编程思想(1) MapReduce的过程(2) 1. MapReduce采用分而治之的思想,将数据处理拆分为主要的Map(映射)与Reduce(化简)两步,MapReduce操作数 ...
- cpu 满载测试软件
for i in `seq 1 $(cat /proc/cpuinfo |grep "physical id" |wc -l)`; do dd if=/dev/zero of=/d ...
- ThreadLocal的内存泄露
ThreadLocal的目的就是为每一个使用ThreadLocal的线程都提供一个值,让该值和使用它的线程绑定,当然每一个线程都可以独立地改变它绑定的值.如果需要隔离多个线程之间的共享冲突,可以使用T ...
- ajax请求执行完成后再执行其他操作(jQuery.page.js插件使用为例)
就我们做知,ajax强大之处在于它的异步请求,但是有时候我们需要ajax执行彻底完成之后再执行其他函数或操作 这个时候往往我们用到ajax的回调函数,但是假如你不想或者不能把接下来的操作写在回调函数中 ...
- LibreOJ #514. 「LibreOJ β Round #2」模拟只会猜题意
内存限制:256 MiB 时间限制:1000 ms 标准输入输出 题目类型:传统 评测方式:文本比较 题目描述 给定一个长度为 nnn 的序列 AAA . 定义 f(l,r)=∑i=lrAif(l,r ...