Play with Chain

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5426    Accepted Submission(s): 2185

Problem Description
YaoYao is fond of playing his chains. He has a chain containing n diamonds on it. Diamonds are numbered from 1 to n.
At first, the diamonds on the chain is a sequence: 1, 2, 3, …, n.
He will perform two types of operations:
CUT a b c: He will first cut down the chain from the ath diamond to the bth diamond. And then insert it after the cth diamond on the remaining chain.
For example, if n=8, the chain is: 1 2 3 4 5 6 7 8; We perform “CUT 3 5 4”, Then we first cut down 3 4 5, and the remaining chain would be: 1 2 6 7 8. Then we insert “3 4 5” into the chain before 5th diamond, the chain turns out to be: 1 2 6 7 3 4 5 8.

FLIP a b: We first cut down the chain from the ath diamond to the bth diamond. Then reverse the chain and put them back to the original position.
For example, if we perform “FLIP 2 6” on the chain: 1 2 6 7 3 4 5 8. The chain will turn out to be: 1 4 3 7 6 2 5 8

He wants to know what the chain looks like after perform m operations. Could you help him? 

 
Input
There will be multiple test cases in a test data. 
For each test case, the first line contains two numbers: n and m (1≤n, m≤3*100000), indicating the total number of diamonds on the chain and the number of operations respectively.
Then m lines follow, each line contains one operation. The command is like this:
CUT a b c // Means a CUT operation, 1 ≤ a ≤ b ≤ n, 0≤ c ≤ n-(b-a+1).
FLIP a b    // Means a FLIP operation, 1 ≤ a < b ≤ n.
The input ends up with two negative numbers, which should not be processed as a case.
 
Output
For each test case, you should print a line with n numbers. The ith number is the number of the ith diamond on the chain.
 
Sample Input
8 2
CUT 3 5 4
FLIP 2 6
-1 -1
 
