这种高级数据结构太难搞了.........现在还是先照着别人的代码敲,做模板..........慢慢花时间来弄懂

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cmath>
  4. #include <cstdio>
  5. #include <cstdlib>
  6. #include <cstring>
  7. #include <string>
  8. #include <vector>
  9. #include <set>
  10. #include <queue>
  11. #include <stack>
  12. #include <climits>//形如INT_MAX一类的
  13. #define MAX 301111
  14. #define INF 0x7FFFFFFF
  15. #define REP(i,s,t) for(int i=(s);i<=(t);++i)
  16. #define ll long long
  17. #define mem(a,b) memset(a,b,sizeof(a))
  18. #define mp(a,b) make_pair(a,b)
  19. #define LL(x) x<<1
  20. #define RR(x) x<<1|1
  21. # define eps 1e-5
  22. //#pragma comment(linker, "/STACK:36777216") ///传说中的外挂
  23. using namespace std;
  24.  
  25. int n,q,root,tot,cnt;
  26. int size[MAX],ch[MAX][2],key[MAX],pre[MAX],num[MAX],lazy[MAX],node[MAX];
  27.  
  28. void newnode(int &x, int va,int fa) {
  29. x = ++ tot;
  30. ch[x][0] = ch[x][1] = 0;
  31. pre[x] = fa;
  32. lazy[x] = 0;
  33. key[x] = va;
  34. }
  35.  
  36. void up(int x) {
  37. size[x] = size[ch[x][0]] + size[ch[x][1]] + 1;
  38. }
  39.  
  40. void down(int x) {
  41. if(lazy[x]) {
  42. swap(ch[x][0],ch[x][1]);
  43. lazy[ch[x][0]] ^= 1;
  44. lazy[ch[x][1]] ^= 1;
  45. lazy[x] = 0;
  46. }
  47. }
  48.  
  49. void build(int &x,int L,int R,int fa) {
  50. if(L > R) return ;
  51. int mid = (L + R) >> 1;
  52. newnode(x,mid,fa);
  53. build(ch[x][0],L,mid-1,x);
  54. build(ch[x][1],mid+1,R,x);
  55. up(x);
  56. }
  57.  
  58. void init() {
  59. root = tot = 0;
  60. ch[0][0] = ch[0][1] = pre[0] = lazy[0] = size[0] = 0;
  61. newnode(root,-1,0);
  62. newnode(ch[root][1],-1,root);
  63. size[root] = 2;
  64. build(ch[ch[root][1]][0],1,n,ch[root][1]);
  65. up(ch[root][1]);
  66. up(root);
  67. }
  68.  
  69. void rotate(int x,int kind) { // 0 :左旋 1:右旋
  70. int y = pre[x];
  71. down(y);
  72. down(x);
  73. ch[y][!kind] = ch[x][kind];
  74. pre[ch[x][kind]] = y;
  75. if(pre[y]) ch[pre[y]][ch[pre[y]][1] == y] = x;
  76. pre[x] = pre[y];
  77. ch[x][kind] = y;
  78. pre[y] = x;
  79. up(y);
  80. }
  81.  
  82. void splay(int x,int g) {
  83. down(x);
  84. while(pre[x] != g) {
  85. if(pre[pre[x]] == g) rotate(x,ch[pre[x]][0] == x);
  86. else {
  87. int y = pre[x];
  88. int kind = (ch[pre[y]][0] == y);
  89. if(ch[y][kind] == x) {
  90. rotate(x,!kind) ;
  91. rotate(x,kind);
  92. } else {
  93. rotate(y,kind);
  94. rotate(x,kind);
  95. }
  96. }
  97. }
  98. up(x);
  99. if(g == 0) root = x;
  100. }
  101.  
  102. int get_kth(int x,int k) {
  103. down(x);
  104. int s = size[ch[x][0]];
  105. if(s == k-1) return x;
  106. if(s >= k) return get_kth(ch[x][0],k);
  107. else return get_kth(ch[x][1],k-s-1);
  108. }
  109.  
  110. int get_min(int x) {
  111. down(x);
  112. while(ch[x][0]) {
  113. x = ch[x][0];
  114. down(x);
  115. }
  116. return x;
  117. }
  118.  
  119. int get_max(int x) {
  120. down(x);
  121. while(ch[x][1]) {
  122. x = ch[x][1];
  123. down(x);
  124. }
  125. return x;
  126. }
  127.  
  128. int get_pre(int x) {
  129. int now = ch[x][0];
  130. while(ch[now][1]) {
  131. now = ch[now][1];
  132. }
  133. return now;
  134. }
  135.  
  136. int get_suc(int x) {
  137. int now = ch[x][1];
  138. while(ch[now][0]) {
  139. now = ch[now][0];
  140. }
  141. return now;
  142. }
  143.  
  144. void rev(int l,int r) {
  145. int x = get_kth(root,l);
  146. int y = get_kth(root,r+2);
  147. splay(x,0);
  148. splay(y,root);
  149. lazy[ch[ch[root][1]][0]] ^= 1;
  150. }
  151.  
  152. void cut(int l,int r,int c) {
  153. int x = get_kth(root,l);
  154. int y = get_kth(root,r+2);
  155. splay(x,0);
  156. splay(y,root);
  157. int tmp = ch[ch[root][1]][0];
  158. ch[ch[root][1]][0] = 0;
  159. up(ch[root][1]);
  160. up(root);
  161. int z = get_kth(root,c+1);
  162. splay(z,0);
  163. int m = get_min(ch[root][1]);
  164. splay(m,root);
  165. ch[ch[root][1]][0] = tmp;
  166. pre[ch[ch[root][1]][0]] = ch[root][1];
  167. up(ch[root][1]);
  168. up(root);
  169. }
  170.  
  171. void print(int x) {
  172. if(x == 0) return ;
  173. down(x);
  174. print(ch[x][0]);
  175. if(cnt >= 1 && cnt <= n) {
  176. if(cnt > 1) printf(" ");
  177. printf("%d",key[x]);
  178. }
  179. cnt ++;
  180. print(ch[x][1]);
  181. }
  182.  
  183. char str[11];
  184. int a,b,c;
  185. int main() {
  186. while(scanf("%d%d",&n,&q) != EOF) {
  187. if(n == -1 && q == -1) break;
  188. init();
  189. while(q--) {
  190. scanf("%s",str);
  191. if(str[0] == 'C') {
  192. scanf("%d%d%d",&a,&b,&c);
  193. cut(a,b,c);
  194. }
  195. if(str[0] == 'F') {
  196. scanf("%d%d",&a,&b);
  197. rev(a,b);
  198. }
  199. }
  200. cnt = 0;
  201. print(root);
  202. puts("");
  203. }
  204. return 0;
  205. }

