题目链接

首先,这题我是没A的。。太毒瘤了

题目本身不难,都是\(Splay\)的基操,但是细节真的容易挂。

调了好久自闭了,果断放弃。。

希望本节目停更。

放上最终版本

#include <cstdio>
#include <algorithm>
#define INF 2147483647
#define lc t[u].ch[0]
#define rc t[u].ch[1]
using namespace std;
inline int read(){
int s = 0, w = 1;
char ch = getchar();
while(ch < '0' || ch > '9'){if(ch == '-') w = -1;else w = 1; ch = getchar();}
while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0',ch = getchar();
return s * w;
}
const int MAXINSERTLEN = 4000010;
struct SplayTree{
int ch[2], fa, size, val, sum, lm, rm, m, lazy, lz, to;
}t[MAXINSERTLEN];
int str[MAXINSERTLEN];
int num, root, n, len, a, b, c, tot;
void pushup(int u){
t[u].size = t[lc].size + t[rc].size + 1;
t[u].sum = t[lc].sum + t[rc].sum + t[u].val;
t[u].lm = max(t[lc].lm, t[lc].sum + t[u].val + t[rc].lm);
t[u].rm = max(t[rc].rm, t[rc].sum + t[u].val + t[lc].rm);
t[u].m = max(max(t[lc].m, t[rc].m), t[lc].rm + t[u].val + t[rc].lm);
}
void pushdown(int u){
if(t[u].lz){
t[lc].lz = t[rc].lz = 1;
t[lc].to = t[rc].to = t[lc].val = t[rc].val = t[u].to;
t[u].lz = t[u].lazy = 0;
t[lc].sum = t[lc].lm = t[lc].rm = t[lc].m = t[lc].size * t[u].to;
t[rc].sum = t[rc].lm = t[rc].rm = t[rc].m = t[rc].size * t[u].to;
}
if(t[u].lazy){
t[lc].lazy ^= 1; t[rc].lazy ^= 1;
t[u].lazy = 0;
swap(lc, rc);
swap(t[u].lm, t[u].rm);
}
}
void rotate(int x){
int y = t[x].fa; int z = t[y].fa;
pushdown(y); pushdown(x); int k = t[y].ch[1] == x;
t[z].ch[t[z].ch[1] == y] = x; t[x].fa = z;
t[y].ch[k] = t[x].ch[k ^ 1]; t[t[x].ch[k ^ 1]].fa = y;
t[x].ch[k ^ 1] = y; t[y].fa = x;
pushup(y); pushup(x);
}
void Splay(int x, int goal){
while(t[x].fa != goal){
int y = t[x].fa; int z = t[y].fa;
pushdown(y); pushdown(x);
if(z != goal)
(t[z].ch[0] == y) ^ (t[y].ch[0] == x) ? rotate(x) : rotate(y);
rotate(x);
}
if(goal == 0) root = x;
}
int build(int l, int r){
int id = ++num;
int mid = (l + r) >> 1;
t[id].val = str[mid];
if(mid > l){
t[id].ch[0] = build(l, mid - 1);
t[t[id].ch[0]].fa = id;
}
if(mid < r){
t[id].ch[1] = build(mid + 1, r);
t[t[id].ch[1]].fa = id;
}
pushup(id);
return id;
}
inline int findKth(int k){
int u = root;
while(1){
pushdown(u);
if(t[t[u].ch[0]].size >= k) u = t[u].ch[0];
else if(t[t[u].ch[0]].size == k - 1) return u;
else k -= t[t[u].ch[0]].size + 1, u = t[u].ch[1];
}
}
char opt;
int main(){
tot = (2 + (len = read())); n = read(); t[0].m = -INF;
root = ++num; t[num].val = -INF;
t[++num].fa = root; t[num].val = -INF;
t[root].ch[1] = num; t[num].size = 1; t[root].size = 2;
for(int i = 1; i <= len; ++i) str[i] = read();
a = build(1, len); t[t[root].ch[1]].ch[0] = a;
t[a].fa = t[root].ch[1];
pushup(t[root].ch[1]); pushup(root);
for(int i = 1; i <= n; ++i){
opt = getchar();
while(opt < 'A' || opt > 'Z') opt = getchar();
if(opt == 'G'){
a = read(); b = read();
int l = findKth(a), r = findKth(a + b + 1);
Splay(l, 0); Splay(r, l);
printf("%d\n", t[t[t[root].ch[1]].ch[0]].sum);
}
if(opt == 'R'){
a = read(); b = read();
int l = findKth(a), r = findKth(a + b + 1);
Splay(l, 0); Splay(r, l);
a = t[t[root].ch[1]].ch[0];
t[a].lazy ^= 1;
}
if(opt == 'M'){
getchar();
if(getchar() == 'X'){
while(getchar() != 'M');
a = findKth(tot);
Splay(findKth(1), 0); Splay(a, root);
printf("%d\n", t[t[a].ch[0]].m);
}
else{
a = read(); b = read(); c = read();
int l = findKth(a), r = findKth(a + b + 1);
Splay(l, 0); Splay(r, l);
a = t[t[root].ch[1]].ch[0];
t[a].lz = 1; t[a].to = t[a].val = c; t[a].sum = c * t[a].size; t[a].lm = t[a].rm = max(c * t[a].size, 0);
pushdown(a); Splay(a, 0);
}
}
if(opt == 'I'){
a = read(); tot += (len = read());
for(int j = 1; j <= len; ++j) str[i] = read();
int l = findKth(a), r = findKth(a + 1);
Splay(l, 0); Splay(r, l);
a = t[root].ch[1]; b = build(1, len);
t[a].ch[0] = b; t[b].fa = a; pushup(a); Splay(a, 0);
}
if(opt == 'D'){
a = read(); tot -= (b = read());
int l = findKth(a), r = findKth(a + b + 1);
Splay(l, 0); Splay(r, l);
t[t[root].ch[1]].ch[0] = 0;
pushup(t[root].ch[1]);
Splay(t[root].ch[1], 0);
}
}
return 0;
}

