BZOJ1500: [NOI2005]维修数列 [splay序列操作]【学习笔记】
以前写过这道题了,但我把以前的内容删掉了,因为现在感觉没法看
重写!
题意:
维护一个数列,支持插入一段数,删除一段数,修改一段数,翻转一段数,查询区间和,区间最大子序列
splay序列操作裸题
需要回收节点编号,所以用到$sz和nw()$,通常维护序列是不用sz的
splay维护的是这个序列,不再存在平衡树左小右大的性质
操作一段区间$[l,r]$,将$l-1\ splay$到根,$r+1\ splay$到右孩子,他的左孩子就是要操作的区间
为了方便加入两个哨兵节点
注意splay和线段树不同,每个节点都代表了一个元素
对于本题来说,因为有可能是修改成0或负数,所以tag=1表示执行了修改操作而不是修改成什么
下传标记修改会覆盖查询,无论时间先后
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define lc t[x].ch[0]
#define rc t[x].ch[1]
#define pa t[x].fa
typedef long long ll;
const int N=5e5, INF=1e9;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
} int n, Q, a[N], k, tot;
char s[]; struct meow{
int ch[], fa, size, v, sum, mx, lm, rm, tag, rev;
meow() {}
meow(int val) {ch[]=ch[]=fa=tag=rev=; size=; v=sum=mx=lm=rm=val;}
}t[N];
int root, sz;
inline int wh(int x) {return t[pa].ch[] == x;}
int st[N], top;
inline int nw() {return top ? st[top--] : ++sz;} inline void paint(int x,int v) {
t[x].tag = ; t[x].v = v;
t[x].sum = t[x].size*v;
t[x].mx = t[x].lm = t[x].rm = max(t[x].sum, v);
t[x].rev = ;
}
inline void rever(int x) {
if(t[x].tag) return; //hi
t[x].rev^=;
swap(lc, rc); swap(t[x].lm, t[x].rm);
}
inline void pushDown(int x) {
if(t[x].rev) {
if(lc) rever(lc);
if(rc) rever(rc);
t[x].rev=;
}
if(t[x].tag) {
if(lc) paint(lc, t[x].v);
if(rc) paint(rc, t[x].v);
t[x].tag=;
}
} inline void update(int x) {
t[x].size = t[lc].size + t[rc].size + ;
t[x].sum = t[lc].sum + t[rc].sum + t[x].v;
t[x].mx = max(max(t[lc].mx, t[rc].mx), max(, t[lc].rm) + t[x].v + max(, t[rc].lm) );
t[x].lm = max(t[lc].lm, t[lc].sum + t[x].v + max(, t[rc].lm) );
t[x].rm = max(t[rc].rm, t[rc].sum + t[x].v + max(, t[lc].rm) );
} inline void rotate(int x) {
int f=t[x].fa, g=t[f].fa, c=wh(x);
if(g) t[g].ch[wh(f)]=x; t[x].fa=g;
t[f].ch[c] = t[x].ch[c^]; t[t[f].ch[c]].fa=f;
t[x].ch[c^]=f; t[f].fa=x;
update(f); update(x);
}
inline void splay(int x,int tar) {
for(; pa!=tar; rotate(x))
if(t[pa].fa != tar) rotate(wh(x)==wh(pa) ? pa : x);
if(tar==) root=x;
} void build(int &x,int l,int r,int f) {
int mid = (l+r)>>;
x=nw(); t[x]=meow(a[mid]); t[x].fa=f;
if(l==r) return;
if(l<mid) build(lc, l, mid-, x);
if(mid<r) build(rc, mid+, r, x);
update(x);
} inline int kth(int k) {
int x=root, lsize=;
while(x) {
pushDown(x);
int _ = lsize + t[lc].size;
if(k<=_) x=lc;
else if(k<=_+) return x;
else lsize=_+, x=rc;
}
return -;
} void Ins(int k, int tot) {
for(int i=; i<=tot; i++) a[i]=read();
int f=kth(k+); splay(f, );
int x=kth(k+); splay(x, f);
build(lc, , tot, x);
update(x); update(f);
} void erase(int x) {
if(!x) return;
st[++top]=x;
erase(lc); erase(rc);
}
void Del(int k, int tot) {
int f=kth(k); splay(f, );
int x=kth(k+tot+); splay(x, f);
erase(lc); lc=;
update(x); update(f);
} void Mak(int k, int tot) {
int f=kth(k); splay(f, );
int x=kth(k+tot+); splay(x, f);
paint(lc, read());
update(x); update(f);
} void Rev(int k, int tot) {
int f=kth(k); splay(f, );
int x=kth(k+tot+); splay(x, f);
rever(lc);
update(x); update(f);
} int Sum(int k, int tot) {
int f=kth(k); splay(f, );
int x=kth(k+tot+); splay(x, f);
return t[lc].sum;
} int main() {
//freopen("in","r",stdin);
n=read(); Q=read();
for(int i=; i<=n+; i++) a[i]=read(); a[] = a[n+] = -INF;
t[]=meow(-INF); t[].sum=t[].size=;
build(root, , n+, );
for(int i=; i<=Q; i++) { //printf("Q %d\n",i);
scanf("%s",s+);
if(s[]=='X') printf("%d\n", t[root].mx);
else {
k=read(); tot=read();
if(s[]=='I') Ins(k, tot);
if(s[]=='D') Del(k, tot);
if(s[]=='M') Mak(k, tot);
if(s[]=='R') Rev(k, tot);
if(s[]=='G') printf("%d\n", Sum(k, tot));
}
}
}
BZOJ1500: [NOI2005]维修数列 [splay序列操作]【学习笔记】的更多相关文章
- [bzoj1500][NOI2005 维修数列] (splay区间操作)
Description Input 输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初始时数列中数的个数,M表示要进行的操作数目. 第2行包含N个数字,描述初始时的数列. 以下M行,每 ...
- BZOJ1500: [NOI2005]维修数列[splay ***]
1500: [NOI2005]维修数列 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 12278 Solved: 3880[Submit][Statu ...
- bzoj1500: [NOI2005]维修数列 (Splay+变态题)
Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 11353 Solved: 3553 [Submit][Status][Discuss] Descrip ...
- [bzoj1500][NOI2005]维修数列——splay
题目 题解 这道题可以说是数列问题的大BOSS,也算是这一周来学习splay等数据结构的一个总结. 我们一个一个地看这些操作. 对于操作1,我们首先建一棵子树,直接接上原树即可. 对于操作2,我们找到 ...
- BZOJ1500 [NOI2005]维修数列(Splay tree)
[Submit][Status][Discuss] Description 请写一个程序,要求维护一个数列,支持以下 6 种操作: 请注意,格式栏 中的下划线‘ _ ’表示实际输入文件中的空格 Inp ...
- [BZOJ1500][NOI2005]维修数列 解题报告 Splay
Portal Gun:[BZOJ1500][NOI2005]维修数列 有一段时间没写博客了,最近在刚数据结构......各种板子背得简直要起飞,题目也是一大堆做不完,这里就挑一道平衡树的题来写写好了 ...
- [BZOJ1500][NOI2005]维修数列---解题报告
Portal Gun:[BZOJ1500][NOI2005]维修数列 有一段时间没写博客了,最近在刚数据结构......各种板子背得简直要起飞,题目也是一大堆做不完,这里就挑一道平衡树的题来写写好了 ...
- 【BZOJ1500】[NOI2005]维修数列 Splay
[BZOJ1500][NOI2005]维修数列 Description Input 输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初始时数列中数的个数,M表示要进行的操作数目.第2行 ...
- 【BZOJ-1500】维修数列 Splay
1500: [NOI2005]维修数列 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 11047 Solved: 3460[Submit][Statu ...
随机推荐
- RESTful架构解读
什么是REST REST与技术无关,代表的是一种软件架构风格.REST全称是Representational State Tranfer, 表征性状态转移. REST从资源的角度类审视整个网络,它将分 ...
- JS实现继承的几种方式(转)
转自:幻天芒的博客 前言 JS作为面向对象的弱类型语言,继承也是其非常强大的特性之一.那么如何在JS中实现继承呢?让我们拭目以待. JS继承的实现方式 既然要实现继承,那么首先我们得有一个父类,代码如 ...
- 论文阅读笔记十二:Encoder-Decoder with Atrous Separable Convolution for Semantic Image Segmentation(DeepLabv3+)(CVPR2018)
论文链接:https://arxiv.org/abs/1802.02611 tensorflow 官方实现: https: //github.com/tensorflow/models/tree/ma ...
- 步步为营-104-Lambda语句
1:Lambda的拼接 首先借助一个Lambda的帮助类 using System; using System.Collections.Generic; using System.Linq; usin ...
- java Map按Key排序
public static void main(String[] args) { Map<String, String> map = new TreeMap<String, Stri ...
- C/C++内存管理器
C标准库提供了malloc,free,calloc,realloc,C++标准库还提供了new, new[], delete, delete[].这些用来管理内存,看起来够用了,为啥还要自己写一个内存 ...
- Knockout示例:User数据CRUD
模拟数据user.json. { "page": 0, "rows": 0, "total": 161, "isSuccess&q ...
- Linux SSH & SCP命令
SSH SSH为建立在应用层和传输层基础上的安全协议 sshd服务使用SSH协议进行远程控制,或在计算机之间传送文件.而实现此功能的telnet(远程桌面) 是不安全的,使用明文传送密码 ssh ss ...
- mysql 查询结果中增加序号
) as rownum,person_id from t_base_person
- 【Android】性能优化的一些方法
1.采用硬件加速,在androidmanifest.xml中application添加 android:hardwareAccelerated="true".不过这个需要在and ...