题目大意

网上有许多题,就是给定一个序列,要你支持几种操作:A、B、C、D。一看另一道题,又是一个序列 要支持几种操作:D、C、B、A。尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技术含量……这样 我也出一道题,我出这一道的目的是为了让大家以后做这种题目有一个“库”可以依靠,没有什么其他的意思。这道题目 就叫序列终结者吧。 【问题描述】 给定一个长度为N的序列,每个序列的元素是一个整数(废话)。要支持以下三种操作: 1. 将[L,R]这个区间内的所有数加上V。 2. 将[L,R]这个区间翻转,比如1 2 3 4变成4 3 2 1。 3. 求[L,R]这个区间中的最大值。 最开始所有元素都是0。

Input

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

Output

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

题解

首先,终于做完splay的5道模板题了。

说一下这个题怎么做。

我们沿用线段树中的lazy标记,给每个节点打两个标记,一旦在find中访问到这个节点,立即pushdown,同时及时update。

然后就是splay的经典操作了。

这个题调了很长时间,所以,以后一定要先静态调试,避免SB错误和理清逻辑后,再用gdb动态调试。

代码

#include <algorithm>
#include <cstdio>
#include <cstring>
#ifdef D
const int maxn = 100;
#else
const int maxn = 100000;
#endif
int data[maxn], fa[maxn], ch[maxn][2], rev[maxn], mx[maxn], pls[maxn],
size[maxn];
int n, m, sz = 0, rt = 0;
void update(int x) {
mx[x] = data[x];
if (ch[x][0] != -1)
mx[x] = std::max(mx[x], mx[ch[x][0]]);
if (ch[x][1] != -1)
mx[x] = std::max(mx[x], mx[ch[x][1]]);
size[x] = 1;
if (ch[x][0] != -1)
size[x] += size[ch[x][0]];
if (ch[x][1] != -1)
size[x] += size[ch[x][1]];
}
void pushdown(int k) {
int &l = ch[k][0], &r = ch[k][1], t = pls[k];
if (t) {
pls[k] = 0;
if (l != -1) {
pls[l] += t;
mx[l] += t;
data[l] += t;
}
if (r != -1) {
pls[r] += t;
mx[r] += t;
data[r] += t;
}
}
if (rev[k]) {
rev[k] = 0;
rev[l] ^= 1;
rev[r] ^= 1;
std::swap(ch[k][0], ch[k][1]);
}
}
void zig(int x) {
int y = fa[x], z = fa[y], l = (ch[y][1] == x), r = l ^ 1;
fa[ch[y][l] = ch[x][r]] = y;
fa[ch[x][r] = y] = x;
fa[x] = z;
if (z != -1) {
ch[z][ch[z][1] == y] = x;
}
update(y);
update(x);
}
void splay(int x, int aim = -1) {
for (int y; (y = fa[x]) != aim; zig(x))
if (fa[y] != aim)
zig((ch[fa[y]][0] == y) == (ch[y][0] == x) ? y : x);
if (aim == -1)
rt = x;
}
int find(int k, int rank) {
if (pls[k] || rev[k])
pushdown(k);
int l = ch[k][0], r = ch[k][1];
if (size[l] + 1 == rank)
return k;
else if (size[l] >= rank)
return find(l, rank);
else
return find(r, rank - size[l] - 1);
}
void build(int l, int r, int last) {
if (l > r)
return;
int now = l;
if (l == r) {
size[now] = 1;
fa[now] = last;
data[now] = rev[now] = mx[now] = 0;
if (l < last)
ch[last][0] = now;
else
ch[last][1] = now;
return;
}
int mid = (l + r) >> 1;
now = mid;
build(l, mid - 1, mid);
build(mid + 1, r, mid);
fa[now] = last;
update(now);
if (mid < last) {
ch[last][0] = now;
} else
ch[last][1] = now;
return;
}
void rv(int l, int r) {
int x = find(rt, l), y = find(rt, r + 2);
splay(x);
splay(y, rt);
int z = ch[y][0];
rev[z] ^= 1;
}
void ps(int l, int r, int v) {
int x = find(rt, l), y = find(rt, r + 2);
splay(x);
splay(y, rt);
int z = ch[y][0];
pls[z] += v;
data[z] += v;
mx[z] += v;
}
int Max(int l, int r) {
int x = find(rt, l), y = find(rt, r + 2);
splay(x);
splay(y, rt);
int z = ch[y][0];
return mx[z];
}
int main() {
#ifdef D
freopen("input", "r", stdin);
#endif
scanf("%d %d", &n, &m);
memset(ch, -1, sizeof(ch));
memset(rev, 0, sizeof(rev));
memset(pls, 0, sizeof(pls));
memset(fa, 0, sizeof(fa));
build(0, n + 1, -1);
rt = (n + 1) >> 1;
for (int i = 1; i <= m; i++) {
int k;
scanf("%d", &k);
if (k == 1) {
int l, r, v;
scanf("%d %d %d", &l, &r, &v);
ps(l, r, v);
}
if (k == 2) {
int l, r;
scanf("%d %d", &l, &r);
rv(l, r);
}
if (k == 3) {
int l, r;
scanf("%d %d", &l, &r);
int ans = Max(l, r);
printf("%d\n", ans);
}
}
}

