题目背景

网上有许多题,就是给定一个序列,要你支持几种操作:A、B、C、D。一看另一道题,又是一个序列要支持几种操作:D、C、B、A。尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技术含量……

这样我也出一道题,我出这一道的目的是为了让大家以后做这种题目有一个“库”可以依靠,没有什么其他的意思。

这道题目就叫序列终结者吧。

题目描述

给定一个长度为N的序列,每个序列的元素是一个整数(废话)。要支持以下三种操作:

  1. 将[L,R][L,R][L,R]这个区间内的所有数加上VVV。
  2. 将[L,R][L,R][L,R]这个区间翻转,比如1 2 3 4变成4 3 2 1
  3. 求[L,R][L,R][L,R]这个区间中的最大值。

最开始所有元素都是000。

输入输出格式

输入格式:

第一行两个整数N,MM为操作个数。

以下MMM行,每行最多四个整数,依次为K,L,R,VK表示是第几种操作,如果不是第1种操作则K后面只有两个数。

输出格式:

对于每个第3种操作,给出正确的回答。

输入输出样例

输入样例#1: 复制

  1. 4 4
  2. 1 1 3 2
  3. 1 2 4 -1
  4. 2 1 3
  5. 3 2 4
输出样例#1: 复制

  1. 2
  2.  
  3. splay Tree
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. #include<cstdlib>
  5. #include<cstring>
  6. #include<string>
  7. #include<cmath>
  8. #include<map>
  9. #include<set>
  10. #include<vector>
  11. #include<queue>
  12. #include<bitset>
  13. #include<ctime>
  14. #include<deque>
  15. #include<stack>
  16. #include<functional>
  17. #include<sstream>
  18. //#pragma GCC optimize(2)
  19. //#include<cctype>
  20. //#pragma GCC optimize("O3")
  21. using namespace std;
  22. #define maxn 100005
  23. #define inf 0x3f3f3f3f
  24. #define INF 9999999999
  25. #define rdint(x) scanf("%d",&x)
  26. #define rdllt(x) scanf("%lld",&x)
  27. #define rdult(x) scanf("%lu",&x)
  28. #define rdlf(x) scanf("%lf",&x)
  29. #define rdstr(x) scanf("%s",x)
  30. typedef long long ll;
  31. typedef unsigned long long ull;
  32. typedef unsigned int U;
  33. #define ms(x) memset((x),0,sizeof(x))
  34. const long long int mod = 1e9 + 7;
  35. #define Mod 1000000000
  36. #define sq(x) (x)*(x)
  37. #define eps 1e-3
  38. typedef pair<int, int> pii;
  39. #define pi acos(-1.0)
  40. //const int N = 1005;
  41. #define REP(i,n) for(int i=0;i<(n);i++)
  42.  
  43. inline ll rd() {
  44. ll x = 0;
  45. char c = getchar();
  46. bool f = false;
  47. while (!isdigit(c)) {
  48. if (c == '-') f = true;
  49. c = getchar();
  50. }
  51. while (isdigit(c)) {
  52. x = (x << 1) + (x << 3) + (c ^ 48);
  53. c = getchar();
  54. }
  55. return f ? -x : x;
  56. }
  57.  
  58. ll gcd(ll a, ll b) {
  59. return b == 0 ? a : gcd(b, a%b);
  60. }
  61. ll sqr(ll x) { return x * x; }
  62.  
  63. /*ll ans;
  64. ll exgcd(ll a, ll b, ll &x, ll &y) {
  65. if (!b) {
  66. x = 1; y = 0; return a;
  67. }
  68. ans = exgcd(b, a%b, x, y);
  69. ll t = x; x = y; y = t - a / b * y;
  70. return ans;
  71. }
  72. */
  73.  
  74. ll qpow(ll a, ll b, ll c) {
  75. ll ans = 1;
  76. a = a % c;
  77. while (b) {
  78. if (b % 2)ans = ans * a%c;
  79. b /= 2; a = a * a%c;
  80. }
  81. return ans;
  82. }
  83.  
  84. struct splay {
  85. int fa, ch[2], size;
  86. int lazy, rev, maxx, value;
  87. }Sp[maxn];
  88.  
  89. int n, m, root, a[maxn];
  90. void pushup(int rt) {
  91. Sp[rt].size = Sp[Sp[rt].ch[0]].size + Sp[Sp[rt].ch[1]].size + 1;
  92. Sp[rt].maxx = max(Sp[rt].value, max(Sp[Sp[rt].ch[0]].maxx, Sp[Sp[rt].ch[1]].maxx));
  93. }
  94.  
  95. void pushdown(int rt) {
  96. if (Sp[rt].lazy) {
  97. if (Sp[rt].ch[0]) {
  98. Sp[Sp[rt].ch[0]].lazy += Sp[rt].lazy;
  99. Sp[Sp[rt].ch[0]].maxx += Sp[rt].lazy;
  100. Sp[Sp[rt].ch[0]].value += Sp[rt].lazy;
  101. }
  102. if (Sp[rt].ch[1]) {
  103. Sp[Sp[rt].ch[1]].lazy += Sp[rt].lazy;
  104. Sp[Sp[rt].ch[1]].maxx += Sp[rt].lazy;
  105. Sp[Sp[rt].ch[1]].value += Sp[rt].lazy;
  106. }
  107. Sp[rt].lazy = 0;
  108. }
  109. if (Sp[rt].rev) {
  110. if (Sp[rt].ch[0]) {
  111. Sp[Sp[rt].ch[0]].rev ^= 1;
  112. swap(Sp[Sp[rt].ch[0]].ch[0], Sp[Sp[rt].ch[0]].ch[1]);
  113. }
  114. if (Sp[rt].ch[1]) {
  115. Sp[Sp[rt].ch[1]].rev ^= 1;
  116. swap(Sp[Sp[rt].ch[1]].ch[0], Sp[Sp[rt].ch[1]].ch[1]);
  117. }
  118. Sp[rt].rev = 0;
  119. }
  120. }
  121.  
  122. int id(int x) {
  123. return Sp[Sp[x].fa].ch[1] == x;
  124. }
  125. void link(int son, int fa, int k) {
  126. Sp[son].fa = fa; Sp[fa].ch[k] = son;
  127. }
  128.  
  129. void rotate(int x) {
  130. int y = Sp[x].fa;
  131. int z = Sp[y].fa;
  132. int yk = id(x);
  133. int zk = id(y);
  134. int b = Sp[x].ch[yk ^ 1];
  135. link(b, y, yk); link(y, x, yk ^ 1);
  136. link(x, z, zk);
  137. pushup(y); pushup(x);
  138. }
  139.  
  140. void SPLAY(int x, int aim) {
  141. while (Sp[x].fa != aim) {
  142. int y = Sp[x].fa;
  143. int z = Sp[y].fa;
  144. if (z != aim)id(x) == id(y) ? rotate(y) : rotate(x);
  145. rotate(x);
  146. }
  147. if (aim == 0)root = x;
  148. }
  149.  
  150. int kth(int k) {
  151. int now = root;
  152. while (1) {
  153. pushdown(now);
  154. int left = Sp[now].ch[0];
  155. if (Sp[left].size + 1 < k) {
  156. k -= Sp[left].size + 1; now = Sp[now].ch[1];
  157. }
  158. else if (Sp[left].size >= k)now = left;
  159. else return now;
  160. }
  161. }
  162.  
  163. int build(int l, int r, int fa) {
  164. if (l > r)return 0;
  165. if (l == r) {
  166. Sp[l].fa = fa; Sp[l].maxx = Sp[l].value = a[l];
  167. Sp[l].size = 1; return l;
  168. }
  169. int mid = (l + r) >> 1;
  170. Sp[mid].ch[0] = build(l, mid - 1, mid);
  171. Sp[mid].ch[1] = build(mid + 1, r, mid);
  172. Sp[mid].value = a[mid];
  173. Sp[mid].fa = fa;
  174. pushup(mid);
  175. return mid;
  176. }
  177.  
  178. int split(int l, int r) {
  179. l = kth(l); r = kth(r + 2);
  180. SPLAY(l, 0); SPLAY(r, l);
  181. return Sp[Sp[root].ch[1]].ch[0];
  182. }
  183.  
  184. void upd(int l, int r, int v) {
  185. int now = split(l, r);
  186. Sp[now].lazy += v; Sp[now].maxx += v; Sp[now].value += v;
  187. pushup(Sp[root].ch[1]); pushup(root);
  188. }
  189.  
  190. void Reverse(int l, int r) {
  191. int now = split(l, r);
  192. Sp[now].rev ^= 1;
  193. swap(Sp[now].ch[0], Sp[now].ch[1]);
  194. pushup(Sp[root].ch[1]); pushup(root);
  195. }
  196. int query(int l, int r) {
  197. return Sp[split(l, r)].maxx;
  198. }
  199.  
  200. int main()
  201. {
  202. //ios::sync_with_stdio(0);
  203. rdint(n); rdint(m);
  204. a[1] = a[n + 2] = Sp[0].maxx = -inf;
  205. root = build(1, n + 2, 0);
  206. int k, l, r, v;
  207. while (m--) {
  208. rdint(k); rdint(l); rdint(r);
  209. if (k == 1) {
  210. rdint(v); upd(l, r, v);
  211. }
  212. else if (k == 2)Reverse(l, r);
  213. else if (k == 3)cout << query(l, r) << endl;
  214. }
  215. return 0;
  216. }

