好吧,我承认这是我用来刷随笔数的喵~

这是一道 splay 裸题,但还是有想本傻 X 一样根本不会写 splay 的,于是乎又用 treap 水过了

splay 的常数我还是知道的,所以真是不知道那些 500ms 的人都是怎么跑出来的,跪求大爷指导

近期要学一下可持久化的 AVL 和尝试把 treap 的代码非递归化模板化(话说我真的不知道原始的 treap 怎么写了)

写完后也会发出来的喵~

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. const int size=;
  5. const int inf=0x7FFFFFFF;
  6.  
  7. template <class type>
  8. inline void swap(type & a, type & b)
  9. {type t=a; a=b; b=t;}
  10.  
  11. namespace treap
  12. {
  13. struct node
  14. {
  15. char ch; int size;
  16. int weight;
  17. bool rev;
  18. node * left, * right;
  19. inline node():size() {left=right=this;}
  20. inline void pushdown() {if (rev) swap(left, right), left->rev^=, right->rev^=, rev=;}
  21. inline void maintain() {size=left->size+right->size+;}
  22. };
  23. node * null=new node();
  24. node MEM[size], * PORT=MEM;
  25.  
  26. inline node * newnode(char ch, int size)
  27. {
  28. node * ret=PORT++;
  29. ret->ch=ch; ret->size=size;
  30. ret->rev=;
  31. ret->left=ret->right=null;
  32. return ret;
  33. }
  34. node * newtreap(char * s, int l, int w)
  35. {
  36. if (!l) return null;
  37. node * ret=newnode(s[l>>], l);
  38. ret->weight=rand()%w+;
  39. if (l==) return ret;
  40. ret->left=newtreap(s, l>>, ret->weight);
  41. ret->right=newtreap(s+(l>>)+, l-(l>>)-, ret->weight);
  42. return ret;
  43. }
  44. void split(node * t, int k, node *& l, node *& r)
  45. {
  46. if (t==null) l=r=null;
  47. else
  48. {
  49. t->pushdown();
  50. if (t->left->size<k)
  51. {
  52. l=t;
  53. split(t->right, k-t->left->size-, t->right, r);
  54. l->maintain();
  55. }
  56. else
  57. {
  58. r=t;
  59. split(t->left, k, l, t->left);
  60. r->maintain();
  61. }
  62. }
  63. }
  64. node * merge(node * l, node * r)
  65. {
  66. if (l==null) return r;
  67. if (r==null) return l;
  68. if (l->weight>r->weight)
  69. {
  70. l->pushdown();
  71. l->right=merge(l->right, r);
  72. l->maintain();
  73. return l;
  74. }
  75. else
  76. {
  77. r->pushdown();
  78. r->left=merge(l, r->left);
  79. r->maintain();
  80. return r;
  81. }
  82. }
  83. }
  84.  
  85. int N, mouse;
  86. char str[size];
  87. treap::node * root=treap::newnode(' ', );
  88. void print(treap::node * );
  89. inline void insert(char * , int);
  90. inline void remove(int);
  91. inline void rotate(int);
  92. inline char get();
  93.  
  94. int main()
  95. {
  96. int x;
  97.  
  98. scanf("%d", &N); mouse=;
  99. root->weight=rand();
  100. for ( ;N;N--)
  101. {
  102. scanf("%s", str);
  103. if (str[]=='G') putchar(get()), putchar('\n');
  104. else if (str[]=='M') scanf("%d", &x), mouse=x;
  105. else if (str[]=='P' && mouse) mouse--;
  106. else if (str[]=='N' && mouse<root->size) mouse++;
  107. else if (str[]=='R') scanf("%d", &x), rotate(x);
  108. else if (str[]=='D') scanf("%d", &x), remove(x);
  109. else
  110. {
  111. scanf("%d", &x);
  112. do gets(str); while (!strlen(str));
  113. insert(str, x);
  114. }
  115. }
  116.  
  117. return ;
  118. }
  119. inline void insert(char * s, int l)
  120. {
  121. treap::node * L, * M, * R;
  122. treap::split(root, mouse, L, R);
  123. M=treap::newtreap(s, l, inf);
  124. L=treap::merge(L, M);
  125. root=treap::merge(L, R);
  126. }
  127. inline void remove(int l)
  128. {
  129. treap::node * L, * M, * R;
  130. treap::split(root, mouse, L, R);
  131. treap::split(R, l, M, R);
  132. root=treap::merge(L, R);
  133. }
  134. inline void rotate(int l)
  135. {
  136. treap::node * L, * M, * R;
  137. treap::split(root, mouse, L, R);
  138. treap::split(R, l, M, R);
  139. M->rev^=;
  140. L=treap::merge(L, M);
  141. root=treap::merge(L, R);
  142. }
  143. inline char get()
  144. {
  145. treap::node * i=root;
  146. int k=mouse+;
  147. for ( ; ; )
  148. {
  149. i->pushdown();
  150. if (i->left->size+==k) return i->ch;
  151. if (i->left->size+<k)
  152. {
  153. k-=i->left->size+;
  154. i=i->right;
  155. }
  156. else
  157. i=i->left;
  158. }
  159. }

