题目链接

//注意反转时先分裂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)的更多相关文章

  1. [洛谷P3391] 文艺平衡树 (Splay模板)

    初识splay 学splay有一段时间了,一直没写...... 本题是splay模板题,维护一个1~n的序列,支持区间翻转(比如1 2 3 4 5 6变成1 2 3 6 5 4),最后输出结果序列. ...

  2. BZOJ3223/洛谷P3391 - 文艺平衡树

    BZOJ链接 洛谷链接 题意 模板题啦~2 代码 //文艺平衡树 #include <cstdio> #include <algorithm> using namespace ...

  3. 洛谷 P3391 文艺平衡树

    题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 --b ...

  4. 洛谷P3391文艺平衡树(Splay)

    题目传送门 转载自https://www.cnblogs.com/yousiki/p/6147455.html,转载请注明出处 经典引文 空间效率:O(n) 时间效率:O(log n)插入.查找.删除 ...

  5. 洛谷P3391 文艺平衡树 (Splay模板)

    模板题. 注意标记即可,另外,涉及区间翻转操作,记得设立首尾哨兵. 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int ...

  6. BZOJ3224/洛谷P3391 - 普通平衡树(Splay)

    BZOJ链接 洛谷链接 题意简述 模板题啦~ 代码 //普通平衡树(Splay) #include <cstdio> int const N=1e5+10; int rt,ndCnt; i ...

  7. 洛谷P3369普通平衡树(Treap)

    题目传送门 转载自https://www.cnblogs.com/fengzhiyuan/articles/7994428.html,转载请注明出处 Treap 简介 Treap 是一种二叉查找树.它 ...

  8. P3391 【模板】文艺平衡树FHQ treap

    P3391 [模板]文艺平衡树(Splay) 题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转 ...

  9. 洛谷.3391.[模板]文艺平衡树(Splay)

    题目链接 //注意建树 #include<cstdio> #include<algorithm> const int N=1e5+5; //using std::swap; i ...

随机推荐

  1. python 退出程序的方式

    python程序退出方式[sys.exit() os._exit() os.kill() os.popen(...)] 知乎说明 http://www.zhihu.com/question/21187 ...

  2. Python Tools for Machine Learning

    Python Tools for Machine Learning Python is one of the best programming languages out there, with an ...

  3. CONFIG_DEBUG_USER【转】

    转自:https://blog.csdn.net/adaptiver/article/details/12778621 关于CONFIG_DEBUG_USER 把menuconfig中查到的 CONF ...

  4. plsql developer如何自定义快捷键

    首选项 用户界面 编辑器 自动替换 选择替换文件,文件内容: sf=select * from df=delete from

  5. 现代C++之理解decltype

     现代C++之理解decltype decltype用于生成变量名或者表达式的类型,其生成的结果有的是显而易见的,可以预测的,容易理解,有些则不容易理解.大多数情况下,与使用模板和auto时进行的类型 ...

  6. MySQL 数据类型(转)

    MySQL 数据类型 在 MySQL 中,有三种主要的类型:文本.数字和日期/时间类型. Text 类型: 数据类型 描述 备注 CHAR(size) 保存固定长度的字符串(可包含字母.数字以及特殊字 ...

  7. jmeter之beanshell取出需要参数,传递给下个请求

    jmeter之beanshell取出需要参数,传递给下个请求 事件背景: 上周同事用jmeter录制脚本,录制成功回放后,并没有达到自己想要的结果. ps:他想从数据库取出某个字段值,然后对数据库做操 ...

  8. 如何把JS对象转成数组

    1. 前言 首先,当JS对象是键值对的情况时(Json对象),因为数组时以数字为索引的,所以只能把JS对象中的Key或者Value组成数组使用. 2. 样例如下: var obj={"one ...

  9. expdp和impdp 使用注意事项

    使用EXPDP和IMPDP时应该注意的事项: EXP和IMP是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用. EXPDP和IMPDP是服务端的工具程序,他们只能在ORACLE服务端使用, ...

  10. asp.net core 操作误区

    更新时提示数据变化错误 在更新事件中提示下面错误,在网上找了一下,大部分都是说是冲突问题,但是测试时同时只有一个客户端在进行操作,不应该会有冲突问题,后来发现编辑加载时的ID,和更新提交时的ID不同了 ...