Sample Output
1 4 3 7 6 2 5 8
  1.  
  1. /*
  2. hdu3487 splay树
  3. cut是将一串数字复制到另外一个位置,flip是将一串数字逆序
  4. cut直接进行一次删除插入即可,flip则是给个转换标记然后更新
  5. hhh-2016-02-20 06:50:15
  6. */
  7.  
  8. #include <functional>
  9. #include <iostream>
  10. #include <cstdio>
  11. #include <cstdlib>
  12. #include <cstring>
  13. #include <algorithm>
  14. #include <map>
  15. #include <cmath>
  16. using namespace std;
  17. typedef long long ll;
  18. typedef long double ld;
  19. #define key_value ch[ch[root][1]][0]
  20. const int maxn = 300010;
  21.  
  22. int ch[maxn][2];
  23. int rev[maxn],a[maxn];
  24. int pre[maxn],key[maxn],siz[maxn];
  25. int root,TOT,cnt,n;
  26.  
  27. void push_up(int r)
  28. {
  29. int lson = ch[r][0],rson = ch[r][1];
  30. siz[r] = siz[lson] + siz[rson] + 1;
  31. }
  32.  
  33. void update_rev(int r)
  34. {
  35. if(!r) return ;
  36. swap(ch[r][0],ch[r][1]);
  37. rev[r] ^= 1;
  38. }
  39.  
  40. void push_down(int r)
  41. {
  42. if(rev[r])
  43. {
  44. update_rev(ch[r][0]);
  45. update_rev(ch[r][1]);
  46. rev[r] = 0;
  47. }
  48. }
  49.  
  50. void inOrder(int r)
  51. {
  52. if(!r)return;
  53. push_down(r);
  54. inOrder(ch[r][0]);
  55. if(cnt >=1 && cnt <= n)
  56. {
  57. printf("%d",key[r]);
  58. if(cnt < n)printf(" ");
  59. else printf("\n");
  60. }
  61. cnt++;
  62. inOrder(ch[r][1]);
  63. }
  64.  
  65. void debug()
  66. {
  67. cnt = 0;
  68. inOrder(root);
  69. }
  70.  
  71. void NewNode(int &r,int far,int k)
  72. {
  73. r = ++TOT;
  74. pre[r] = far;
  75. ch[r][0] = ch[r][1] = 0;
  76. key[r] = k;
  77. siz[r] = 1;
  78. rev[r] = 0;
  79. }
  80.  
  81. void rotat(int x,int kind)
  82. {
  83. int y = pre[x];
  84. push_down(y);
  85. push_down(x);
  86. ch[y][!kind] = ch[x][kind];
  87. pre[ch[x][kind]] = y;
  88. if(pre[y])
  89. ch[pre[y]][ch[pre[y]][1]==y] = x;
  90. pre[x] = pre[y];
  91. ch[x][kind] = y;
  92. pre[y] = x;
  93. push_up(y);
  94. }
  95.  
  96. void build(int &x,int l,int r,int far)
  97. {
  98. if(l > r) return ;
  99. int mid = (l+r) >>1;
  100. NewNode(x,far,mid);
  101. build(ch[x][0],l,mid-1,x);
  102. build(ch[x][1],mid+1,r,x);
  103. push_up(x);
  104. }
  105.  
  106. void splay(int r,int goal)
  107. {
  108. push_down(r);
  109. while(pre[r] != goal)
  110. {
  111. if(pre[pre[r]] == goal)
  112. {
  113. push_down(pre[r]);
  114. push_down(r);
  115. rotat(r,ch[pre[r]][0] == r);
  116. }
  117. else
  118. {
  119. push_down(pre[pre[r]]);
  120. push_down(pre[r]);
  121. push_down(r);
  122. int y = pre[r];
  123. int kind = ch[pre[y]][0] == y;
  124. if(ch[y][kind] == r)
  125. {
  126. rotat(r,!kind);
  127. rotat(r,kind);
  128. }
  129. else
  130. {
  131. rotat(y,kind);
  132. rotat(r,kind);
  133. }
  134. }
  135. }
  136. push_up(r);
  137. if(goal == 0)
  138. root = r;
  139. }
  140.  
  141. int get_kth(int r,int k)
  142. {
  143. push_down(r);
  144. int t = siz[ch[r][0]]+1;
  145. if(t == k) return r;
  146. if(t > k) return get_kth(ch[r][0],k);
  147. else return get_kth(ch[r][1],k-t);
  148. }
  149.  
  150. int CUT(int l,int r,int b)
  151. {
  152. splay(get_kth(root,l),0);
  153. splay(get_kth(root,r+2),root);
  154. int tmp = key_value;
  155. pre[key_value] = 0;
  156. key_value = 0;
  157. push_up(ch[root][1]);
  158. push_up(root);
  159.  
  160. splay(get_kth(root,b+1),0);
  161. splay(get_kth(root,b+2),root);
  162. pre[tmp] = ch[root][1];
  163. key_value = tmp;
  164. push_up(ch[root][1]);
  165. push_up(root);
  166. //debug();
  167. }
  168.  
  169. void Reverse(int l,int r)
  170. {
  171. splay(get_kth(root,l),0);
  172. splay(get_kth(root,r+2),root);
  173. update_rev(key_value);
  174. push_up(ch[root][1]);
  175. push_up(root);
  176. //debug();
  177. }
  178.  
  179. void ini(int n)
  180. {
  181. TOT = root = 0;
  182. ch[root][0] = ch[root][1] = pre[root] = siz[root] = 0;
  183. rev[root] = 0;
  184. NewNode(root,0,-1);
  185. NewNode(ch[root][1],root,-1);
  186. build(key_value,1,n,ch[root][1]);
  187. //inOrder(root);
  188. //printf("\n");
  189. push_up(ch[root][1]);
  190. push_up(root);
  191. }
  192.  
  193. int main()
  194. {
  195. int q;
  196. while(scanf("%d%d",&n,&q) != EOF)
  197. {
  198.  
  199. if(n == -1 && q == -1)
  200. break;
  201. ini(n);
  202. for(int i =1; i <= q; i++)
  203. {
  204. char qry[10];
  205. scanf("%s",qry);
  206. int a,b,len;
  207. if(qry[0] == 'C')
  208. {
  209. scanf("%d%d%d",&a,&len,&b);
  210. CUT(a,len,b);
  211. }
  212. else
  213. {
  214. scanf("%d%d",&a,&b);
  215. Reverse(a,b);
  216. }
  217. }
  218. cnt = 0;
  219. inOrder(root);
  220. }
  221. return 0;
  222. }

  

