洛谷 3391 【模板】文艺平衡树 Treap区间翻转

【题解】
用Treap维护这个序列。
加入的时候直接插入到末尾,这样Treap就变成一棵以插入时间先后为排序关键字的二叉搜索树。
对于翻转操作,我们分裂出需要翻转的区间,给这个区间的root打一个翻转标记。
在分裂、合并、输出的时候,遇到翻转标记,就把左右儿子交换,同时下传标记。
#include<cstdio>
#include<algorithm>
#define ls (a[u].l)
#define rs (a[u].r)
using namespace std;
const int maxn=;
int n,m,x,y,z,l,r,tot,root;
struct treap{int l,r,v,rnd,size,rot;}a[maxn];
inline int read(){
int k=,f=; char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(''<=c&&c<='')k=k*+c-'',c=getchar();
return k*f;
}
void newnode(int val){a[++tot]=(treap){,,val,rand(),,};}
void update(int u){a[u].size=a[ls].size+a[rs].size+;}
void rotate(int u){if(a[u].rot) a[u].rot^=,swap(ls,rs),a[ls].rot^=,a[rs].rot^=;}
void split(int u,int k,int &x,int &y){
rotate(u);
if(!k){x=; y=u; return;}
if(a[u].size==k){x=u; y=; return;}
if(a[ls].size>=k) split(ls,k,x,ls),y=u;
else split(rs,k-a[ls].size-,rs,y),x=u;
update(u);
}
int merge(int x,int y){
if(!x||!y) return x+y;
rotate(x); rotate(y);
if(a[x].rnd<a[y].rnd){
a[x].r=merge(a[x].r,y); update(x); return x;
}
else{
a[y].l=merge(x,a[y].l); update(y); return y;
}
}
void put(int u){
rotate(u);
if(ls) put(ls);
printf("%d ",a[u].v);
if(rs) put(rs);
}
int main(){
n=read(); m=read();
for(int i=;i<=n;i++) newnode(i),root=merge(root,tot);
while(m--){
l=read()-; r=read();
split(root,l,x,y); split(y,r-l,y,z);
a[y].rot^=;
root=merge(merge(x,y),z);
}
put(root);
return ;
}
洛谷 3391 【模板】文艺平衡树 Treap区间翻转的更多相关文章
- 洛谷.3391.[模板]文艺平衡树(Splay)
题目链接 //注意建树 #include<cstdio> #include<algorithm> const int N=1e5+5; //using std::swap; i ...
- 洛谷P3373 [模板]线段树 2(区间增减.乘 区间求和)
To 洛谷.3373 [模板]线段树2 题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.将某区间每一个数乘上x 3.求出某区间每一个数的和 输入输出格式 输入格 ...
- 【洛谷P3391】文艺平衡树——Splay学习笔记(二)
题目链接 Splay基础操作 \(Splay\)上的区间翻转 首先,这里的\(Splay\)维护的是一个序列的顺序,每个结点即为序列中的一个数,序列的顺序即为\(Splay\)的中序遍历 那么如何实现 ...
- bzoj 3223 文艺平衡树 splay 区间翻转
Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 17715 Solved: 7769[Submit][Status][ ...
- 洛谷.3369.[模板]普通平衡树(fhq Treap)
题目链接 第一次(2017.12.24): #include<cstdio> #include<cctype> #include<algorithm> //#def ...
- 洛谷.3369.[模板]普通平衡树(Splay)
题目链接 第一次写(2017.11.7): #include<cstdio> #include<cctype> using namespace std; const int N ...
- 文艺平衡树(区间翻转)(Splay模板)
这篇blog写的吼啊 #include<cstdio> #include<iostream> #include<cstring> using namespace s ...
- luoguP3391[模板]文艺平衡树(Splay) 题解
链接一下题目:luoguP3391[模板]文艺平衡树(Splay) 平衡树解析 这里的Splay维护的显然不再是权值排序 现在按照的是序列中的编号排序(不过在这道题目里面就是权值诶...) 那么,继续 ...
- luoguP3369[模板]普通平衡树(Treap/SBT) 题解
链接一下题目:luoguP3369[模板]普通平衡树(Treap/SBT) 平衡树解析 #include<iostream> #include<cstdlib> #includ ...
随机推荐
- Domain-specific language 领域特定语言
https://en.wikipedia.org/wiki/Domain-specific_language A domain-specific language (DSL) is a compute ...
- 20170410 --- Linux备课资料 --- 压缩与解压缩
这节课我们来学习一下压缩与解压缩,那什么是压缩与解压缩呢? 联想一下Windows系统: 选中文件,右键选择即可 如果压缩,可以选择要压缩的格式,而解压缩直接选择就可以完成了 Linux是通过命令的方 ...
- android电池管理系统
原文:http://www.2cto.com/kf/201408/326462.html 1.概述 随着移动智能设备的快速发屏,电池的续航能力在很大情况下诱导了大众消费者的购买选择,android系统 ...
- Anaconda 安装和使用Numpy、Scipy、pandas、Scikit-learn
Anaconda 安装和使用 https://www.cnblogs.com/liruihuan/p/9101613.html 最近看了些关于数据分析的书,想系统的整理下相关知识,算是学习笔记吧,也希 ...
- 杂项:E-Learning
ylbtech-杂项:E-Learning 1.返回顶部 1. E-Learning:英文全称为(Electronic Learning),中文译作“数字(化)学习”.“电子(化)学习”.“网络(化) ...
- js addeventlistener 刮刮贴
<!doctype html><html><head><meta charset="utf-8"><title>无标题文 ...
- python网络爬虫之图片链家在技术.seleninum和PhantonJS
一.什么是图片懒加载? 案例分析:抓取站长素材http://sc.chinaz.com/中的图片数据 #!/usr/bin/env python # -*- coding:utf-8 -*- impo ...
- cocos2dx实现单机版三国杀(一)
首先需要一个UI交互类 GameUI -layer 一个游戏驱动类,负责游戏逻辑的循环 暂时定为GameScene- scene GameScene obj 调用update 更新游戏,addch ...
- NSNotificationCenter 的使用详解
通常我们在 iOS 中发生什么事件时该做什么是由 Delegate 实现的,例如 View 加载完后会触发 viewDidLoad.Apple 还为我们提供了另一种通知响应方式,那就是 NSNotif ...
- Robomongo 0.9.0 连接mongo数据库时,提示连接失败 的解决方案
Robomongo 0.9.0 连接mongo数据库时,提示连接失败.(IP和端口号确定是对的) 基本注意点: 1.mongodb服务打开,打开时,指定端口号,默认为27017,使用默认值,则不用指定 ...