【洛谷 P2042】 [NOI2005]维护数列(自闭记第一期)
首先,这题我是没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]维护数列(自闭记第一期)的更多相关文章
- 洛谷 P2042 [NOI2005]维护数列-Splay(插入 删除 修改 翻转 求和 最大的子序列)
因为要讲座,随便写一下,等讲完有时间好好写一篇splay的博客. 先直接上题目然后贴代码,具体讲解都写代码里了. 参考的博客等的链接都贴代码里了,有空再好好写. P2042 [NOI2005]维护数列 ...
- 洛谷P2042 [NOI2005]维护数列
#include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> #in ...
- 洛谷.2042.[NOI2005]维护数列(Splay)
题目链接 2017.12.24 第一次写: 时间: 2316ms (1268ms) 空间: 19.42MB (19.5MB)(O2) 注:洛谷测的时间浮动比较大 /* 插入一段数:将这些数先单独建一棵 ...
- 【洛谷P2042】维护数列
题目大意:维护一个序列,支持区间插入,区间删除,区间翻转,查询区间元素和,查询区间最大子段和操作. 题解:毒瘤题...QAQ打完这道题发现自己以前学了一个假的 Splay.. 对于区间操作,用 spl ...
- P2042 [NOI2005]维护数列 && Splay区间操作(四)
到这里 \(A\) 了这题, \(Splay\) 就能算入好门了吧. 今天是个特殊的日子, \(NOI\) 出成绩, 大佬 \(Cu\) 不敢相信这一切这么快, 一下子机房就只剩我和 \(zrs\) ...
- P2042 [NOI2005]维护数列[splay或非旋treap·毒瘤题]
P2042 [NOI2005]维护数列 数列区间和,最大子列和(必须不为空),支持翻转.修改值.插入删除. 练码力的题,很毒瘤.个人因为太菜了,对splay极其生疏,犯了大量错误,在此记录,望以后一定 ...
- Luogu P2042 [NOI2005]维护数列(平衡树)
P2042 [NOI2005]维护数列 题意 题目描述 请写一个程序,要求维护一个数列,支持以下\(6\)种操作:(请注意,格式栏中的下划线'_'表示实际输入文件中的空格) 输入输出格式 输入格式: ...
- BZOJ 1500 Luogu P2042 [NOI2005] 维护数列 (Splay)
手动博客搬家: 本文发表于20180825 00:34:49, 原地址https://blog.csdn.net/suncongbo/article/details/82027387 题目链接: (l ...
- Luogu P2042 [NOI2005]维护数列
题目描述 请写一个程序,要求维护一个数列,支持以下 6 种操作:(请注意,格式栏 中的下划线' _ '表示实际输入文件中的空格) 输入输出格式 输入格式: 输入文件的第 1 行包含两个数 N 和 M, ...
- P2042 [NOI2005]维护数列
思路 超级恶心的pushdown 昏天黑地的调 让我想起了我那前几个月的线段树2 错误 这恶心的一道题终于过了 太多错误,简直说不过来 pushup pushdown 主要就是这俩不太清晰,乱push ...
随机推荐
- ZooKeeper server &&client
写了一个关于zookeepeer应用的简单demo 服务端定时的向zookeeper集群注册,客户端监听zookeeper服务节点变化,一旦变化,立刻响应,更新服务端列表 服务端代码: #includ ...
- lintcode-176-图中两个点之间的路线
176-图中两个点之间的路线 给出一张有向图,设计一个算法判断两个点 s 与 t 之间是否存在路线. 样例 如下图: for s = B and t = E, return true for s = ...
- LintCode-379.将数组重新排序以构造最小值
将数组重新排序以构造最小值 给定一个整数数组,请将其重新排序,以构造最小值. 注意事项 The result may be very large, so you need to return a st ...
- vue-cli3配置postcss-cssnext
1. // npm install autoprefixer --save-dev 此步不需要了,因为postcss-cssnext自带这个依赖 npm install postcss-cssnext ...
- bzoj1143-祭祀
题目 给出一个有向无环图,要在上面安放祭祀点.两个祭祀点必须不可达,求最多能安放多少个祭祀点. 分析 由于一条无法再延伸链上只能安放一个祭祀点,而我们要求的是最多能安放祭祀点的个数,所以要求的就是最长 ...
- [GDOI2014]拯救莫莉斯 状压DP
题面: 莫莉斯·乔是圣域里一个叱咤风云的人物,他凭借着自身超强的经济头脑,牢牢控制了圣域的石油市场. 圣域的地图可以看成是一个n*m的矩阵.每个整数坐标点(x , y)表示一座城市( 1\le x\l ...
- BZOJ5292 & 洛谷4457 & LOJ2513:[BJOI2018]治疗之雨——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=5292 https://www.luogu.org/problemnew/show/P4457 ht ...
- HDU3065:病毒侵袭持续中——题解
http://acm.hdu.edu.cn/showproblem.php?pid=3065 ———————————————————————————— Problem Description 小t非常 ...
- Redux的应该注意的问题
1. Store中的State修改不能直接修改原有的State,若直接修改State,则redux中的所有操作都将指向 内存中的同一个state,将无法获取每一次操作前后的state,就无法追溯sta ...
- NOIP2018普及&提高题解
第一次考$NOIP$的我已经自闭了 $CCF$告诉我们了一件事情,要对自己写的程序有信仰,相信$CCF$的数据是水的 普及组: 分数:$100+100+30+100=330$ $1.titile$: ...