题面大意:

使用平衡树维护一个数列,支持插入,修改,删除,翻转,求和,求最大和这 \(6\) 个操作.

题意分析:

Splay 裸题,几乎各种操作都有了,这个代码就发给大家当个模板吧.

最后求最大和的时候可以事先维护好最大和,然后输出即可。

代码:

#include <cstdio>
#include <climits>
#include <algorithm>
#include <iostream>
#define maxn 1000001
using namespace std; int stack[maxn], top, v[maxn];
struct Splay {
#define T(x) (tree[f[x]][1]==x)
#define ls(x) tree[x][0]
#define rs(x) tree[x][1]
int tree[maxn][2], f[maxn], size[maxn], val[maxn];
int sum[maxn], L[maxn], R[maxn], Max[maxn];
bool rev[maxn], mark[maxn];
int root, len; void cov_tag(int x, int v) {
if (!x) return;
sum[x] = size[x] * v;
val[x] = v;
L[x] = R[x] = Max[x] = max(v, sum[x]);
mark[x] = 1, rev[x] = 0;
} void rev_tag(int x) {
if (!x) return;
swap(L[x], R[x]);
swap(ls(x), rs(x));
rev[x] ^= 1;
} void pushdown(int x) {
if (rev[x]) {
rev_tag(ls(x));
rev_tag(rs(x));
rev[x] = 0;
}
if (mark[x]) {
cov_tag(ls(x), val[x]);
cov_tag(rs(x), val[x]);
mark[x] = 0;
}
} void updata(int x) {
size[x] = size[ls(x)] + size[rs(x)] + 1;
Max[x] = max(max(Max[ls(x)], Max[rs(x)]), max(0, R[ls(x)]) + val[x] + max(0, L[rs(x)]));
L[x] = max(L[ls(x)], sum[ls(x)] + val[x] + max(0, L[rs(x)]));
R[x] = max(R[rs(x)], sum[rs(x)] + val[x] + max(0, R[ls(x)]));
sum[x] = sum[ls(x)] + sum[rs(x)] + val[x];
} int get() {
int x;
x = top ? stack[top--] : ++len;
ls(x) = rs(x) = f[x] = 0;
rev[x] = mark[x] = 0;
size[x] = 1;
sum[x] = L[x] = R[x] = val[x] = -1e9;
return x;
} void build(int fa, int l, int r, int& x) {
if (l > r) return;
int mid = (l + r) >> 1;
x = get(), f[x] = fa, val[x] = v[mid];
if (l == r) {
size[x] = 1;
Max[x] = L[x] = R[x] = sum[x] = val[x];
return;
}
build(x, l, mid - 1, ls(x));
build(x, mid + 1, r, rs(x));
updata(x);
} void init(int n) {
L[0] = R[0] = Max[0] = -1e9;
len = 2, root = 1;
rs(1) = size[1] = 2, L[1] = R[1] = val[1] = sum[1] = -1e9;
f[2] = size[2] = 1, L[2] = R[2] = val[2] = sum[2] = -1e9;
for (int i = 1; i <= n; i++) {
cin >> v[i];
}
build(2, 1, n, ls(2));
updata(2), updata(1);
} void move(int x) {
int fa = f[x], son = tree[x][T(x) ^ 1];
tree[x][T(x) ^ 1] = fa;
tree[fa][T(x)] = son;
if (son) f[son] = fa;
f[x] = f[fa];
if (f[x]) tree[f[x]][T(fa)] = x;
f[fa] = x;
updata(fa), updata(x);
} void splay(int x) {
while (f[x]) {
if (f[f[x]]) T(x) == T(f[x]) ? move(f[x]) : move(x);
move(x);
}
root = x;
} int find(int x, int i) {
pushdown(i);
if (size[ls(i)] + 1 == x) return i;
if (x <= size[ls(i)]) return find(x, ls(i));
return find(x - size[ls(i)] - 1, rs(i));
} void insert() {
int x, y, tot;
cin >> x >> tot;
x++;
y = x + 1;
x = find(x, root), splay(x);
y = find(y, root), splay(y);
if (f[x] != root) move(x);
for (int i = 1; i <= tot; i++) {
cin >> v[i];
}
build(x, 1, tot, rs(x));
updata(x), updata(y);
} void reuse(int x) {
if (!x) return;
stack[++top] = x;
reuse(ls(x)), reuse(rs(x));
} void Del() {
int x, y;
cin >> x >> y;
y = y + x - 1;
x = find(x, root), splay(x);
y = find(y + 2, root), splay(y);
if (f[x] != root) move(x);
reuse(rs(x));
f[rs(x)] = 0, rs(x) = 0;
updata(x), updata(y);
} void cover() {
int x, y, v;
cin >> x >> y >> v;
x = find(x, root), splay(x);
y = find(y + 2, root), splay(y);
if (f[x] != root) move(x);
cov_tag(rs(x), v);
updata(x), updata(y);
} void reverse() {
int x, y;
cin >> x >> y;
y = y + x - 1;
x = find(x, root), splay(x);
y = find(y + 2, root), splay(y);
if (f[x] != root) move(x);
rev_tag(rs(x));
updata(x), updata(y);
} void query_sum() {
int x, y;
cin >> x >> y;
y = y + x - 1;
x = find(x, root), splay(x);
y = find(y + 2, root), splay(y);
if (f[x] != root) move(x);
printf("%d\n", sum[rs(x)]);
} void query_max() {
printf("%d\n", Max[root]);
}
} splay; int main()
{
int n, m;
scanf("%d %d", &n, &m);
splay.init(n); // 先插入 n 个数并建立平衡树
while (n--)
{
string ss;
cin >> ss; if (ss[0] == 'I') { // 插入操作
splay.insert();
}
if (ss[0] == 'D') { // 删除操作
splay.Del();
}
if (ss[0] == 'M' && ss[2] == 'K') { // 修改操作
splay.cover();
}
if (ss[0] == 'R') { // 旋转操作
splay.reverse();
}
if (ss[0] == 'G') { // 求和操作
splay.query_sum();
}
if (ss[0] == 'M' && ss[2] == 'X') { // 修改操作
splay.query_max();
}
}
return 0;
}

