【洛谷 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 ...
随机推荐
- <Effective C++>读书摘要--Designs and Declarations<二>
<Item 20> Prefer pass-by-reference-to-const to pass-by-value 1.By default, C++ passes objects ...
- 转 高性能IO模型浅析
高性能IO模型浅析 转自:http://www.cnblogs.com/fanzhidongyzby/p/4098546.html 服务器端编程经常需要构造高性能的IO模型,常见的IO模型有四种: ( ...
- Windows 7中安装Solr7
最新忙里偷闲,研究一下了Lucene.Net,发现操作比较繁琐,同比相似的功能,感觉Solr比较简单,容易使用.不过由于Solr使用的是Java的环境,对于.Net开发的人员来说,还是比较陌生,搭配环 ...
- BZOJ 1407 Savage(拓展欧几里得)
这题的时间复杂度真玄学... O(m*n^2).1e8也能过啊... 首先题目保证m<=1e6. 这启发我们枚举或者二分答案? 但是答案不满足单调性,考虑从小到大枚举m. 对于每一个m,枚举两个 ...
- 使用Visual C ++和Open Folder自定义环境
使用Visual C ++和Open Folder自定义环境 来源 https://blogs.msdn.microsoft.com/vcblog/2016/10/05/bring-your-c-co ...
- 详解 ES6 Modules
详解 ES6 Modules 对于新人朋友来说,想要自己去搞定一个ES6开发环境并不是一件容易的事情,因为构建工具的学习本身又是一个非常大的方向,我们需要花费不少的时间才能掌握它. 好在慢慢的开始有大 ...
- javascript中var let const三种变量声明方式
javascript中var let const三种变量声明方式 1.var ①var表示声明了一个变量,并且可以同时初始化该变量. ②使用var语句声明的变量的作用域是当前执行位置的上下文:一个函 ...
- [BZOJ2067]szn
description BZOJ权限题. solution 一道非常好的二分+贪心题目. 第一问就是\(\frac{\sum_u(deg[u]-1)}{2}+1\). 第二问需要在方案最优的情况下最长 ...
- [洛谷P5075][JSOI2012]分零食
题目大意:有$m(m\leqslant10^8)$个人站成一排,有$n(n\leqslant10^4)$个糖果,若第$i$个人没有糖果,那么第$i+1$个人也没有糖果.一个人有$x$个糖果会获得快乐值 ...
- [洛谷P1341]无序字母对
题目大意:给一张无向图,找一条字典序最小的欧拉路径 题解:若图不连通或有两个以上的奇数点,则没有欧拉路径,可以$dfs$,在回溯时把这个节点加入答案 卡点:没有在回溯时加入答案,导致出现了欧拉路径没走 ...