题意:

区间翻转,切割,插入

  1. // File Name: ACM/HDU/3487.cpp
  2. // Author: Zlbing
  3. // Created Time: 2013年08月10日 星期六 21时35分28秒
  4.  
  5. #include<iostream>
  6. #include<string>
  7. #include<algorithm>
  8. #include<cstdlib>
  9. #include<cstdio>
  10. #include<set>
  11. #include<map>
  12. #include<vector>
  13. #include<cstring>
  14. #include<stack>
  15. #include<cmath>
  16. #include<queue>
  17. using namespace std;
  18. #define CL(x,v); memset(x,v,sizeof(x));
  19. #define INF 0x3f3f3f3f
  20. #define LL long long
  21. #define REP(i,r,n) for(int i=r;i<=n;i++)
  22. #define RREP(i,n,r) for(int i=n;i>=r;i--)
  23. #define L ch[x][0]
  24. #define R ch[x][1]
  25. #define KT (ch[ ch[rt][1] ][0])
  26. const int MAXN = 3e5+;
  27. struct SplayTree {
  28. int sz[MAXN];
  29. int ch[MAXN][];
  30. int pre[MAXN];
  31. int rt,top;
  32. inline void down(int x){
  33. if(flip[x]) {
  34. flip[ L ] ^= ;
  35. flip[ R ] ^= ;
  36. swap(L,R);
  37. flip[x]=;
  38. }
  39. }
  40. inline void up(int x){
  41. sz[x]=+sz[ L ] + sz[ R ];
  42. }
  43. inline void Rotate(int x,int f){
  44. int y=pre[x];
  45. down(y);
  46. down(x);
  47. ch[y][!f] = ch[x][f];
  48. pre[ ch[x][f] ] = y;
  49. pre[x] = pre[y];
  50. if(pre[x]) ch[ pre[y] ][ ch[pre[y]][] == y ] =x;
  51. ch[x][f] = y;
  52. pre[y] = x;
  53. up(y);
  54. }
  55. inline void Splay(int x,int goal){//将x旋转到goal的下面
  56. down(x);////防止pre[x]就是目标点,下面的循环就进不去了,x的信息就传不下去了
  57. while(pre[x] != goal){
  58. down(pre[pre[x]]); down(pre[x]);down(x);//在旋转之前需要先下传标记,因为节点的位置可能会发生改变
  59. if(pre[pre[x]] == goal) Rotate(x , ch[pre[x]][] == x);
  60. else {
  61. int y=pre[x],z=pre[y];
  62. int f = (ch[z][]==y);
  63. if(ch[y][f] == x) Rotate(x,!f),Rotate(x,f);
  64. else Rotate(y,f),Rotate(x,f);
  65. }
  66. }
  67. up(x);
  68. if(goal==) rt=x;
  69. }
  70. inline void RTO(int k,int goal){//将第k位数旋转到goal的下面
  71. int x=rt;
  72. down(x);
  73. while(sz[ L ]+ != k) {
  74. if(k < sz[ L ] + ) x=L;
  75. else {
  76. k-=(sz[ L ]+);
  77. x = R;
  78. }
  79. down(x);
  80. }
  81. Splay(x,goal);
  82. }
  83. void vist(int x){
  84. if(x){
  85. printf("结点%2d : 左儿子 %2d 右儿子 %2d %2d flip:%d\n",x,L,R,val[x],flip[x]);
  86. vist(L);
  87. vist(R);
  88. }
  89. }
  90. void Newnode(int &x,int c,int f){
  91. x=++top;flip[x]=;
  92. L = R = ; pre[x] = f;
  93. sz[x]=; val[x]=c;
  94. }
  95. inline void build(int &x,int l,int r,int f){
  96. if(l>r) return ;
  97. int m=(l+r)>>;
  98. Newnode(x,m,f);
  99. build(L , l , m- , x);
  100. build(R , m+ , r , x);
  101. pre[x]=f;
  102. up(x);
  103. }
  104.  
  105. inline void init(int n){
  106. ch[][]=ch[][]=pre[]=sz[]=;
  107. rt=top=; flip[]=; val[]=;
  108. Newnode(rt,-,);
  109. Newnode(ch[rt][],-,rt);
  110. sz[rt]=;
  111. build(KT,,n,ch[rt][]);
  112. }
  113. void Del(){
  114. int t=rt;
  115. if(ch[rt][]) {
  116. rt=ch[rt][];
  117. RTO(,);
  118. ch[rt][]=ch[t][];
  119. if(ch[rt][]) pre[ch[rt][]]=rt;
  120. }
  121. else rt=ch[rt][];
  122. pre[rt]=;
  123. up(rt);
  124. }
  125. void solve1(int a,int b,int c)
  126. {
  127. RTO(a,);
  128. RTO(b+,rt);
  129. int tmp=KT;
  130. KT=;
  131. up(ch[rt][]);
  132. up(rt);
  133.  
  134. RTO(c+,);
  135. RTO(c+,rt);
  136. KT=tmp;
  137. pre[tmp]=ch[rt][];
  138. up(ch[rt][]);
  139. up(rt);
  140. }
  141. void solve2(int a,int b)
  142. {
  143. RTO(a,);
  144. RTO(b+,rt);
  145. flip[KT]^=;
  146. }
  147. void print(int x)
  148. {
  149. if(x)
  150. {
  151. down(x);
  152. print(L);
  153. if(val[x]!=-)
  154. {
  155. if(flag)printf(" ");
  156. flag=;
  157. printf("%d",val[x]);
  158. }
  159. print(R);
  160. }
  161. }
  162. void out()
  163. {
  164. flag=;
  165. print(rt);
  166. printf("\n");
  167. }
  168. int flip[MAXN];
  169. int val[MAXN];
  170. int flag;
  171. }spt;
  172.  
  173. int main()
  174. {
  175. int n,m;
  176. while(~scanf("%d%d",&n,&m))
  177. {
  178. if(n==-)break;
  179. char op[];
  180. int a,b,c;
  181. spt.init(n);
  182. REP(i,,m)
  183. {
  184. scanf("%s",op);
  185. if(op[]=='C')
  186. {
  187. scanf("%d%d%d",&a,&b,&c);
  188. spt.solve1(a,b,c);
  189. }
  190. else
  191. {
  192. scanf("%d%d",&a,&b);
  193. spt.solve2(a,b);
  194. }
  195.  
  196. }
  197. spt.out();
  198. }
  199. return ;
  200. }

