3223: Tyvj 1729 文艺平衡树

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 3884  Solved: 2235
[Submit][Status][Discuss]

Description

您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1

Input

第一行为n,m n表示初始序列有n个数,这个序列依次是(1,2……n-1,n)  m表示翻转操作次数
接下来m行每行两个数[l,r] 数据保证 1<=l<=r<=n

Output

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

Sample Input

5 3

1 3

1 3

1 4

Sample Output

4 3 2 1 5

HINT

N,M<=100000

Source

[Submit][Status][Discuss]


    不是特别难,打个lazy标记就行了,详见[Splay]

  1. /**
  2. * bzoj
  3. * Problem#3223
  4. * Accepted
  5. * Time:2012ms
  6. * Memory:4336k
  7. */
  8. #include<iostream>
  9. #include<fstream>
  10. #include<sstream>
  11. #include<cstdio>
  12. #include<cstdlib>
  13. #include<cstring>
  14. #include<ctime>
  15. #include<cctype>
  16. #include<cmath>
  17. #include<algorithm>
  18. #include<stack>
  19. #include<queue>
  20. #include<set>
  21. #include<map>
  22. #include<vector>
  23. using namespace std;
  24. typedef bool boolean;
  25. #define smin(a, b) (a) = min((a), (b))
  26. #define smax(a, b) (a) = max((a), (b))
  27. template<typename T>
  28. inline void readInteger(T& u){
  29. char x;
  30. int aFlag = ;
  31. while(!isdigit((x = getchar())) && x != '-' && x != -);
  32. if(x == -) return;
  33. if(x == '-'){
  34. x = getchar();
  35. aFlag = -;
  36. }
  37. for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
  38. ungetc(x, stdin);
  39. u *= aFlag;
  40. }
  41.  
  42. template<typename T>
  43. class SplayNode {
  44. public:
  45. T data;
  46. int s;
  47. boolean lazy;
  48. SplayNode* next[];
  49. SplayNode* father;
  50. SplayNode():s(), lazy(){
  51. memset(next, , sizeof(next));
  52. }
  53. SplayNode(T data, SplayNode* father):data(data), father(father), s(), lazy(){
  54. memset(next, , sizeof(next));
  55. }
  56. int cmp(T a){
  57. if(a == data) return -;
  58. return (a > data) ? () : ();
  59. }
  60. int getWhich(SplayNode* p){
  61. return (next[] == p) ? () : ();
  62. }
  63. void maintain(){
  64. s = ;
  65. for(int i = ; i < ; i++)
  66. if(next[i] != NULL)
  67. s += next[i]->s;
  68. }
  69. void pushDown(){
  70. swap(next[], next[]);
  71. for(int i = ; i < ; i++)
  72. if(next[i] != NULL)
  73. next[i]->lazy ^= ;
  74. lazy = false;
  75. }
  76. };
  77.  
  78. template<typename T>
  79. class Splay {
  80. protected:
  81. inline static void rotate(SplayNode<T>*& node, int d){
  82. SplayNode<T> *father = node->father;
  83. SplayNode<T> *newRoot = node->next[d ^ ];
  84. if(newRoot->lazy) newRoot->pushDown();
  85. node->next[d ^ ] = newRoot->next[d];
  86. node->father = newRoot;
  87. newRoot->next[d] = node;
  88. newRoot->father = father;
  89. if(node->next[d ^ ] != NULL) node->next[d ^ ]->father = node;
  90. if(father != NULL) father->next[father->getWhich(node)] = newRoot;
  91. node->maintain();
  92. node->father->maintain();
  93. }
  94.  
  95. static SplayNode<T>* insert(SplayNode<T>*& node, SplayNode<T>* father, T data){
  96. if(node == NULL){
  97. node = new SplayNode<T>(data, father);
  98. return node;
  99. }
  100. int d = node->cmp(data);
  101. if(d == -) return NULL;
  102. SplayNode<T>* res = insert(node->next[d], node, data);
  103. if(res != NULL) node->maintain();
  104. return res;
  105. }
  106.  
  107. static SplayNode<T>* findKth(SplayNode<T>*& node, int k){
  108. if(node->lazy) node->pushDown();
  109. int ls = (node->next[] != NULL) ? (node->next[]->s) : ();
  110. if(k >= ls + && k <= ls + ) return node;
  111. if(k <= ls) return findKth(node->next[], k);
  112. return findKth(node->next[], k - ls - );
  113. }
  114.  
  115. public:
  116. SplayNode<T> *root;
  117.  
  118. Splay(){ }
  119.  
  120. inline void splay(SplayNode<T>* node, SplayNode<T>* father){
  121. if(node == father) return;
  122. while(node->father != father){
  123. SplayNode<T>* f = node->father;
  124. int fd = f->getWhich(node);
  125. SplayNode<T>* ff = f->father;
  126. if(ff == father){
  127. rotate(f, fd ^ );
  128. break;
  129. }
  130. int ffd = ff->getWhich(f);;
  131. if(ffd == fd){
  132. rotate(ff, ffd ^ );
  133. rotate(f, fd ^ );
  134. }else{
  135. rotate(f, fd ^ );
  136. rotate(ff, ffd ^ );
  137. }
  138. }
  139. if(father == NULL)
  140. root = node;
  141. }
  142.  
  143. inline SplayNode<T>* insert(T data){
  144. SplayNode<T>* res = insert(root, NULL, data);
  145. if(res != NULL) splay(res, NULL);
  146. return res;
  147. }
  148.  
  149. inline SplayNode<T>* findKth(int k, SplayNode<T>* father){
  150. if(k <= || k > root->s) return NULL;
  151. SplayNode<T>* p = findKth(root, k);
  152. splay(p, father);
  153. return p;
  154. }
  155.  
  156. SplayNode<T>* split(int from, int end){
  157. if(from > end) return NULL;
  158. if(from == && end == root->s){
  159. findKth(, NULL);
  160. return this->root;
  161. }
  162. if(from == ){
  163. findKth(end + , NULL);
  164. findKth(from, root);
  165. return root->next[];
  166. }
  167. if(end == root->s){
  168. findKth(from - , NULL);
  169. findKth(end, root);
  170. return root->next[];
  171. }
  172. findKth(end + , NULL);
  173. findKth(from - , root);
  174. return root->next[]->next[];
  175. }
  176.  
  177. void out(SplayNode<T>* node){
  178. if(node == NULL) return;
  179. if(node->lazy) node->pushDown();
  180. out(node->next[]);
  181. printf("%d ", node->data);
  182. out(node->next[]);
  183. }
  184.  
  185. void debugOut(SplayNode<T>* node){ //调试使用函数,打印Splay
  186. if(node == NULL) return;
  187. cout << node->data << "(" << node->s << "," << ((node->father == NULL) ? (-) : (node->father->data)) << "," << node->lazy << "){";
  188. debugOut(node->next[]);
  189. cout << ",";
  190. debugOut(node->next[]);
  191. cout << "}";
  192. }
  193. };
  194.  
  195. int n, m;
  196. Splay<int> s;
  197.  
  198. int main(){
  199. readInteger(n);
  200. readInteger(m);
  201. for(int i = ; i <= n; i++){
  202. s.insert(i);
  203. }
  204. for(int i = , a, b; i<= m; i++){
  205. readInteger(a);
  206. readInteger(b);
  207. if(a == b) continue;
  208. SplayNode<int>* p = s.split(a, b);
  209. p->lazy ^= ;
  210. }
  211. s.out(s.root);
  212. return ;
  213. }

