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

思路

一看就知道是Splay。。。。如此经典。也是我写过的第二道splay。第一道是文本编辑器,但是本地测AC,提交的话就WA到死。。

只有区间翻转操作,如果我们要翻转[L,R]的话,就将L-1转到根,再将第R+1大的转到根的右子树。把根的右子树的左子树打上翻转标记就行了。

[1,L-1]和[R+1,n]都被转到别的地方去了,所以不会被旋转。

然后别的都是splay的基本操作啦~只要每次记得push_down就行啦~

  1. #include <iostream>
  2. #include <cstring>
  3. #include <string>
  4. #include <cstdio>
  5. #include <cstdlib>
  6. #include <cmath>
  7. #include <algorithm>
  8. #include <queue>
  9. #include <stack>
  10. #include <map>
  11. #include <set>
  12. #include <list>
  13. #include <vector>
  14. #include <ctime>
  15. #include <functional>
  16. #define pritnf printf
  17. #define scafn scanf
  18. #define sacnf scanf
  19. #define For(i,j,k) for(int i=(j);i<=(k);(i)++)
  20. #define Clear(a) memset(a,0,sizeof(a))
  21. using namespace std;
  22. typedef unsigned int Uint;
  23. const int INF=0x3fffffff;
  24. ///==============struct declaration==============
  25. struct Node{
  26. int Val;bool Rev;
  27. int siz;
  28. Node *lc,*rc;
  29. Node(){lc=rc=NULL;Rev=false;siz=;}
  30. };
  31. ///==============var declaration=================
  32. const int MAXN=;
  33. int n,q;
  34. int num[MAXN];
  35. Node *root=NULL;
  36. ///==============function declaration============
  37. void BuildTree(int l,int r,Node *&o);
  38. int find_kth(int rank,Node *&o);
  39. void Splay(int Rank,Node *&o);
  40. void Rrotate(Node *&x);void Lrotate(Node *&x);
  41. void push_down(Node *&x);void Output(Node *&x);
  42. void update(Node *&x);void TestOutput(Node *&x);
  43. ///==============main code=======================
  44. int main()
  45. {
  46. #define FILE__
  47. #ifdef FILE__
  48. freopen("input","r",stdin);
  49. freopen("output","w",stdout);
  50. #endif
  51. scanf("%d%d",&n,&q);
  52. BuildTree(,n+,root);//TestOutput(root);printf("\n");
  53. while (q--){
  54. int L,R;scanf("%d%d",&L,&R);
  55. if (L==R) continue;
  56. Splay(L,root);
  57. if (root->lc==NULL)
  58. Splay(R+,root->rc);
  59. else
  60. Splay(R+-root->lc->siz,root->rc);
  61. if (root->rc!=NULL&&root->rc->lc!=NULL)
  62. root->rc->lc->Rev^=;
  63. //TestOutput(root);printf("\n");
  64. }
  65. Output(root);
  66. return ;
  67. }
  68. ///================fuction code====================
  69. void BuildTree(int l,int r,Node *&o){
  70. int m=(l+r)>>;
  71. o=new(Node);o->Val=m;
  72. if (l==r) return;
  73. if (m>l) BuildTree(l,m-,o->lc);
  74. if (m<r) BuildTree(m+,r,o->rc);
  75. if (o->lc!=NULL) o->siz+=o->lc->siz;
  76. if (o->rc!=NULL) o->siz+=o->rc->siz;
  77. }
  78. void Lrotate(Node *&x){
  79. push_down(x);push_down(x->lc);
  80. Node *y=x->lc;
  81. x->lc=y->rc;
  82. y->rc=x;x=y;
  83. update(x->rc);update(x);
  84. }
  85. void Rrotate(Node *&x){
  86. push_down(x);push_down(x->rc);
  87. Node *y=x->rc;
  88. x->rc=y->lc;
  89. y->lc=x;x=y;
  90. update(x->lc);update(x);
  91. }
  92. void push_down(Node *&x){
  93. if (x->Rev){
  94. swap(x->lc,x->rc);
  95. x->Rev=false;
  96. if (x->lc!=NULL) x->lc->Rev^=;
  97. if (x->rc!=NULL) x->rc->Rev^=;
  98. }
  99. }
  100. void update(Node *&x){
  101. x->siz=;
  102. if (x->lc!=NULL) x->siz+=x->lc->siz;
  103. if (x->rc!=NULL) x->siz+=x->rc->siz;
  104. }
  105. void Splay(int Rank,Node *&o){
  106. int ls=;push_down(o);
  107. if (o->lc!=NULL) ls=o->lc->siz;
  108. if (Rank==ls+) return;
  109. if (Rank>ls+){
  110. Splay(Rank-ls-,o->rc);
  111. Rrotate(o);
  112. }
  113. else{
  114. Splay(Rank,o->lc);
  115. Lrotate(o);
  116. }
  117. }
  118. void Output(Node *&x){
  119. if (x==NULL) return;
  120. push_down(x);
  121. Output(x->lc);
  122. if (x->Val!=&&x->Val!=n+)
  123. printf("%d ",x->Val);
  124. Output(x->rc);
  125. }
  126. void TestOutput(Node *&x){
  127. if (x==NULL) return;
  128. if (!x->Rev){
  129. printf("%d(",x->Val);
  130. TestOutput(x->lc);
  131. printf(",");
  132. TestOutput(x->rc);
  133. printf(")");
  134. }
  135. else{
  136. printf("%d(",x->Val);
  137. TestOutput(x->rc);
  138. printf(",");
  139. TestOutput(x->lc);
  140. printf(")");
  141. }
  142. }

