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
题目 题解 这道题可以说是数列问题的大BOSS,也算是这一周来学习splay等数据结构的一个总结. 我们一个一个地看这些操作. 对于操作1,我们首先建一棵子树,直接接上原树即可. 对于操作2,我们找到 ...
- BZOJ1500: [NOI2005]维修数列 [splay序列操作]【学习笔记】
以前写过这道题了,但我把以前的内容删掉了,因为现在感觉没法看 重写! 题意: 维护一个数列,支持插入一段数,删除一段数,修改一段数,翻转一段数,查询区间和,区间最大子序列 splay序列操作裸题 需要 ...
- [bzoj1500][NOI2005 维修数列] (splay区间操作)
Description Input 输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初始时数列中数的个数,M表示要进行的操作数目. 第2行包含N个数字,描述初始时的数列. 以下M行,每 ...
- BZOJ1500 [NOI2005]维修数列(Splay tree)
[Submit][Status][Discuss] Description 请写一个程序,要求维护一个数列,支持以下 6 种操作: 请注意,格式栏 中的下划线‘ _ ’表示实际输入文件中的空格 Inp ...
- bzoj1500: [NOI2005]维修数列 (Splay+变态题)
Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 11353 Solved: 3553 [Submit][Status][Discuss] Descrip ...
- [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 ...
随机推荐
- 【C#进阶系列】28 基元线程同步构造
多个线程同时访问共享数据时,线程同步能防止数据损坏.之所以要强调同时,是因为线程同步问题实际上就是计时问题. 不需要线程同步是最理想的情况,因为线程同步一般很繁琐,涉及到线程同步锁的获取和释放,容易遗 ...
- composer 代码库管理
composer 代码库管理 参考资料:https://packagist.org/about#naming-your-package简介composer 命令的请求源是 packagist.org* ...
- 【Java每日一题】20161222
package Dec2016; import java.util.Random; public class Ques1222 { public static void main(String[] a ...
- appach2.4 + php7 +mysql5.7.14 配置
步骤1.首先打开Apache2.2\conf里面的httpd.conf文件.在里面找到: ServerRoot ,改成Appache所在目录 步骤二 在LoadModule 后面添加支持php7的扩 ...
- Java03
字符输入 Scanner scan = new Scanner(System.in); char ch = scan.next().charAt(0); 车到路口例 package C ...
- Mac入门(一)基本用法
我前五年一直外包到微软,每天使用的都是Windows系统和.NET. 2012年加入VMware, 公司的工作机是台Mac 笔记本(MacBook Pro), 所以有机会接触Mac系统 Mac和Wi ...
- C# Cache 设定缓存过期时间方法 绝对过期时间 和 相对过期时间(即:访问激活后不过期)
摘自: http://www.cnblogs.com/zj1111184556/p/3493840.html 1. 设定绝对过期时间 /// <summary> /// 设定绝对的过期时间 ...
- sql 小操作
1.COALESCE(oe.promotion_value, 0) 设置默认值 2.group_concat(pspk.name) 合并字段
- jquery动态改变div宽度和高度
效果体验:http://keleyi.com/keleyi/phtml/jquery/23.htm 完整代码: <!DOCTYPE html PUBLIC "-//W3C//DTD X ...
- JS学习笔记9之event事件及其他事件
-->鼠标事件-->event事件对象-->默认事件-->键盘事件(keyCode)-->拖拽效果 一.鼠标事件 onclick ---------------鼠标点击事 ...