[题解]bzoj 3223 文艺平衡树的更多相关文章

  1. bzoj 3223 文艺平衡树 - Splay

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

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

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

  3. BZOJ 3223 文艺平衡树 [codevs3303翻转区间]

    AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=3223 通道2:http://codevs.cn/problem/3303/ 题目分析: 我 ...

  4. BZOJ 3223 文艺平衡树

    Description   您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2  ...

  5. bzoj 3223 文艺平衡树 Splay 打标志

    是NOI2003Editor的一个子任务 #include <cstdio> #include <vector> #define maxn 100010 using names ...

  6. [bzoj3224]普通平衡树/3223文艺平衡树

    这是一道很普通的题.. 最近花了很多时间来想要去干什么,感觉自己还是太拿衣服 做这道题是因为偶尔看到了lavender的blog和她的bzoj早期AC记录,就被题目深深地吸引到了,原因有二: 自己sp ...

  7. 3223. 文艺平衡树【平衡树-splay】

    Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3  ...

  8. 【题解】P3391 文艺平衡树

    用pb_ds库中的rope水过去的,忽然发现这玩意能水好多模拟题. 详见这个博客:背景的小姐姐真的好看 声明 #include <ext/rope> using namespace __g ...

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

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

随机推荐

  1. Hibernate锁机制

    业务逻辑的实现过程中,往往需要保证数据访问的排他性.因此,我们就需要通过一些机制来保证这些数据在某个操作过程中不会被外界修改,这样的机制,在这里,也就是所谓的“锁”,即给我们选定的目标数据上锁,使其无 ...

  2. PAT1006

    At the beginning of every day, the first person who signs in the computer room will unlock the door, ...

  3. MFC乱七八糟笔记

    1.CBitmap------------------------------------------------------------------------------------- 1.类层次 ...

  4. Bootstrap 模态对话框只加载一次 remote 数据的解决办法 转载

    http://my.oschina.net/qczhang/blog/190215 摘要 前端框架 Bootstrap 的模态对话框,可以使用 remote 选项指定一个 URL,这样对话框在第一次弹 ...

  5. 【转载】区间DP

    http://www.cnblogs.com/zsboy/archive/2013/03/08/2950261.html 博客园 首页 新随笔 联系 订阅 管理 定义区间DP   区间动态规划问题一般 ...

  6. FusionCharts生成报表应用

    1.需要组装要展示的数据,至于如何怎样去设计数据模型,看你要展示的图形和需要的数据就行了.来个简单的. 实体类,只有两个属性,也可以使用Bean里面的实体类,无所谓了. package com.gol ...

  7. 2016大连网络赛 Football Games

    Football Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) P ...

  8. 深入理解最强桌面地图控件GMAP.NET ---离线地图

    enjoyeclipse 深入理解最强桌面地图控件GMAP.NET ---离线地图 这章会介绍GMAP.NET的核心功能之一:离线地图.这个功能可以满足很多政府项目.保密项目.或者由于种种原因不能上网 ...

  9. hrbustoj 2130 一笔画(状态压缩)

    基础状态压缩 转移方程为 dp[j][i] = min(dp[j][i],dp[k][i^(1<<j)] + dis[k][j]); #include<iostream> #i ...

  10. STM32开发指南-按键输入实验

    I/O口做为输入的例子.通过配置寄存器设置为输入口,检测对应寄存器的值,判读输入状态,按键是否被按下.