题解 - 【NOI2015】维修数列的更多相关文章

  1. 【BZOJ1500】【NOI2005】维修数列(Splay)

    [BZOJ1500][NOI2005]维修数列(Splay) 题面 不想再看见这种毒瘤题,自己去BZOJ看 题解 Splay良心模板题 真的很简单 我一言不发 #include<iostream ...

  2. 【BZOJ1500】[NOI2005]维修数列 Splay

    [BZOJ1500][NOI2005]维修数列 Description Input 输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初始时数列中数的个数,M表示要进行的操作数目.第2行 ...

  3. 【BZOJ】1500: [NOI2005]维修数列

    [算法]splay [题解]数据结构 感谢Occult的模板>_<:HYSBZ 1500 维修数列 #include<cstdio> #include<cctype> ...

  4. splay模板三合一 luogu2042 [NOI2005]维护数列/bzoj1500 [NOI2005]维修数列 | poj3580 SuperMemo | luogu3391 【模板】文艺平衡树(Splay)

    先是维修数列 题解看这里,但是我写的跑得很慢 #include <iostream> #include <cstdio> using namespace std; int n, ...

  5. 【BZOJ-1500】维修数列 Splay

    1500: [NOI2005]维修数列 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 11047  Solved: 3460[Submit][Statu ...

  6. [NOI2005] 维修数列

    1500: [NOI2005]维修数列 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 8397  Solved: 2530 Description In ...

  7. bzoj 1500: [NOI2005]维修数列 splay

    1500: [NOI2005]维修数列 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 6556  Solved: 1963[Submit][Status ...

  8. [BZOJ1500][NOI2005]维修数列---解题报告

    Portal Gun:[BZOJ1500][NOI2005]维修数列 有一段时间没写博客了,最近在刚数据结构......各种板子背得简直要起飞,题目也是一大堆做不完,这里就挑一道平衡树的题来写写好了 ...

  9. [BZOJ 1500]维修数列 [Splay Tree从进阶到住院]

    历尽艰辛终于A掉了这题QwQ 贴COGS评论区几句话=.= 策爷:"splay/块状链表的自虐题.".深刻理解到如果没有M倾向就不要去写这题了.. -Chenyao2333 记得b ...

  10. BZOJ_1500_[NOI2005]维修数列_splay

    BZOJ_1500_[NOI2005]维修数列_splay 题意: 分析: 节点维护从左开始的最大连续子段和,从右开始的最大连续子段和,区间的最大连续子段和 插入:重新建一棵树,把pos旋到根,把po ...

随机推荐

  1. Spring Cloud 学习笔记一

    一.spring cloud 搭建注册中心(Eureka server) 1.spring cloud中提供了多种分步式服务组件,其都依赖于注册中心(eureka),注册中心的服务者与发现者都通过Eu ...

  2. CentOS8的网络管理变化

    资料来源: https://www.cnblogs.com/linuxandy/p/10839856.html 1.CentOS8使用NetworkManager.service(简称NM)来管理网络 ...

  3. Redis 入门到分布式 (六)常见的持久化开发运维问题

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一.常见问题目录 fork操作 进程外开销 AOF追加阻塞 单机多实例部署 二. fork 1.Fork ...

  4. elasticsearch中保存时间格式

    利用logstash从文档中导入数据到es中,若未事先设定数据格式,有可能存储时间并未保存为date格式而是text格式. 时间若保存为text,则在会以字符串数组格式存储在es中,是乱序,不好查询. ...

  5. Java实现 LeetCode 605 种花问题(边界问题)

    605. 种花问题 假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有.可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去. 给定一个花坛(表示为一个数组包含0和1,其中0表示没种 ...

  6. Java实现 蓝桥杯 算法训练 出现次数最多的整数

    算法训练 出现次数最多的整数 时间限制:1.0s 内存限制:512.0MB 提交此题 问题描述 编写一个程序,读入一组整数,这组整数是按照从小到大的顺序排列的,它们的个数N也是由用户输入的,最多不会 ...

  7. Java实现 LeetCode 165 比较版本号

    165. 比较版本号 比较两个版本号 version1 和 version2. 如果 version1 > version2 返回 1,如果 version1 < version2 返回 ...

  8. 第四届蓝桥杯JavaC组国(决)赛真题

    解题代码部分来自网友,如果有不对的地方,欢迎各位大佬评论 题目1.好好学习 汤姆跟爷爷来中国旅游.一天,他帮助中国的小朋友贴标语.他负责贴的标语是分别写在四块红纸上的四个大字:"好.好.学. ...

  9. 彻底解决go get golang.org/x等包失败与VSCode golang插件安装失败问题

    由于某种众所周知的一些原因,https://golang.org/ golang 的官方域名是被墙了的,这也就导致了, 在广大 go 开发者使用 golang 的时候,总会出现 go get 失败的问 ...

  10. ProxySQL简介原理及读写分离应用

    MySQL-ProxySQL中间件简介 同类型产品 MySQL Route:是现在MySQL官方Oracle公司发布出来的一个中间件. Atlas:是由奇虎360公发的基于MySQL协议的数据库中间件 ...