[bzoj1251]序列终结者——splay的更多相关文章

  1. bzoj1251 序列终结者(splay)

    人生第一发splay,写得巨丑,最后忘记了push_down以后要将子节点maintain 9k代码不忍直视 #define NDEBUG #include<cstdio> #includ ...

  2. [BZOJ1251]序列终结者

    [BZOJ1251]序列终结者 试题描述 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列 要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题 ...

  3. 【BZOJ1251】序列终结者 Splay

    一道模板题,一直没发现自己的快速读入读不了负数,我竟然能活到现在真是万幸. #include <iostream> #include <cstdio> #define inf ...

  4. bzoj1251 序列终结者(Splay Tree+懒惰标记)

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

  5. BZOJ1251 序列终结者(Splay平衡树)(占位)

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

  6. [bzoj1251]序列终结者_splay

    序列终结者 bzoj-1251 题目大意:给定一个长度为n的正整数序列,支持区间加,区间反转,查询区间最大值.所有元素开始都是0. 注释:$1\le n\le 5\cdot 10^4$,操作个数不多于 ...

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

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

  8. CODEVS 4655 序列终结者-splay(区间更新、区间翻转、区间最值)

    4655 序列终结者  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题解       题目描述 Description 网上有许多题,就是给定一个序列,要 ...

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

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

随机推荐

  1. Bug是一种财富-------研发同学的错题集、测试同学的遗漏用例集

    此文已由作者王晓明授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 各位看官,可能看到标题的你一定认为这是一篇涉嫌"炒作"的文章,亦或是为了吸引眼球而起的标 ...

  2. 大数据de 2文章

    点击可免费试用网易有数 文章来源:网易有数的搭积木原则阐述 ,经作者文雯授权发布 wo ceceshi 相关文章:[推荐] SpringBoot入门(五)--自定义配置

  3. 免费天气预报API接口

    一.中国气象局(http://www.weather.com.cn) 1.实时接口 http://mobile.weather.com.cn/data/sk/101010100.html http:/ ...

  4. SPRITEKIT游戏框架之关于PHYSICS物理引擎属性

    Spritekit提供了一个默认的物理模拟系统,用来模拟真实物理世界,可以使得编程者将注意力从力学碰撞和重力模拟的计算中解放出来,通过简单地代码来实现物理碰撞的模拟,而将注意力集中在更需要花费精力的地 ...

  5. 小程序js脚本模块化调用

    可以将一些公共的代码抽离成为一个单独的 js 文件,作为一个模块.模块只有通过 module.exports 或者 exports 才能对外暴露接口. 1. common.js // common.j ...

  6. 基于HTML5移动web应用

    一.基于HTML5移动web应用 1.canvas 绘图 2.多媒体 3.本地存储 4.离线应用 5.使用地理位置 6.移动web框架   二.具体说明 1.HTML5标准最大的变化就是支持Web绘图 ...

  7. LeetCode 410——分割数组的最大值

    1. 题目 2. 解答 此题目为 今日头条 2018 AI Camp 5 月 26 日在线笔试编程题第二道--最小分割分数. class Solution { public: // 若分割数组的最大值 ...

  8. python——pyinstaller生成exe基本使用和遇到的坑

    1.安装 pip install pyinstaller 2.常规操作 在cmd界面(之前安装python或者anaconda的时候正确添加环境变量的话,是可以在cmd界面直接执行pyinstalle ...

  9. 数论初步——Eratosthenes筛法

    具体内容见紫书p312-p313 一.用Eratosthenes筛法构造1~n的素数表 思想:对于不超过n的每个非负整数p,删除2p,3p,4p…,当处理完所有的数后,还没有被删除的就是素数. 代码: ...

  10. 【Python】Python内置函数dir详解

    1.命令介绍 最近学习并使用了一个python的内置函数dir,首先help一下: 复制代码代码如下: >>> help(dir)Help on built-in function ...