hdu-3487-Play with Chain-(splay 区间翻转,切割,插入)的更多相关文章

  1. HDU 3487 Play with Chain | Splay

    Play with Chain Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. HDU 3487 Play with Chain(Splay)

    题目大意 给一个数列,初始时为 1, 2, 3, ..., n,现在有两种共 m 个操作 操作1. CUT a b c 表示把数列中第 a 个到第 b 个从原数列中删除得到一个新数列,并将它添加到新数 ...

  3. HDU 3487 Play with Chain 【Splay】

    1-n的序列,有两种操作: 1,将一段区间翻转 2,将一段区间切下来放到剩余序列的第C个数后 采用延迟更新的方法维护区间的翻转,并维护一个size域. 添加一个最大点和一个最小点,防止出界 翻转时,将 ...

  4. hdu 3487 Play with Chain

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3487 YaoYao is fond of playing his chains. He has a c ...

  5. hdu-1890-Robotic Sort splay区间翻转

    题意: 依次找第i大的数下标pos[i],然后将区间[i,pos[i]]翻转 分析: splay树区间翻转 // File Name: ACM/HDU/1890.cpp // Author: Zlbi ...

  6. bzoj 3223 文艺平衡树 splay 区间翻转

    Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 17715  Solved: 7769[Submit][Status][ ...

  7. bzoj 1251序列终结者 splay 区间翻转,最值,区间更新

    序列终结者 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 4594  Solved: 1939[Submit][Status][Discuss] De ...

  8. Hdu 3487 play the chain

    Description 瑶瑶很喜欢玩项链,她有一根项链上面有很多宝石,宝石从1到n编号. 首先,项链上的宝石的编号组成一个序列:1,2,3,...,n. 她喜欢两种操作: 1.CUT a b c:他会 ...

  9. BZOJ 3223 Splay区间翻转

    思路: 区间翻转的裸题 终于tm理解splay了-- //By SiriusRen #include <cstdio> #include <cstring> #include ...

  10. splay区间翻转

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

随机推荐

  1. 惠普 hpssacli 工具使用

    查看raid卡信息(包括控制器状态.Cache状态.电池状态) # hpssacli ctrl all show status 查看raid详细信息 # hpssacli ctrl slot=0 sh ...

  2. Oracler读取各种格式的相关日期格式

    CREATE OR REPLACE Package Pkg_Stm_Date As     --Purpose:相关日期处理功能包          --获取某一天是第几周     Function ...

  3. mvc Action上面加 [HttpPost]

    mvc  Action上面加 [HttpPost]  意思就是这个action只能响应post请求. 如果发get请求这里是没有响应的

  4. Oracle --1536错误解决(超出表空间)

    --导入数据库时提示 超出表空间限额,1536错误,解决方法:去除限额. 执行:--alter user username quota unlimited on users; 例: alter use ...

  5. 怎样使用svn开发项目

    那么首先什么是svn呢?官方有很好的解释,我说一下个人简单的理解,svn就是开源的版本控制软件, 那么什么是版本呢?简单的说版本就是标记,比如你买了一本书,同样的书名,但是版本不一定一样, 因为里面可 ...

  6. CSS Clip剪切元素动画实例

    1.CSS .fixed { position: fixed; width: 90px; height: 90px; background: red; border: 0px solid blue; ...

  7. 简单讲解iOS应用开发中的MD5加密的相关使用<转>

    这篇文章主要介绍了iOS应用开发中的MD5加密的相关使用,示例代码基于传统的Objective-C,需要的朋友可以参考下 一.简单说明 1.说明 在开发应用的时候,数据的安全性至关重要,而仅仅用POS ...

  8. C#堆栈原理(我有两个例子测试你到底会不会)

    背景 上次写了一篇文章关于try finnally的一些疑问(被我用windows live覆盖了,草),后来经过大神们解释,我明白了在我理解了try.finnally运行原理后,还欠缺的就是关于值类 ...

  9. c++面试(一)

    1.在c++中可以通过"::"来直接操作全局变量. 2.i++与++i效率的比较. (1)內建数据类型时,他们的效率差别不大. (2)自定义数据类型(类等)的情况,(++i)可以返 ...

  10. 赋值,copy和deepcopy

    python的复制,拷贝,和深拷贝. >>> a=[23,3]>>> b=a>>> b.append(234)>>> a[23, ...