洛谷P3391 【模板】文艺平衡树(Splay)(FHQ Treap)
题目背景
这是一道经典的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次变换后的结果
输入输出样例
说明
n, m \leq 100000n,m≤100000
FHQ无敌,
解决区间问题的时候按照$r$分成两个
再按照$l$分成两个
那么我们就得到了需要翻转的区间
然后愉快的打标记就好啦
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
using namespace std;
#define ls T[now].ch[0]
#define rs T[now].ch[1]
const int MAXN=1e6+;
inline char nc()
{
static char buf[MAXN],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,,MAXN,stdin),p1==p2)?EOF:*p1++;
}
inline int read()
{
char c=nc();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=nc();}
while(c>=''&&c<=''){x=x*+c-'',c=nc();}
return x*f;
}
struct node
{
int ch[],val,siz,pri,mark;
}T[MAXN];
int tot=;
int x,y,z,root=,n,m;
int newnode(int v)
{
T[++tot].siz=;
T[tot].val=v;
T[tot].pri=rand();
return tot;
}
void update(int now)
{
T[now].siz=T[ls].siz+T[rs].siz+;
}
int Build(int l,int r)
{
if(l>r) return ;
int mid=(l+r)>>;
int now=newnode(mid-);
ls=Build(l,mid-);
rs=Build(mid+,r);
update(now);
return now;
}
void pushdown(int now)
{
if(T[now].mark&&now)
{
swap(ls,rs);
if(ls) T[ls].mark^=;
if(rs) T[rs].mark^=;
T[now].mark=;
}
}
void split(int now,int k,int &x,int &y)
{
if(!now) {x=y=;return ;}
pushdown(now);
if(T[ls].siz<k)
x=now,split(rs,k-T[ls].siz-,rs,y);
else
y=now,split(ls,k,x,ls);
update(now);
}
int merge(int x,int y)
{
if(!x||!y) return x+y;
pushdown(x);pushdown(y);
if(T[x].pri<T[y].pri)
{
T[x].ch[]=merge(T[x].ch[],y);
update(x);
return x;
}
else
{
T[y].ch[]=merge(x,T[y].ch[]);
update(y);
return y;
}
}
void dfs(int now)
{
pushdown(now);
if(T[now].ch[]) dfs(T[now].ch[]);
if(T[now].val>=&&T[now].val<=n) printf("%d ",T[now].val);
if(T[now].ch[]) dfs(T[now].ch[]);
}
int main()
{
#ifdef WIN32
freopen("a.in","r",stdin);
#else
#endif
//srand((unsigned)time(NULL));
n=read(),m=read();
root=Build(,n+);
while(m--)
{
int l=read(),r=read();
int a,b,c,d;
split(root,r+,a,b);
split(a,l,c,d);
T[d].mark^=;
root=merge( merge(c,d) ,b );
}
dfs(root);
return ;
}
洛谷P3391 【模板】文艺平衡树(Splay)(FHQ Treap)的更多相关文章
- 洛谷.3391.[模板]文艺平衡树(Splay)
题目链接 //注意建树 #include<cstdio> #include<algorithm> const int N=1e5+5; //using std::swap; i ...
- 【洛谷P3391】文艺平衡树——Splay学习笔记(二)
题目链接 Splay基础操作 \(Splay\)上的区间翻转 首先,这里的\(Splay\)维护的是一个序列的顺序,每个结点即为序列中的一个数,序列的顺序即为\(Splay\)的中序遍历 那么如何实现 ...
- 洛谷.3369.[模板]普通平衡树(Splay)
题目链接 第一次写(2017.11.7): #include<cstdio> #include<cctype> using namespace std; const int N ...
- luoguP3391[模板]文艺平衡树(Splay) 题解
链接一下题目:luoguP3391[模板]文艺平衡树(Splay) 平衡树解析 这里的Splay维护的显然不再是权值排序 现在按照的是序列中的编号排序(不过在这道题目里面就是权值诶...) 那么,继续 ...
- LOJ#105. 文艺平衡树(FHQ Treap)
题面 传送门 题解 \(FHQ\ Treap\)比起\(Splay\)还是稍微好写一点--就是老是忘了要下穿标记-- //minamoto #include<bits/stdc++.h> ...
- 洛谷P5055 【模板】可持久化文艺平衡树(FHQ Treap)
题面 传送门 题解 日常敲板子.jpg //minamoto #include<bits/stdc++.h> #define R register #define inline __inl ...
- 洛谷 P3391 模板Splay
#include<bits/stdc++.h> using namespace std; #define maxn 200000 int read() { ,w=; ;ch=getchar ...
- 洛谷.3369.[模板]普通平衡树(fhq Treap)
题目链接 第一次(2017.12.24): #include<cstdio> #include<cctype> #include<algorithm> //#def ...
- 【洛谷P3369】普通平衡树——Splay学习笔记(一)
二叉搜索树(二叉排序树) 概念:一棵树,若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值: 它的左.右子树也分别为二叉搜索树 ...
- 洛谷 P3391 【模板】文艺平衡树(Splay)
题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...
随机推荐
- [Poi] Setup PostCSS and Tailwind with Poi
This lesson walks through setting up a Poi project using PostCSS and the popular Tailwind library fo ...
- .Net商品管理(注释,百度,提问,对比,总结)
管理控制器 using System; using System.Collections.Generic; using System.Linq; using System.Web; using Sys ...
- Flex 最全的换行,制表符,回车,空格......特殊符号
字符 十进制字符编号 实体名字 说明 — &#; — 未使用Unused — &#; — 未使用Unused — &#; — 未使用Unused — &#; — 未使用 ...
- obdg反汇编破解crackme
obdg是一个反汇编软件 直接将要反汇编的exe文件拖入或者file->open打开文件,等待一段时间就会显示出来 界面中分别为汇编代码(程序内存内容),寄存器内容,数据内存内容,栈内容 代码界 ...
- mysql 5.7 双主+主从配置
mysql5.7安装及赋权 wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm rpm -ivh mysql57 ...
- expr---计算工具
expr命令是一款表达式计算工具,使用它完成表达式的求值操作. expr的常用运算符: 加法运算:+ 减法运算:- 乘法运算:\* 除法运算:/ 求摸(取余)运算:% 语法 expr(选项)(参数) ...
- 构建基于Javascript的移动CMS——加入滑动
在和几个有兴趣做移动CMS的小伙伴讨论了一番之后,我们认为当前比較重要的便是统一一下RESTful API.然而近期持续断网中,又遭遇了一次停电,暂停了对API的思考.在周末无聊的时光了看了<人 ...
- 【Linux探索之旅】第四部分第三课:文件传输,潇洒同步
内容简单介绍 .第四部分第三课:文件传输.潇洒同步 2.第四部分第四课:分析网络.隔离防火 文件传输.潇洒同步 这一课的内容相对简单,所以我们慢慢享用. 经过上一课的学习.我们已经知道怎样远程连接到其 ...
- XCode6报数组越界错误的问题
今天碰到一个非常奇葩的问题, 调试了半天: 错误:"index 0 beyond bounds for empty array", 意思就是说数据源数组为nil, 所以你调用直接 ...
- rtmp,rtsp,hLS区别
流媒体协议一共三种:rtmp,rtsp,http live streaming(apple和adobe各一种)rtmp是adobe的,rtsp android native支持,http live s ...