题目背景

这是一道经典的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 n 1≤l≤r≤n

输出格式:
输出一行n个数字,表示原始序列经过m次变换后的结果

输入输出样例

输入样例#1:
5 3
1 3
1 3
1 4
输出样例#1: 
4 3 2 1 5

题解:打标记的splay!调了一个上午终于调出来了!继续挖坑~

代码如下:

  1. #include<cmath>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<iostream>
  5. #include<algorithm>
  6. #define N 100010
  7. using namespace std;
  8. struct Splay
  9. {
  10. int key[N],son[N][],size[N],fa[N],tag[N],rt,sz,pos;
  11. inline void push_up(int x)
  12. {
  13. size[x]=size[son[x][]]+size[son[x][]]+;
  14. }
  15. inline void push_down(int x)
  16. {
  17. if(!tag[x])
  18. {
  19. return;
  20. }
  21. tag[x]=;
  22. tag[son[x][]]^=;
  23. tag[son[x][]]^=;
  24. swap(son[x][],son[x][]);
  25. }
  26. inline void rotate(int x)
  27. {
  28. int y=fa[x],z=fa[y],k=(son[y][]==x);
  29. son[z][son[z][]==y]=x;
  30. fa[x]=z;
  31. son[y][!k]=son[x][k];
  32. fa[son[x][k]]=y;
  33. son[x][k]=y;
  34. fa[y]=x;
  35. push_up(y);
  36. }
  37. inline void splay(int x,int goal)
  38. {
  39. for(push_down(x); fa[x]!=goal; rotate(x))
  40. {
  41. int y=fa[x],z=fa[y];
  42. if(z!=goal)
  43. {
  44. (son[y][]==x)^(son[z][]==y)?rotate(x):rotate(y);
  45. }
  46. }
  47. push_up(x);
  48. if(!goal)
  49. {
  50. rt=x;
  51. }
  52. }
  53. inline int kth(int k)
  54. {
  55. for(int x=rt;;)
  56. {
  57. push_down(x);
  58. int y=son[x][];
  59. if(k>size[y]+)
  60. {
  61. k-=size[y]+;
  62. x=son[x][];
  63. }
  64. else
  65. {
  66. if(k>size[y])
  67. {
  68. return x;
  69. }
  70. else
  71. {
  72. x=y;
  73. }
  74. }
  75. }
  76. }
  77. inline void reverse(int l,int r)
  78. {
  79. if(l>r)
  80. {
  81. swap(l,r);
  82. }
  83. int x=kth(l),y=kth(r+);
  84. splay(x,);
  85. splay(y,x);
  86. tag[son[y][]]^=;
  87. }
  88. inline int build(int *a,int father,int l,int r)
  89. {
  90. if(l>r)
  91. {
  92. return ;
  93. }
  94. int mid=(l+r)>>,x=++sz;
  95. fa[x]=father;
  96. key[x]=a[mid];
  97. son[x][]=build(a,x,l,mid-);
  98. son[x][]=build(a,x,mid+,r);
  99. push_up(x);
  100. return x;
  101. }
  102. inline void put(int x)
  103. {
  104. push_down(x);
  105. if(son[x][])
  106. {
  107. put(son[x][]);
  108. }
  109. if(key[x]>-1e9&&key[x]<1e9)
  110. {
  111. printf("%d ",key[x]);
  112. }
  113. if(son[x][])
  114. {
  115. put(son[x][]);
  116. }
  117. }
  118. } splay1;
  119. int main()
  120. {
  121. int n,m,l,r,a[];
  122. scanf("%d%d",&n,&m);
  123. for(int i=; i<=n+; i++)
  124. {
  125. a[i]=i-;
  126. }
  127. a[]=-1e9;
  128. a[n+]=1e9;
  129. splay1.rt=splay1.build(a,,,n+);
  130. for(int i=; i<=m; i++)
  131. {
  132. scanf("%d%d",&l,&r);
  133. splay1.reverse(l,r);
  134. }
  135. splay1.put(splay1.rt);
  136. }

