BZOJ 1500 [NOI2005]维修数列 FHQ Treap
终于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的更多相关文章
- bzoj 1500: [NOI2005]维修数列 splay
1500: [NOI2005]维修数列 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 6556 Solved: 1963[Submit][Status ...
- BZOJ 1500: [NOI2005]维修数列 (splay tree)
1500: [NOI2005]维修数列 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 4229 Solved: 1283[Submit][Status ...
- [BZOJ 1500] [NOI2005] 维修数列
题目链接:BZOJ - 1500 题目分析 我要先说一下,这道题我写了一晚上,然后Debug了一整个白天..........再一次被自己的蒟蒻程度震惊= = 这道题是传说中的Splay维护数列的Bos ...
- 【BZOJ】1500: [NOI2005]维修数列
[算法]splay [题解]数据结构 感谢Occult的模板>_<:HYSBZ 1500 维修数列 #include<cstdio> #include<cctype> ...
- 1500: [NOI2005]维修数列
Description Input 输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初始时数列中数的个数,M表示要进行的操作数目.第2行包含N个数字,描述初始时的数列.以下M行,每行一 ...
- 【BZOJ】1500: [NOI2005]维修数列(splay+变态题)
http://www.lydsy.com/JudgeOnline/problem.php?id=1500 模板不打熟你确定考场上调试得出来? 首先有非常多的坑点...我遇到的第一个就是,如何pushu ...
- bzoj千题计划221:bzoj1500: [NOI2005]维修数列(fhq treap)
http://www.lydsy.com/JudgeOnline/problem.php?id=1500 1.覆盖标记用INF表示无覆盖标记,要求可能用0覆盖 2.代表空节点的0号节点和首尾的两个虚拟 ...
- [NOI2005] 维修数列
1500: [NOI2005]维修数列 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 8397 Solved: 2530 Description In ...
- [BZOJ1500][NOI2005]维修数列---解题报告
Portal Gun:[BZOJ1500][NOI2005]维修数列 有一段时间没写博客了,最近在刚数据结构......各种板子背得简直要起飞,题目也是一大堆做不完,这里就挑一道平衡树的题来写写好了 ...
随机推荐
- iOS+PHP图片上传
这篇博客用于实现iOS客户端通过POST请求,将图片上传到服务器上.服务器端语言采用PHP,服务器环境使用MAMP搭建.先使用浏览器测试图片是否可以上传,浏览器测试成功之后再测试iOS客户端是否可以成 ...
- #define与typedef区别
1) #define是预处理指令,在编译预处理时进行简单的替换,不作正确性检查,不关含义是否正确照样带入,只有在编译已被展开的源程序时才会发现可能的错误并报错.例如: #define PI 3.141 ...
- GrayCode for state machine
How & Why use Gray Code A gray counter is a binary counter where only one bit changes at a time. ...
- python xml包 xml.etree.ElementTree使用记录
19.7.1 教程 这是一个简短的教程使用xml.etree.ElementTree(简称为et).目标是展示一些构建模块和模块的基本概念 9.7.1.1. XML tree and elements ...
- python os.startfile python实现双击运行程序 python监控windows程序 监控进程不在时重新启动
用python监控您的window服务 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://world77.blog.51cto.co ...
- k8s 基础 k8s架构和组件
k8s 的总架构图
- Nios程序烧写到EPCS方法 - 第1页 - asus119's Blog - EDN China电子设计技术
Nios程序烧写到EPCS方法 - 第1页 - asus119's Blog - EDN China电子设计技术 这里主要是针对EP3C系列FPGA的Nios程序固化到EPCS中的方法做简要说明.硬件 ...
- (转)64位系统安装Delphi7提示Can’t load package:dclite70.bpl 以及 提示地址错误
第一个问题: 今天在64的Win7上安装Delphi7,在启动时候出现如下提示: Can't load package:dclite70.bpl 告诉大家一个解决办法,就是给Delphi32.exe去 ...
- Springboot ResponseEntity IE无法正常下载文件
项目在google浏览器下都很nice了,但当测试到IE的时候开始出现各种问题. 项目是前端js通过URL传参fileName到后台解析返回ResponseEntity 前端代码如下: window. ...
- 24、sam- 详解
http://note.youdao.com/share/?id=312fa04209cb87f7674de9a9544f329a&type=note#/ https://davetang.o ...