Luogu P3391 【模板】文艺平衡树(FHQ-Treap)
题意
给出一个长为$n$序列$[1,2,...,n]$,$m$次操作,每次指定一段区间$[l,r]$,将这段区间翻转,求最终序列
题解
虽然标题是$Splay$,但是我要用$FHQ\ Treap$,考虑先将$[l,r]$这段区间$split$出来($k$即为这段区间)
void split(int o, int k, int &l, int &r) {
if(!o) { l = r = 0; return ; }
if(siz[lc[o]] < k) l = o, split(rc[o], k - siz[lc[o]] - 1, rc[o], r);
else r = o, split(lc[o], k, l, lc[o]);
upt(o);
}//注意这里要按size来split
//写在main函数中
while(m--) {
read(x), read(y);
split(rt, y, l, r), split(l, x - 1, l, k);
rev[k] ^= 1; rt = merge(merge(l, k), r);
}//x,y为操作的区间
然后再将这段区间打一个翻转标记(因为平衡树是可以中序遍历输出的吧...,$rev$为翻转标记)
每次涉及到某个节点时,将$rev$标记下放就好了
void pushdown(int o) {
std::swap(lc[o], rc[o]);
if(lc[o]) rev[lc[o]] ^= 1;
if(rc[o]) rev[rc[o]] ^= 1;
rev[o] = 0;
}
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
template<typename T>
void read(T &x) {
int flag = 1; x = 0; char ch = getchar();
while(ch < '0' || ch > '9') { if(ch == '-') flag = -flag; ch = getchar(); }
while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); x *= flag;
}
const int N = 1e5 + 10;
int n, m, lc[N], rc[N], siz[N], val[N], pri[N], rev[N], tot;
inline void upt(int o) { siz[o] = siz[lc[o]] + siz[rc[o]] + 1; }
inline int node(int x) { val[++tot] = x, pri[tot] = rand(), siz[tot] = 1; return tot;}
void pushdown(int o) {
std::swap(lc[o], rc[o]);
if(lc[o]) rev[lc[o]] ^= 1;
if(rc[o]) rev[rc[o]] ^= 1;
rev[o] = 0;
}
void split(int o, int k, int &l, int &r) {
if(!o) { l = r = 0; return ; }
if(rev[o]) pushdown(o);
if(siz[lc[o]] < k) l = o, split(rc[o], k - siz[lc[o]] - 1, rc[o], r);
else r = o, split(lc[o], k, l, lc[o]);
upt(o);
}
int merge(int l, int r) {
if(!l || !r) return l + r;
if(pri[l] < pri[r]) { if(rev[l]) pushdown(l); rc[l] = merge(rc[l], r), upt(l); return l; }
else { if(rev[r]) pushdown(r); lc[r] = merge(l, lc[r]), upt(r); return r; }
}
void print(int o) {
if(!o) return ;
if(rev[o]) pushdown(o);
print(lc[o]), printf("%d ", val[o]), print(rc[o]);
}
int main () {
read(n), read(m), srand((unsigned)time(NULL));
int x, y, l, r, k, rt = 0;
for(int i = 1; i <= n; ++i) rt = merge(rt, node(i));
while(m--) {
read(x), read(y);
split(rt, y, l, r), split(l, x - 1, l, k);
rev[k] ^= 1; rt = merge(merge(l, k), r);
} print(rt);
return 0;
}
Luogu P3391 【模板】文艺平衡树(FHQ-Treap)的更多相关文章
- P3391 【模板】文艺平衡树FHQ treap
P3391 [模板]文艺平衡树(Splay) 题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转 ...
- 洛谷.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中唯一一种 ...
随机推荐
- Django ORM常用的函数以及修饰词
函数名称或修饰词 说明 filter() 返回符合指定条件的QuerySet exclude() 返回不符合指定条件的QuerySet ordey_by() 串接到QuerySet之后,针对某一指定的 ...
- Item 9 覆盖equals时总要覆盖hashCode
为什么覆盖equals时,总要覆盖hashCode? 原因是,根据Object规范: 如果两个对象根据equals(Object)方法比较是相等的,那么调用这两个对象中任意一个对象的hashCod ...
- JS之递归(例题:猴子吃桃)
例题1:公园里有200个桃子,猴子每天吃掉一半以后扔掉一个,问6天以后还剩余多少桃子? var sum = 200; for(var i= 0;i<6;i++) { sum = parseInt ...
- java提取SVN提交log
http://wiki.svnkit.com/Printing_Out_Repository_History 这个介绍的相当详细. 总之就是要使用SVNKit包,下载地址.http://svnkit. ...
- mac系统用docker安装oracle数据库
oracle没有mac可用的版本,最好的办法是通过docker安装 一.下载docker 1.通过brew下载 brew cask install docker 2.手动下载(需要vpn) https ...
- 网络设备之分配net_device结构
注册网络设备时,会调用pci_driver->probe函数,以e100为例,最终会调用alloc_netdev_mqs来分配内存,并且在分配内存后调用setup函数(以太网为ether_set ...
- python基础===中文手册,可查询各个模块
http://python.usyiyi.cn/translate/python_352/index.html
- libsensor
https://github.com/easyiot-china/libsensor https://github.com/adafruit/DHT-sensor-library https://gi ...
- C#比较两个list集合,两集合同时存在或A集合存在B集合中无
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Educational Codeforces Round 26 F. Prefix Sums 二分,组合数
题目链接:http://codeforces.com/contest/837/problem/F 题意:如题QAQ 解法:参考题解博客:http://www.cnblogs.com/FxxL/p/72 ...