BZOJ3223 文艺平衡树(splay)的更多相关文章

  1. JZYZOJ1998 [bzoj3223] 文艺平衡树 splay 平衡树

    http://172.20.6.3/Problem_Show.asp?id=1998 平衡树区间翻转的板子,重新写一遍,给自己码一个板子. #include<iostream> #incl ...

  2. [bzoj3223]文艺平衡树(splay区间反转模板)

    解题关键:splay模板题. #include<cstdio> #include<cstring> #include<algorithm> #include< ...

  3. [bzoj3223]文艺平衡树——splay

    题意 你应当编写一个数据结构,支持以下操作: 反转一个区间 题解 我们把在数组中的位置当作权值,这样原序列就在这种权值意义下有序,我们考虑使用splay维护. 对于操作rev[l,r],我们首先把l- ...

  4. 【阶梯报告】洛谷P3391【模板】文艺平衡树 splay

    [阶梯报告]洛谷P3391[模板]文艺平衡树 splay 题目链接在这里[链接](https://www.luogu.org/problemnew/show/P3391)最近在学习splay,终于做对 ...

  5. luoguP3391[模板]文艺平衡树(Splay) 题解

    链接一下题目:luoguP3391[模板]文艺平衡树(Splay) 平衡树解析 这里的Splay维护的显然不再是权值排序 现在按照的是序列中的编号排序(不过在这道题目里面就是权值诶...) 那么,继续 ...

  6. BZOJ3223: Tyvj 1729 文艺平衡树 [splay]

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3595  Solved: 2029[Submit][Sta ...

  7. bzoj3223 文艺平衡树 (treap or splay分裂+合并)

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 3313  Solved: 1883 [Submit][S ...

  8. Tyvj P1729 文艺平衡树 Splay

    题目: http://tyvj.cn/p/1729 P1729 文艺平衡树 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 此为平衡树系列第二道:文艺平衡树 ...

  9. BZOJ 3223: Tyvj 1729 文艺平衡树(splay)

    速度居然进前十了...第八... splay, 区间翻转,用一个类似线段树的lazy标记表示是否翻转 ------------------------------------------------- ...

随机推荐

  1. ubuntu下网卡eth1如何修改为eth0

    正常来说,Linux在识别网卡时第一张会是eth0,第二张才是eth1.有时候我们使用虚拟机克隆技术后网卡的信息就会改变,新克隆出来的虚拟主机网卡名字可能变为eth1.无论我们怎么修改都无法改变,这就 ...

  2. 题目1534:数组中第K小的数字 ——二分

    http://ac.jobdu.com/problem.php?pid=1534 给定两个整型数组A和B.我们将A和B中的元素两两相加可以得到数组C.譬如A为[1,2],B为[3,4].那么由A和B中 ...

  3. HTTP API 设计指南

    本指南描述了一系列 HTTP+JSON API 的设计实践, 来自并展开于 Heroku Platform API 的工作.本指南指导着Heroku内部API的开发,我们希望也能对Heroku以外的A ...

  4. .net中webconfig自定义配置

    在configuration节点,也就是文件的根节点下,增加如下节点 <appSettings> <!--<add key="propPath" value ...

  5. 分布式缓存系统 Memcached 状态机之socket连接与派发

    上节已经分析到了主线程中监听socket注册事件和工作线程中连接socket注册事件的回调函数都是event_handler,且event_handler的核心部分都是一个有限状态机:drive_ma ...

  6. 20181101_将WCF寄宿到控制台

    使用管理员权限打开VS2017 2. 创建以下代码进行测试: a)         创建一个空白解决方案 b)         创建三个类库文件 c)         IMathService代码如下 ...

  7. log4net 使用总结- (2)在ASP.NET MVC 中使用

    log4net在ASP.NET MVC中的配置,还有一种配置方式,即不在web.config中,而是单独新建一个log4net.config 在根目录下   第一.引用log4net.dll   第二 ...

  8. Py修行路 python基础(二)变量 字符 列表

    变量 容器 变量名 标记 数据的作用 字符编码 二进制位 = bit1个二进制位是计算机里的最小表示单元 1个字节是计算机里最小的存储单位 8bits = 1Byte =1字节1024Bytes = ...

  9. 科学家开发新AI系统,可读取大脑信息并表达复杂思想

    我们终于找到了一种方法,可以在核磁共振成像的信号中看到这种复杂的想法.美国卡内基梅隆大学的Marcel Just说,思维和大脑活动模式之间的对应关系告诉我们这些想法是如何构建的. 人工智能系统表明,大 ...

  10. Asp.NetCore远程自启动、重启、关闭实现

    一.背景 NetCore作为微服务可以注册到服务中心,服务中心可以远程启动.重启.关闭该微服务 二.实现 1.创建一个NetCore 2.0 WebApi项目 2.创建一个进程去管理NetCore程序 ...