hdu3487 splay树的更多相关文章

  1. Hdu-3487 Splay树,删除,添加,Lazy延迟标记操作

    HDU_3487 题意:给出n和q,n代表1-n的序列,接下来q有两种操作,Cut a b c:表示把区间[a,b]截掉然后放在第c个数的后面,Flip a b 表示把区间[a,b]反转,经过一系列的 ...

  2. Splay树-Codevs 1296 营业额统计

    Codevs 1296 营业额统计 题目描述 Description Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司 ...

  3. ZOJ3765 Lights Splay树

    非常裸的一棵Splay树,需要询问的是区间gcd,但是区间上每个数分成了两种状态,做的时候分别存在val[2]的数组里就好.区间gcd的时候基本上不支持区间的操作了吧..不然你一个区间里加一个数gcd ...

  4. Splay树再学习

    队友最近可能在学Splay,然后让我敲下HDU1754的题,其实是很裸的一个线段树,不过用下Splay也无妨,他说他双旋超时,单旋过了,所以我就敲来看下.但是之前写的那个Splay越发的觉得不能看,所 ...

  5. 暑假学习日记:Splay树

    从昨天开始我就想学这个伸展树了,今天花了一个上午2个多小时加下午2个多小时,学习了一下伸展树(Splay树),学习的时候主要是看别人博客啦~发现下面这个博客挺不错的http://zakir.is-pr ...

  6. 1439. Battle with You-Know-Who(splay树)

    1439 路漫漫其修远兮~ 手抄一枚splay树 长长的模版.. 关于spaly树的讲解   网上很多随手贴一篇 貌似这题可以用什么bst啦 堆啦 平衡树啦 等等 这些本质都是有共同点的 查找.删除特 ...

  7. 伸展树(Splay树)的简要操作

    伸展树(splay树),是二叉排序树的一种.[两个月之前写过,今天突然想写个博客...] 伸展树和一般的二叉排序树不同的是,在每次执行完插入.查询.删除等操作后,都会自动平衡这棵树.(说是自动,也就是 ...

  8. [Splay伸展树]splay树入门级教程

    首先声明,本教程的对象是完全没有接触过splay的OIer,大牛请右上角.. 首先引入一下splay的概念,他的中文名是伸展树,意思差不多就是可以随意翻转的二叉树 PS:百度百科中伸展树读作:BoGa ...

  9. hdu 3436 splay树+离散化*

    Queue-jumpers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

随机推荐

  1. 腾讯云服务器上安装phstudy和lnmp

    phpstudy的安装:wget -c http://lamp.phpstudy.net/phpstudy.bin chmod +x phpstudy.bin #权限设置./phpstudy.bin ...

  2. [Oracle]undo表空间使用量为100%

    在Toad中发现undo表空间undotbs1使用量已经达到100%,但是奇怪的是数据库并没有hang住,依然可以正常运转 通过Oracle提供的EM查看undotbs1表空间的使用,也达到了78.8 ...

  3. TortoiseGit安装与使用

    公司的源码是在码云上,平时进行项目源码管理和团队开发都会使用到GIT,花了一天时间才将Git搞明白,这是一个工具,我在这里就简单说一下,其安装使用方法,也是对自己学习的总结;本文章适合于刚接触GIT的 ...

  4. SpringBoot的配置文件加载顺序和使用方式

    1.bootstrap.properties bootstrap.properties 配置文件是由"根"上下文优先加载,程序启动之初就感知 如:Spring Cloud Conf ...

  5. matlab等高线绘制

    参考代码: figure;// Figure建立新的图形 z=double(z); x=1:length(z); y=x; [X2,Y2]=meshgrid(x,y); subplot(121); [ ...

  6. python全栈开发-hashlib模块(数据加密)、suprocess模块、xml模块

    一.hashlib模块 1.什么叫hash:hash是一种算法(3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法 ...

  7. vue-router动态路由 刷新页面 静态资源没有加载的原因

    在做项目的时候,发现刷新页面的时候,静态路由没有加载,度娘了一圈,终于解决了. vue-router使用history模式+使用嵌套路由: 访问路由和嵌套路由页面,显示正常,但是刷新页面的时候,嵌套路 ...

  8. Python面向对象——多重继承大揭秘

    1如果如图所示使用多重继承,我们将看到什么 2我们看到了基类被执行了两次Baseclass 3代码验证吧 class BaseClass: num_base_calls = 0 def call_me ...

  9. ubuntu安装mysql并修改编码为utf-8

    参考地址:ubuntu中文 sudo apt-get install mysql-server mysql-client -y # 中途会要求输入一下root用户的密码 编辑/etc/mysql/co ...

  10. 从让 HTTPS 更安全出发,聊聊 HTTPS

    随着公众对网络安全的日益关注,各种网络安全防护手段层出不穷.HTTPS Everywhere作为提升HTTPS安全性的有效手段,日前安全性与实用性再次得到了加强. HTTPS虽然可以有效提升用户浏览网 ...