BZOJ 3223

那个TestOutput是我用来在不改变标记的情况下看数的结构的。

【splay】文艺平衡树 BZOJ 3223的更多相关文章

  1. 3223: Tyvj 1729 文艺平衡树 - BZOJ

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

  2. splay 文艺平衡树 (数据结构)

    题目大意:略 splay维护区间翻转裸题,为了减少不必要的麻烦,多插入两个点,分别是0和n+1 每次找区间的第K个值,就在splay上二分即可 顺便学了一下splay的完美建树,而且splay有一些小 ...

  3. bzoj 3223 文艺平衡树 - Splay

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

  4. BZOJ 3223: Tyvj 1729 文艺平衡树(splay)

    速度居然进前十了...第八... splay, 区间翻转,用一个类似线段树的lazy标记表示是否翻转 ------------------------------------------------- ...

  5. bzoj 3223: Tyvj 1729 文艺平衡树 (splay)

    链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3223 题面: 3223: Tyvj 1729 文艺平衡树 Time Limit: 10 S ...

  6. BZOJ 3223: Tyvj 1729 文艺平衡树-Splay树(区间翻转)模板题

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

  7. fhq_treap || BZOJ 3223: Tyvj 1729 文艺平衡树 || Luogu P3391 【模板】文艺平衡树(Splay)

    题面: [模板]文艺平衡树(Splay) 题解:无 代码: #include<cstdio> #include<cstring> #include<iostream> ...

  8. [题解]bzoj 3223 文艺平衡树

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

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

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

随机推荐

  1. [LeetCode] Add Binary 二进制数相加

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

  2. [LeetCode] First Missing Positive 首个缺失的正数

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  3. FineUI(专业版)公测版发布(这速度,真TM快!)

    经过近一年的筹备.编码和测试,FineUI(专业版)公测版终于和大家见面了!现在就来体验一下专业版飞一般的速度吧:http://fineui.com/demo_pro/FineUI(专业版)首页:ht ...

  4. 将HTML5封装成android应用APK文件的几种方法

    越来越多的开发者热衷于使用html5+JavaScript开发移动Web App.不过,HTML5 Web APP的出现能否在未来取代移动应用,就目前来说,还是个未知数.一方面,用户在使用习惯上,不喜 ...

  5. jQuery 邮箱下拉列表自动补全

    综述 我想大家一定见到过,在某个网站填写邮箱的时候,还没有填写完,就会出现一系列下拉列表,帮你自动补全邮箱的功能.现在我们就用jQuery来实现一下. 博主原创代码,如有代码写的不完善的地方还望大家多 ...

  6. maven

    maven常见问题问答 1.前言 Maven,发音是[`meivin],"专家"的意思.它是一个很好的项目管理工具,很早就进入了我的必备工具行列,但是这次为了把project1项目 ...

  7. asp.net mvc 权限过滤和单点登录(禁止重复登录)

    1.权限控制使用controller和 action来实现,权限方式有很多种,最近开发项目使用控制控制器方式实现代码如下 /// <summary> /// 用户权限控制 /// < ...

  8. centos tar压缩与解压缩命令大全

    tar命令详解 -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用 ...

  9. ThinkPhp 3.2 自动验证

    自动验证是ThinkPHP模型层提供的一种数据验证方法,可以在使用create创建数据对象的时候自动进行数据验证. 数据验证有两种方式: 静态方式:在模型类里面通过$_validate属性定义验证规则 ...

  10. BOM操作

    BOM操作 //浏览器对象模型 opener=window.open(页面的url,打开方式) opener.document.body.style.background="red" ...