本傻装B不成反被艹系列

[AHOI 2006][BZOJ 1269]文本编辑器editor的更多相关文章

  1. BZOJ 1269 文本编辑器editor(伸展树)

    题意 https://www.lydsy.com/JudgeOnline/problem.php?id=1269 思路 伸展树(\(\text{splay}\))功能比较齐全的模板,能较好的体现 \( ...

  2. BZOJ 1269 文本编辑器 Splay

    题目大意:维护一个文本编辑器,支持下列操作: 1.将光标移动到某一位置 2.在光标后插入一段字符串 3.删除光标后的一段字符 4.翻转光标后的一段字符 5.输出光标后的一个字符 6.光标-- 7.光标 ...

  3. 【BZOJ】【1269】【AHOI2006】文本编辑器editor

    Splay Splay序列维护的模板题了……为了便于处理边界情况,我们可以先插入两个空格当作最左端和最右端,然后……其实本题主要考察的就是Build.splay和Findkth这三个操作,我们可以实现 ...

  4. BZOJ 1269: [AHOI2006]文本编辑器editor( splay )

    splay..( BZOJ 1507 题目基本相同..双倍经验 ) ------------------------------------------------------------------ ...

  5. BZOJ 1269: [AHOI2006]文本编辑器editor (splay tree)

    1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1213  Solved: 454[Submit ...

  6. AHOI2006文本编辑器editor

    1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1885  Solved: 683[Submit ...

  7. BZOJ_1269&&1507_[AHOI2006]文本编辑器editor&&[NOI2003]Editor

    BZOJ_1269&&1507_[AHOI2006]文本编辑器editor&&[NOI2003]Editor 题意: 分析: splay模拟即可 注意1507的读入格式 ...

  8. BZOJ1269 [AHOI2006]文本编辑器editor 【82行splay】

    1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 4633  Solved: 1782 [Sub ...

  9. [bzoj1269]文本编辑器editor [bzoj1500]维修数列

    1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2540 Solved: 923 [Submit ...

随机推荐

  1. main函数参数的使用

    int main(int argc, char * argv[]) argc: argument count argv:argument vector 其中, char * argv[] 指针数组 c ...

  2. 使用WebClient Post方式模拟上传文件和数据

    假如某网站有个表单,例如(url: http://localhost/login.aspx):帐号  密码 我们需要在程序中提交数据到这个表单,对于这种表单,我们可以使用 WebClient.Uplo ...

  3. Linux内核-链表

    linux内核链表的定义(定义了双向链表,不含数据域) 定义在 /linux-source-3.13.0/include/linux/types.h 头文件中. struct list_head { ...

  4. Mapper类/Reducer类中的setup方法和cleanup方法以及run方法的介绍

    在hadoop的源码中,基类Mapper类和Reducer类中都是只包含四个方法:setup方法,cleanup方法,run方法,map方法.如下所示: 其方法的调用方式是在run方法中,如下所示: ...

  5. JS 防止表单重复提交

    <script type="text/javascript"> var checkSubmitFlg = false; function checkSubmit() { ...

  6. 2016 - 1 -19 初学HTML5 第一天

    1.HTML COMMANDS MHTL commands called elements.Usually, an element has a start tag and an end tag e.g ...

  7. 【LeetCode OJ】Word Ladder I

    Problem Link: http://oj.leetcode.com/problems/word-ladder/ Two typical techniques are inspected in t ...

  8. WPF Step By Step 完整布局介绍

    WPF Step By Step 完整布局介绍 回顾 上一篇,我们介绍了基本控件及控件的重要属性和用法,我们本篇详细介绍WPF中的几种布局容器及每种布局容器的使用场景,当 然这些都是本人在实际项目中的 ...

  9. magento添加分类属性

    在magento中给产品添加自定义属性是很容易实现在后台就可以很轻易添加,但是给分类就不行了,magento本身没有提供给category添加自定义属性.在实际的运用过程中我们想给cagegory添加 ...

  10. magento后台登陆被锁定 索引报错的解决:General error: 1205 Lock wait timeout

    1. magento在索引的时候用shell,有时候会报错: General error: 1205 Lock wait timeout exceeded 这个时候,是因为行锁的原因,在表中您直接用s ...