序列终结者 BZOJ 1251 Splay的更多相关文章

  1. 1251. 序列终结者【平衡树-splay】

    Description 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列 要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这 ...

  2. BZOJ 1251 Splay维护序列

    思路: splay维护序列的裸题 啊woc调了一天 感谢yzy大佬的模板-- //By SiriusRen #include <cstdio> #include <cstring&g ...

  3. BZOJ 1251 序列终结者(Splay)

    题目大意 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技术 ...

  4. BZOJ 1251: 序列终结者 [splay]

    1251: 序列终结者 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 3778  Solved: 1583[Submit][Status][Discu ...

  5. 【BZOJ】1251: 序列终结者(splay)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1251 不行..为什么写个splay老是犯逗,这次又是null的mx没有赋值-maxlongint.. ...

  6. bzoj 1251序列终结者 splay 区间翻转,最值,区间更新

    序列终结者 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 4594  Solved: 1939[Submit][Status][Discuss] De ...

  7. 【BZOJ】【1251】序列终结者

    Splay 还是splay序列维护,这题我WA了的原因是:在Push_up的时候,当前子树的max我是直接取的L.R和v[x]的最大值,但是如果没有左/右儿子,默认是会访问0号结点的mx值,而这个值没 ...

  8. BZOJ 1251: 序列终结者

    1251: 序列终结者 Time Limit: 20 Sec  Memory Limit: 162 MB Submit: 3773  Solved: 1579 [Submit][Status][Dis ...

  9. 「BZOJ1251」序列终结者 (splay 区间操作)

    题面: 1251: 序列终结者 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 5367  Solved: 2323[Submit][Status][D ...

随机推荐

  1. node install webpack -cli webpack4.xxxx

    webpack 4.xx 版本 分离了 webpack-cli ; 安装webpack4.xx 需要再安装webpack-cli;

  2. hadoop block大小为128的原因

    1.减轻了namenode的压力 原因是hadoop集群在启动的时候,datanode会上报自己的block的信息给namenode.namenode把这些信息放到内存中.那么如果块变大了,那么nam ...

  3. LAMP 2.5 Apache禁止解析php

    某个目录下禁止解析 php,这个很有用,我们做网站安全的时候,这个用的很多,比如某些目录可以上传文件, 为了避免上传的文件有木马, 所以我们禁止这个目录下面的访问解析 php. 安装目录下有个data ...

  4. springmvc 路径问题

    web项目中的相对路径可以分为二类: 1.以斜杠开头:以斜杠开头的又分为二类(分类依据是斜杠出现的位置):如果出现在java代码或者配置文件(xml,properties等),这个路径叫做后台路径. ...

  5. Go语言-变量和常量

    我们在这里需要优先说明的是用于声明变量的关键字var,以及用于声明常量的关键字const.要知道,绝大多数的数据类型的值都可以被赋给一个变量,包括函数.而常量则不同,它只能被赋予基本数据类型的值本身. ...

  6. Navicat MySQL安装

    1 下载安装包 点击下载安装包 2 解压安装包(解压后有三个文件) 第一个 Crack 是注册文件 第二个 chinese.rar 是汉化包 第三个 navicat_trial.exe 是安装程序 3 ...

  7. MinGW lapack 在windows 上安装

    MinGW基本的配置环境 编译安装 方案一:MinGW Makefiles 我的配置好之后mingw文件夹下没有mingw32-make.exe,使用 mingw-get install mingw3 ...

  8. 长城防火墙(GFW)

    一.简介 中国防火长城,官方名为金盾工程,是由政府运作的一个互联网审查监控项目.在其管辖互联网内部建立的多套网络审查系统的总称,包括相关行政审查系统.其英文名称Great Firewall of Ch ...

  9. C语言中的static的作用?

    在C语言中,static的字面意思很容易把我们导入歧途,其实它的作用有三条. (1)第一个作用:隐藏. 当我们同时编译多个文件时,所有未加static前缀的全局变量和函数都具有全局可见性.为理解这句话 ...

  10. 一个小错误,在for循环中,每次repaint()的时候,记得先把frame涂成白色的。等于擦掉原来的痕迹·。

    import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Animate { int x=1; in ...