【洛谷 P2042】 [NOI2005]维护数列(自闭记第一期)的更多相关文章

  1. 洛谷 P2042 [NOI2005]维护数列-Splay(插入 删除 修改 翻转 求和 最大的子序列)

    因为要讲座,随便写一下,等讲完有时间好好写一篇splay的博客. 先直接上题目然后贴代码,具体讲解都写代码里了. 参考的博客等的链接都贴代码里了,有空再好好写. P2042 [NOI2005]维护数列 ...

  2. 洛谷P2042 [NOI2005]维护数列

    #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> #in ...

  3. 洛谷.2042.[NOI2005]维护数列(Splay)

    题目链接 2017.12.24 第一次写: 时间: 2316ms (1268ms) 空间: 19.42MB (19.5MB)(O2) 注:洛谷测的时间浮动比较大 /* 插入一段数:将这些数先单独建一棵 ...

  4. 【洛谷P2042】维护数列

    题目大意:维护一个序列,支持区间插入,区间删除,区间翻转,查询区间元素和,查询区间最大子段和操作. 题解:毒瘤题...QAQ打完这道题发现自己以前学了一个假的 Splay.. 对于区间操作,用 spl ...

  5. P2042 [NOI2005]维护数列 && Splay区间操作(四)

    到这里 \(A\) 了这题, \(Splay\) 就能算入好门了吧. 今天是个特殊的日子, \(NOI\) 出成绩, 大佬 \(Cu\) 不敢相信这一切这么快, 一下子机房就只剩我和 \(zrs\) ...

  6. P2042 [NOI2005]维护数列[splay或非旋treap·毒瘤题]

    P2042 [NOI2005]维护数列 数列区间和,最大子列和(必须不为空),支持翻转.修改值.插入删除. 练码力的题,很毒瘤.个人因为太菜了,对splay极其生疏,犯了大量错误,在此记录,望以后一定 ...

  7. Luogu P2042 [NOI2005]维护数列(平衡树)

    P2042 [NOI2005]维护数列 题意 题目描述 请写一个程序,要求维护一个数列,支持以下\(6\)种操作:(请注意,格式栏中的下划线'_'表示实际输入文件中的空格) 输入输出格式 输入格式: ...

  8. BZOJ 1500 Luogu P2042 [NOI2005] 维护数列 (Splay)

    手动博客搬家: 本文发表于20180825 00:34:49, 原地址https://blog.csdn.net/suncongbo/article/details/82027387 题目链接: (l ...

  9. Luogu P2042 [NOI2005]维护数列

    题目描述 请写一个程序,要求维护一个数列,支持以下 6 种操作:(请注意,格式栏 中的下划线' _ '表示实际输入文件中的空格) 输入输出格式 输入格式: 输入文件的第 1 行包含两个数 N 和 M, ...

  10. P2042 [NOI2005]维护数列

    思路 超级恶心的pushdown 昏天黑地的调 让我想起了我那前几个月的线段树2 错误 这恶心的一道题终于过了 太多错误,简直说不过来 pushup pushdown 主要就是这俩不太清晰,乱push ...

随机推荐

  1. thrift多平台安装

    thrift支持多语言的RPC,一直都想深入学习了解thrift,最近有空,就上网查了些资料,学习了一下,对它的使用有了一些了解.本篇是写thrift的安装,使用方法会另起一篇来写. 本文使用thri ...

  2. HDU 2115 I Love This Game

    http://acm.hdu.edu.cn/showproblem.php?pid=2115 Problem Description Do you like playing basketball ? ...

  3. pycharm/webstorm创建react项目

    1.安装nodejs 2.安装reactapp依赖:npm install -g create-react-app 在pycharm/webstorm中选择react

  4. tomcat8配置管理员后仍然报403

    tomcat8配置管理员后仍然报403   修改conf/tomcat-users.xml <role rolename="manager"/> <role ro ...

  5. Matlab快捷键

    1. 在命令窗口(Command Window)中: 1) [上.下键]――切换到之前.之后的命令,可以重复按多次来达到你想要的命令 2) clc――清除命令窗口显示的语句,此命令并不清空当前工作区的 ...

  6. 【转】log4j.properties文件的配置

    一.前言 log4j使用的还是比较多的,但是对于其配置又很难描述清楚要怎么配置,说明我自己对于log4j的配置并不是非常熟悉,所以在网上找了一篇详尽的 博文转载,在此非常感谢原文作者的辛苦付出,如有需 ...

  7. 当要将其他类型转成String类型时候 看String的方法

    当要将其他类型转成String类型时候 看String的方法进行转换

  8. 【zoj2314】Reactor Cooling 有上下界可行流

    题目描述 The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuc ...

  9. BZOJ4584 & 洛谷3643 & UOJ204:[APIO2016]划艇——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=4584 https://www.luogu.org/problemnew/show/P3643 ht ...

  10. 【神仙DP】【UVa11400】Lighting System Design

    传送门 Description Translation 题目大意:有一个照明系统需要用到n种灯,每种灯的电压为V,电源费用K,每个灯泡费用为C,需要该灯的数量为L.注意到,电压相同的灯泡只需要共享一个 ...