HDU 3478 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 3436 splay树+离散化*

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

  4. hdu 1890 splay树

    Robotic Sort Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  5. hdu3487 splay树

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

  6. Splay树学习

    首先给出一论文讲的很好: http://www.docin.com/p-63165342.html http://www.docin.com/p-62465596.html 然后给出模板胡浩大神的模板 ...

  7. HDU 3966 & POJ 3237 & HYSBZ 2243 树链剖分

    树链剖分是一个很固定的套路 一般用来解决树上两点之间的路径更改与查询 思想是将一棵树分成不想交的几条链 并且由于dfs的顺序性 给每条链上的点或边标的号必定是连着的 那么每两个点之间的路径都可以拆成几 ...

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

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

  9. ZOJ3765 Lights Splay树

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

随机推荐

  1. java--多线程之Thread继承

    多线程,是java的特殊机制.所谓线程就是程序执行的流程.“多线程”就是可以在同一时刻能够执行多个程序块(注意,是程序块,而不是程序),这样一来就可以使得程序的执行速度大大增加. package Te ...

  2. Python中__init__方法介绍

    本文介绍Python中__init__方法的意义.         __init__方法在类的一个对象被建立时,马上运行.这个方法可以用来对你的对象做一些你希望的 初始化 .注意,这个名称的开始和结尾 ...

  3. 基于visual Studio2013解决C语言竞赛题之0806平均分

     题目

  4. C++ map排序(按照value值排序)_glp_hit_新浪博客

    C++ map排序(按照value值排序)_glp_hit_新浪博客     C++ map排序(按照value值排序)    (2012-07-12 14:19:51)    转载▼    标签:  ...

  5. WSGI详解

    WSGI接口 了解了HTTP协议和HTML文档,我们其实就明白了一个Web应用的本质就是: 浏览器发送一个HTTP请求: 服务器收到请求,生成一个HTML文档: 服务器把HTML文档作为HTTP响应的 ...

  6. 基于visual Studio2013解决面试题之0909移动星号

     题目

  7. GAE+bottle+jinja2+beaker快速开发demo - Python,GAE - language - ITeye论坛

    GAE+bottle+jinja2+beaker快速开发demo - Python,GAE - language - ITeye论坛     :GAE+bottle+jinja2+beaker快速开发 ...

  8. Spark SQL 源代码分析之 In-Memory Columnar Storage 之 in-memory query

    /** Spark SQL源代码分析系列文章*/ 前面讲到了Spark SQL In-Memory Columnar Storage的存储结构是基于列存储的. 那么基于以上存储结构,我们查询cache ...

  9. Java 通过 BufferReader 实现 文件 写入读取 示例

    package com.javatest.techzero.gui; import java.io.BufferedReader; import java.io.File; import java.i ...

  10. Python全栈开发

    Python全栈开发 一文让你彻底明白Python装饰器原理,从此面试工作再也不怕了. 一.装饰器 装饰器可以使函数执行前和执行后分别执行其他的附加功能,这种在代码运行期间动态增加功能的方式,称之为“ ...