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 ...
随机推荐
- time与datetime模块
在python中,通常用下面几种方式来表示时间: 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量. 格式化的时间字符串(format s ...
- hive和hbase比较(整理)
hive1.可以理解为一种SQL执行引擎,对SQL的支持最终转换为map/reduce任务2.不支持更新.删除操作,但可以插入3.任务不是实时执行,用时一般为数分钟到数小时4.本身可以不存储数据,只存 ...
- Oracle查询CLOB字段类型的内容
select dbms_lob.substr(lo.ldtext) as text from longdescription lo
- 用SQL语句查询zabbix的监控数据
参考地址:http://blog.51cto.com/sfzhang88/1558254 -- 获取主机id -- 10084 select hostid from hosts where host= ...
- 新的表格展示利器 Bootstrap Table Ⅰ
1.bootstrap table简介及特征 Bootstrap Table是国人开发的一款基于 Bootstrap 的 jQuery 表格插件,通过简单的设置,就可以拥有强大的单选.多选.排序.分 ...
- python多线程之t.setDaemon(True) 和 t.join()
0.目录 1.参考2.结论 (1)通过 t.setDaemon(True) 将子线程设置为守护进程(默认False),主线程代码执行完毕后,python程序退出,无需理会守护子线程的状态. ...
- nginx 服务脚本编写模板
编写nginx服务脚本:脚本内容如下: [root@www ~]# cat /etc/init.d/nginx #!/bin/bash # nginx Startup script for the N ...
- C# 文件拖放到此程序的操作
问题描述: 怎么写代码可以实现指定类型的文件通过鼠标拖放显示在程序的文本框中,如:选中3个文件(3个文件的格式有MP3和wma)拖到程序,程序的文本框显示这三个文件的路径...解决代码: thi ...
- 【转】Windows Server 2008 R2下安装 .net framework3.5
原文地址:http://hi.baidu.com/tonny_dxf/item/6831bcdc3d7c06e7b2f7777c [你必须用角色管理工具安装.net framework3.5 ...
- nginx+keepalived实现 负载均衡 高可用
Vip: 192.168.220.18 Rip1:192.168.220.2 Rip:192.168.220.3 Rip可以配置在一个服务器上通过ip做虚拟主机 1 rs上配置环境 2 配置应用 Yu ...