洛谷.3391.文艺平衡树(fhq Traep)
//注意反转时先分裂r,因为l,r是针对整棵树的排名
#include<cstdio>
#include<cctype>
#include<algorithm>
//#define gc() getchar()
#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
const int N=1e5+7,MAXIN=2e6;
int n,root,sz[N],son[N][2],fix[N],tag[N];
char IN[MAXIN],*SS=IN,*TT=IN;
inline int read()
{
int now=0,f=1;register char c=gc();
for(;!isdigit(c);c=gc()) if(c=='-') f=-1;
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now*f;
}
inline void Update(int rt)
{
sz[rt]=sz[son[rt][0]]+sz[son[rt][1]]+1;
}
inline void Down(int rt)
{
tag[son[rt][0]]^=1,
tag[son[rt][1]]^=1;
std::swap(son[rt][0],son[rt][1]);
tag[rt]=0;//清空!
}
int Build(int l,int r)
{
if(l>r) return 0;
int m=l+r>>1;
fix[m]=rand(),sz[m]=1;
son[m][0]=Build(l,m-1),
son[m][1]=Build(m+1,r);
Update(m);
return m;
}
void Split(int rt,int k,int &x,int &y)
{
if(!rt) x=y=0;
else
{
if(tag[rt]) Down(rt);
if(k<=sz[son[rt][0]]) y=rt,Split(son[rt][0],k,x,son[rt][0]);
else x=rt,Split(son[rt][1],k-sz[son[rt][0]]-1,son[rt][1],y);
Update(rt);
}
}
int Merge(int x,int y)
{
if(!x||!y) return x+y;
if(tag[x]) Down(x);
if(tag[y]) Down(y);
if(fix[x]<fix[y])
{
son[x][1]=Merge(son[x][1],y);
Update(x);
return x;
}
else
{
son[y][0]=Merge(x,son[y][0]);
Update(y);
return y;
}
}
void Reverse()
{
int x,y,z,l=read(),r=read();
Split(root,r+1,y,z), Split(y,l,x,y);
tag[y]^=1;
root=Merge(Merge(x,y),z);
}
void DFS(int rt)
{
if(tag[rt]) Down(rt);
if(son[rt][0]) DFS(son[rt][0]);
if(rt!=1&&rt!=n+2) printf("%d ",rt-1);
if(son[rt][1]) DFS(son[rt][1]);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("3391.in","r",stdin);
#endif
n=read();
root=Build(1,n+2);
int q=read();
while(q--) Reverse();
DFS(root);
return 0;
}
洛谷.3391.文艺平衡树(fhq Traep)的更多相关文章
- [洛谷P3391] 文艺平衡树 (Splay模板)
初识splay 学splay有一段时间了,一直没写...... 本题是splay模板题,维护一个1~n的序列,支持区间翻转(比如1 2 3 4 5 6变成1 2 3 6 5 4),最后输出结果序列. ...
- BZOJ3223/洛谷P3391 - 文艺平衡树
BZOJ链接 洛谷链接 题意 模板题啦~2 代码 //文艺平衡树 #include <cstdio> #include <algorithm> using namespace ...
- 洛谷 P3391 文艺平衡树
题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 --b ...
- 洛谷P3391文艺平衡树(Splay)
题目传送门 转载自https://www.cnblogs.com/yousiki/p/6147455.html,转载请注明出处 经典引文 空间效率:O(n) 时间效率:O(log n)插入.查找.删除 ...
- 洛谷P3391 文艺平衡树 (Splay模板)
模板题. 注意标记即可,另外,涉及区间翻转操作,记得设立首尾哨兵. 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int ...
- BZOJ3224/洛谷P3391 - 普通平衡树(Splay)
BZOJ链接 洛谷链接 题意简述 模板题啦~ 代码 //普通平衡树(Splay) #include <cstdio> int const N=1e5+10; int rt,ndCnt; i ...
- 洛谷P3369普通平衡树(Treap)
题目传送门 转载自https://www.cnblogs.com/fengzhiyuan/articles/7994428.html,转载请注明出处 Treap 简介 Treap 是一种二叉查找树.它 ...
- P3391 【模板】文艺平衡树FHQ treap
P3391 [模板]文艺平衡树(Splay) 题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转 ...
- 洛谷.3391.[模板]文艺平衡树(Splay)
题目链接 //注意建树 #include<cstdio> #include<algorithm> const int N=1e5+5; //using std::swap; i ...
随机推荐
- oracle 远程导入导出(本地win)
导出 exp hongtastock_account/hongtastock_account@192.168.1.22/orcl file=D:\hongta\hongtastock_account. ...
- 【译】.NET Core 2.2 Preview 2 发布
原文出自.Net Blog Announcing .NET Core 2.2 Preview 2 今天,我们宣布推出.NET Core 2.2 Preview 2.我们有很多重要改进要和你分享,而且我 ...
- KVM -> 虚拟机管理&console登录_02
1.KVM虚拟机管理操作 virsh命令常用参数总结 1.开机关机: virsh list (只可以查看运行的虚拟机) virsh list --all (全部都可以查看) 开机与关机: virsh ...
- linux设备模型:扩展篇
Linux设备模型组件:总线 一.定义:总线是不同IC器件之间相互通讯的通道;在计算机中,一个总线就是处理器与一个或多个不同外设之间的通讯通道;为了设备模型的目的,所有的设备都通过总线相互连接,甚至 ...
- SQL开发测试使用基础
目录 一.客户端配置与使用 1.oracle(PLSQL Developer) 2.hive(hive cli)及命令 3.postgre(pgAdmin) 二.注意事项及基础 ...
- v-bind绑定属性样式——class的三种绑定方式
1.布尔值的绑定方式 <div id="demo"> <span v-bind:class="{‘class-a‘:isA ,‘class-b‘:isB ...
- graphql详解
随着系统业务量的增大不同的应用和系统共同使用着许多的服务api,而随着业务的变化和发展,不同的应用对相同资源的不同使用方法最终会导致需要维护的服务api数量呈现爆炸式的增长,比如我试着跑了下我们自己业 ...
- JavaScript脚本的两种放置方式
JavaScript脚本的两种放置方式 1在body里用 script标签引用 2 直接写在<script></script>标签之中
- xxx is not in sudoers file 解决(转)
解决方案:首需要切换到root身份$su -(注意有- ,这和su是不同的,在用命令"su"的时候只是切换到root,但没有把root的环境变量传过去,还是当前用户的环境变量,用& ...
- C++传值、传引用
C++传值.传引用 C++的函数参数传递方式,可以是传值方式,也可以是传引用方式.传值的本质是:形参是实参的一份复制.传引用的本质是:形参和实参是同一个东西. 传值和传引用,对大多数常见类型都是适用的 ...