终于A了这题。。。这题还是很好。。。但是我太菜。。。重构了三遍qwq


FHQ Treap大法好!qwq。。。~~

Ins:直接拿输入造一棵树,把原来的树split成[1,pos],[pos+1,n],然后merge三棵树;

Del:把要删的区间split出来,merge他两边的树,记着要回收内存;

Mk-Same:把要改的区间split出来,打上标记,更新这棵树根的信息(见cover(x,v)),再merge回去;

Rev:把要翻转的区间split出来,打上标记,更新这棵树根的信息(见reverse(x)),再merge回去;

Get:把要求的区间split出来,输出sum[根],然后再merge回去;

Mx:输出mx[根]

维护最大子段和的思路跟线段树是类似的(链接

顺便说一下自己当初犯的错误:

1.没有及时下放rev标记;重构x1

2.由于读取字符串用的getchar(),但是操作中有些串有负号。。。导致我读进来一堆负数。。。以后乖乖用字符串吧。。;重构x2,第三次重构才发现

3.stk内存池开小了。。。以为自己哪里递归写错了MLE+RE,后来看到网上有用queue的,就水了一波结果水过了。。后来又换成了数组stk。。;重构x3

代码:

#include<cstdio>
#include<iostream>
#include<queue>
#include<cstdlib>
#define R register int
#define ls(x) (ch[x][0])
#define rs(x) (ch[x][1])
using namespace std;
const int N=,Inf=;
inline int g() {
R ret=,fix=; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-:fix;
do ret=ret*+(ch^); while(isdigit(ch=getchar())); return ret*fix;
}
int n,m,tot,rt,top,sz[N],ch[N][],vl[N],mxl[N],mxr[N],mx[N],sum[N],tg[N],cov[N],dat[N],a[N>>],stk[N<<];
inline int max(int a,int b) {return a>b?a:b;}
inline int cre(int v) {R x; if(top) x=stk[top],--top; else x=++tot;
sz[x]=,ls(x)=rs(x)=tg[x]=; cov[x]=Inf,dat[x]=rand();
vl[x]=sum[x]=mx[x]=mxl[x]=mxr[x]=v; return x;
}
inline void upd(int x) {
if(!x) return ; sz[x]=sz[ls(x)]+sz[rs(x)]+; sum[x]=sum[ls(x)]+sum[rs(x)]+vl[x];
mx[x]=max(max(mxl[rs(x)],)+vl[x]+max(mxr[ls(x)],),max(mx[ls(x)],mx[rs(x)]));
mxl[x]=max(mxl[ls(x)],sum[ls(x)]+vl[x]+max(mxl[rs(x)],));
mxr[x]=max(mxr[rs(x)],sum[rs(x)]+vl[x]+max(mxr[ls(x)],));
}
inline void cover(int x,int v) {vl[x]=cov[x]=v,sum[x]=sz[x]*v,mxl[x]=mxr[x]=max(sum[x],),mx[x]=max(vl[x],sum[x]);}
inline void reverse(int x) {swap(ls(x),rs(x)),swap(mxl[x],mxr[x]),tg[x]^=;}
inline void spread(int x) {
if(tg[x]) {if(ls(x)) reverse(ls(x)); if(rs(x)) reverse(rs(x));}
if(cov[x]!=Inf) {if(ls(x)) cover(ls(x),cov[x]); if(rs(x)) cover(rs(x),cov[x]);} tg[x]=,cov[x]=Inf;
}
inline int build(int l,int r) {
if(l>r) return ; R md=l+r>>,v=a[md]; R x=cre(v);
ls(x)=build(l,md-),rs(x)=build(md+,r); upd(x); return x;
}
inline void split(int o,int rk,int& x,int& y) {
if(!o) {x=y=; return ;} spread(o);
if(rk>sz[ls(o)]) x=o,split(rs(x),rk-sz[ls(x)]-,rs(x),y);
else y=o,split(ls(y),rk,x,ls(y)); upd(o);
}
inline int merge(int x,int y) {
if(!x||!y) return x|y; spread(x),spread(y);
if(dat[x]<dat[y]) {rs(x)=merge(rs(x),y); upd(x); return x;}
else {ls(y)=merge(x,ls(y)); upd(y); return y;}
}
inline void del(int x) {if(!x) return; del(ls(x)),stk[++top]=x,del(rs(x));}
signed main() { R w,x,y,z; srand(); vl[]=mx[]=-Inf,sum[]=sz[]=tg[]=cov[]=;
n=g(),m=g(); for(R i=,x;i<=n;++i) a[i]=g(); rt=build(,n); while(m--) { register char ch;
while(!isalpha(ch=getchar())); if(ch=='M') {
getchar(),ch=getchar(); if(ch=='K') {
while(!isspace(ch=getchar())); R pos=g(),tot=g(),c=g(); split(rt,pos-,x,y),split(y,tot,y,z);
cover(y,c); rt=merge(x,merge(y,z));
} else if(ch=='X') {printf("%d\n",mx[rt]); while(!isspace(ch=getchar()));}
} else if(ch=='I') {R pos=g(),tot=g(); for(R i=,x;i<=tot;++i) a[i]=g(); z=build(,tot);split(rt,pos,x,y),rt=merge(merge(x,z),y);}
else if(ch=='D') {while(!isspace(ch=getchar())); R pos=g(),tot=g(); split(rt,pos-,x,y),split(y,tot,y,z),rt=merge(x,z); del(y);}
else if(ch=='R') {while(!isspace(ch=getchar())); R pos=g(),tot=g(); split(rt,pos-,x,y),split(y,tot,y,z); reverse(y); rt=merge(x,merge(y,z));}
else if(ch=='G') {while(!isspace(ch=getchar())); R pos=g(),tot=g(); split(rt,pos-,x,y),split(y,tot,y,z); printf("%d\n",sum[y]); rt=merge(x,merge(y,z));}
}
}

2019.05.11

BZOJ 1500 [NOI2005]维修数列 FHQ Treap的更多相关文章

  1. bzoj 1500: [NOI2005]维修数列 splay

    1500: [NOI2005]维修数列 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 6556  Solved: 1963[Submit][Status ...

  2. BZOJ 1500: [NOI2005]维修数列 (splay tree)

    1500: [NOI2005]维修数列 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 4229  Solved: 1283[Submit][Status ...

  3. [BZOJ 1500] [NOI2005] 维修数列

    题目链接:BZOJ - 1500 题目分析 我要先说一下,这道题我写了一晚上,然后Debug了一整个白天..........再一次被自己的蒟蒻程度震惊= = 这道题是传说中的Splay维护数列的Bos ...

  4. 【BZOJ】1500: [NOI2005]维修数列

    [算法]splay [题解]数据结构 感谢Occult的模板>_<:HYSBZ 1500 维修数列 #include<cstdio> #include<cctype> ...

  5. 1500: [NOI2005]维修数列

    Description Input 输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初始时数列中数的个数,M表示要进行的操作数目.第2行包含N个数字,描述初始时的数列.以下M行,每行一 ...

  6. 【BZOJ】1500: [NOI2005]维修数列(splay+变态题)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1500 模板不打熟你确定考场上调试得出来? 首先有非常多的坑点...我遇到的第一个就是,如何pushu ...

  7. bzoj千题计划221:bzoj1500: [NOI2005]维修数列(fhq treap)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1500 1.覆盖标记用INF表示无覆盖标记,要求可能用0覆盖 2.代表空节点的0号节点和首尾的两个虚拟 ...

  8. [NOI2005] 维修数列

    1500: [NOI2005]维修数列 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 8397  Solved: 2530 Description In ...

  9. [BZOJ1500][NOI2005]维修数列---解题报告

    Portal Gun:[BZOJ1500][NOI2005]维修数列 有一段时间没写博客了,最近在刚数据结构......各种板子背得简直要起飞,题目也是一大堆做不完,这里就挑一道平衡树的题来写写好了 ...

随机推荐

  1. AngularJS方法 —— angular.copy

    描述: 复制一个对象或者一个数组(好吧,万物皆对象,数组也是一个对象). 如果省略了destination,一个新的对象或数组将会被创建出来: 如果提供了destination,则source对象中的 ...

  2. CH#24C 逃不掉的路 和 HDU3686 Traffic Real Time Query System

    逃不掉的路 CH Round #24 - 三体杯 Round #1 题目描述 现代社会,路是必不可少的.任意两个城镇都有路相连,而且往往不止一条.但有些路连年被各种XXOO,走着很不爽.按理说条条大路 ...

  3. node.js的国内源

    node.js在使用npm安装包是,由于源是国外的,有可能会被GFW屏蔽. 通过下面的方法可以把源指向国内的. 具体方法如下: 编辑 ~/.npmrc 加入下面内容 registry = http:/ ...

  4. HDOJ1548(DFS超内存,BFS过了)

    DFS代码 #include<iostream> #include<cstdio> using namespace std; #define Min(a,b) (a<b) ...

  5. libvirt, libvirt-python, libvirtd 关系浅析

    libvirt 官方解释:  http://libvirt.org/ 见分隔线以下. 我的理解:libvirt 作为一个中间层,封装了对下层虚拟化 hypervisor 的操作方法.也就是说,无论你是 ...

  6. [置顶] 什么是C语言结构体字节对齐,为什么要对齐?

    一.概念 对齐跟数据在内存中的位置有关.如果一个变量的内存地址正好位于它长度的整数倍,他就被称做自然对齐.比如在32位cpu下,假设一个整型变量的地址为0x00000004,那它就是自然对齐的.   ...

  7. C#使用NPOI读取电子表格Excel到DataGridView中

    上篇博文中已经介绍了如何写入Excel文件.这篇再介绍一下 如何从Excel中读取数据并保存到DataGridView中. 从Excel中读取数据并保存至DataGridView中,Excel文件第一 ...

  8. WPF Canvas

    Canvas为容器控件,用于定位. 1.基本应用 <Border HorizontalAlignment="Left" VerticalAlignment="Top ...

  9. 关于web中注册倒数的问题(亲测)

    <title></title>    <script type="text/javascript">        var leftSecond ...

  10. 判断页面是在pc端打开还是在移动端打开

    在项目开发中会遇到在不同的设备中打开页面是不同的,比如: 我在手机中打开一个网站和pc打开一个网站,页面是不同的 具体实施如下 //判断打开网站的终端 var ua = window.navigato ...