[BZOJ1500][NOI2005]维修数列(splay)
1500: [NOI2005]维修数列
Time Limit: 10 Sec Memory Limit: 64 MB
Submit: 16266 Solved: 5410
[Submit][Status][Discuss]Description
请写一个程序,要求维护一个数列,支持以下 6 种操作:请注意,格式栏 中的下划线‘ _ ’表示实际输入文件中的空格Input
输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初始时数列中数的个数,M表示要进行的操作数目。
第2行包含N个数字,描述初始时的数列。
以下M行,每行一条命令,格式参见问题描述中的表格。
任何时刻数列中最多含有500 000个数,数列中任何一个数字均在[-1 000, 1 000]内。
插入的数字总数不超过4 000 000个,输入文件大小不超过20MBytes。Output
对于输入数据中的GET-SUM和MAX-SUM操作,向输出文件依次打印结果,每个答案(数字)占一行。
Sample Input
9 8
2 -6 3 5 1 -5 -3 6 3
GET-SUM 5 4
MAX-SUM
INSERT 8 3 -5 7 2
DELETE 12 1
MAKE-SAME 3 3 2
REVERSE 3 6
GET-SUM 5 4
MAX-SUMSample Output
-1
10
1
10HINT
Source
[Submit][Status][Discuss]
战胜恐惧去写就行了,没什么难的,不过是板子题。
主要问题就是如果模板不是非常熟练的话,几乎不可能在写代码之前将所有问题都考虑清楚,这就需要gdb和输出调试的能力,一个datamaker和一个网上的标程对拍还是比较有效的。有几个需要注意的问题:
1. cov标记只存0和1,不要存具体应该下放的数。因为MAKE-SAME的数可能为0,而v[x]本身存的就是需要下放的数。
2. 垃圾要回收,用栈就可以了,这题不卡时间和空间,可以放心的写。
3. 这题MAX-SUM要求至少包含一个数(也就是这个区间全为负数的时候不能输出0),mx[]存的是至少包含一个数的最大区间和,mxl[]和mxr[]存的是靠左/右的连续一段的最大和(可以为0)。这样就需要mx[0]=-inf
4. 多push()和upd(),其余细节考虑清楚就可以了
写了一个小时,调了三个小时。
BZOJ 150T 留念
1 #include<cstdio> 2 #include<algorithm> 3 #include<iostream> 4 #define rep(i,l,r) for (int i=l; i<=r; i++) 5 using namespace std; 6 7 const int N=1000100,inf=1000000000; 8 int n,m,pos,tot,rt,top,k,nd,f[N],sz[N],v[N],sm[N],mxl[N],mxr[N],mx[N],ch[N][2],stk[N],C[N],R[N],a[N]; 9 char op[20]; 10 11 int get(){ int x; if (top) return x=stk[top--]; return ++nd; } 12 void thr(int x){ f[x]=v[x]=sm[x]=mxl[x]=mxr[x]=mx[x]=ch[x][0]=ch[x][1]=0; stk[++top]=x; } 13 14 void cov(int x,int k){ 15 sm[x]=sz[x]*k; v[x]=k; 16 mxl[x]=mxr[x]=(k>0)?sm[x]:0; mx[x]=(k>0)?sm[x]:k; 17 C[x]=1; 18 } 19 20 void rev(int x){ swap(ch[x][0],ch[x][1]); swap(mxl[x],mxr[x]); R[x]^=1; } 21 22 void push(int x){ 23 if (C[x]){ 24 if (ch[x][0]) cov(ch[x][0],v[x]); 25 if (ch[x][1]) cov(ch[x][1],v[x]); C[x]=0; 26 } 27 if (R[x]){ 28 if (ch[x][0]) rev(ch[x][0]); 29 if (ch[x][1]) rev(ch[x][1]); R[x]=0; 30 } 31 } 32 33 void upd(int x){ 34 int ls=ch[x][0],rs=ch[x][1]; 35 sz[x]=sz[ls]+sz[rs]+1; sm[x]=sm[ls]+sm[rs]+v[x]; 36 mx[x]=max(max(mx[ls],mx[rs]),mxr[ls]+mxl[rs]+v[x]); 37 mxl[x]=max(mxl[ls],sm[ls]+mxl[rs]+v[x]); 38 mxr[x]=max(mxr[rs],sm[rs]+mxr[ls]+v[x]); 39 } 40 41 int build(int l,int r){ 42 int mid=(l+r)>>1; 43 int x=get(); v[x]=sm[x]=mx[x]=a[mid]; 44 mxl[x]=mxr[x]=(a[mid]>0)?a[mid]:0; 45 if (l<mid) f[ch[x][0]=build(l,mid-1)]=x; 46 if (mid<r) f[ch[x][1]=build(mid+1,r)]=x; 47 upd(x); return x; 48 } 49 50 void rot(int &rt,int x){ 51 int y=f[x],z=f[y],w=ch[y][1]==x; 52 if (y==rt) rt=x; else ch[z][ch[z][1]==y]=x; 53 f[x]=z; f[y]=x; f[ch[x][w^1]]=y; 54 ch[y][w]=ch[x][w^1]; ch[x][w^1]=y; upd(y); 55 } 56 57 void splay(int &rt,int x){ 58 while (x!=rt){ 59 int y=f[x],z=f[y]; 60 if (y!=rt){ if ((ch[z][0]==y)^(ch[y][0]==x)) rot(rt,x); else rot(rt,y); } 61 rot(rt,x); 62 } 63 upd(x); 64 } 65 66 int find(int x,int k){ 67 push(x); 68 if (sz[ch[x][0]]+1==k) return x; 69 if (k<=sz[ch[x][0]]) return find(ch[x][0],k); 70 else return find(ch[x][1],k-sz[ch[x][0]]-1); 71 } 72 73 void ins(int pos,int p){ 74 int x=find(rt,pos),y=find(rt,pos+1); 75 splay(rt,x); splay(ch[rt][1],y); 76 ch[y][0]=p; f[p]=y; upd(y); upd(x); 77 } 78 79 void era(int x){ 80 push(x); 81 if (ch[x][0]) era(ch[x][0]); 82 if (ch[x][1]) era(ch[x][1]); 83 thr(x); 84 } 85 86 int split(int x,int y){ 87 x=find(rt,x); y=find(rt,y); splay(rt,x); splay(ch[x][1],y); 88 push(ch[y][0]); return ch[y][0]; 89 } 90 91 void del(int x,int y){ 92 x=find(rt,x); y=find(rt,y); splay(rt,x); splay(ch[x][1],y); 93 era(ch[y][0]); ch[y][0]=0; upd(y); upd(x); 94 } 95 96 int main(){ 97 freopen("bzoj1500.in","r",stdin); 98 freopen("bzoj1500.out","w",stdout); 99 scanf("%d%d",&n,&m); mx[0]=-inf; 100 rep(i,2,n+1) scanf("%d",&a[i]); a[1]=-inf; a[n+2]=inf; 101 rt=build(1,n+2); 102 while (m--){ 103 scanf("%s",op); 104 if (op[0]=='I'){ 105 scanf("%d%d",&pos,&tot); rep(i,1,tot) scanf("%d",&a[i]); 106 int x=build(1,tot); ins(pos+1,x); 107 } 108 if (op[0]=='D') scanf("%d%d",&pos,&tot),del(pos,pos+tot+1); 109 if (op[0]=='M' && op[2]=='K') scanf("%d%d%d",&pos,&tot,&k),cov(split(pos,pos+tot+1),k); 110 if (op[0]=='R') scanf("%d%d",&pos,&tot),rev(split(pos,pos+tot+1)); 111 if (op[0]=='G') scanf("%d%d",&pos,&tot),printf("%d\n",sm[split(pos,pos+tot+1)]); 112 if (op[0]=='M' && op[2]=='X') printf("%d\n",mx[split(1,sz[rt])]); 113 } 114 return 0; 115 }
转载于:https://www.cnblogs.com/HocRiser/p/8547359.html
[BZOJ1500][NOI2005]维修数列(splay)相关推荐
- BZOJ1500 [NOI2005]维修数列(Splay tree)
[Submit][Status][Discuss] Description 请写一个程序,要求维护一个数列,支持以下 6 种操作: 请注意,格式栏 中的下划线' _ '表示实际输入文件中的空格 Inp ...
- 【BZOJ1500】[NOI2005]维修数列 Splay
[BZOJ1500][NOI2005]维修数列 Description Input 输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初始时数列中数的个数,M表示要进行的操作数目. 第2 ...
- bzoj1500: [NOI2005]维修数列
模板题 之前还写了点铺垫题 bzoj3223 教你如何reverse bzoj1507 教你如何维护序列 bzoj1269 教你如何维护序列&reverse 然后再做这个题就好多啦 #incl ...
- 1500: [NOI2005]维修数列 (Splay)
1W1A(inf开太大) #include<algorithm> #include<iostream> #include<cstring> #include< ...
- 数据结构之fhq-treap——Chef and Sets,[HNOI2012]永无乡,Play with Chain,[NOI2005]维修数列(结构体版代码)
因为非常板,所以主要是代码 Tyvj 1728 普通平衡树 Chef and Sets [HNOI2012]永无乡 Play with Chain [NOI2005]维修数列 题目很水,所以可能会出现 ...
- BZOJ 1500 Luogu P2042 [NOI2005] 维护数列 (Splay)
BZOJ 1500 Luogu P2042 [NOI2005] 维护数列 (Splay) 手动博客搬家: 本文发表于20180825 00:34:49, 原地址https://blog.csdn.ne ...
- [BZOJ 1500] [NOI2005] 维修数列
题目链接:BZOJ - 1500 题目分析 我要先说一下,这道题我写了一晚上,然后Debug了一整个白天..........再一次被自己的蒟蒻程度震惊= = 这道题是传说中的Splay维护数列的Bos ...
- 数据结构(Splay平衡树):COGS 339. [NOI2005] 维护数列
339. [NOI2005] 维护数列 时间限制:3 s 内存限制:256 MB [问题描述] 请写一个程序,要求维护一个数列,支持以下 6 种操作:(请注意,格式栏 中的下划线' _ '表示实际 ...
- [bzoj1500 维修数列](NOI2005) (splay)
真的是太弱了TAT...光是把代码码出来就花了3h..还调了快1h才弄完T_T 号称考你会不会splay(当然通过条件是1h内AC..吓傻)... 黄学长的题解:http://hzwer.com/28 ...
最新文章
- 2021年大数据Spark(五):大环境搭建本地模式 Local
- VSCode使用笔记
- 超详干货!Linux 环境变量配置全攻略
- Centos7-Mysql 5.6 多主一从 解决方案详细配置
- Elasticsearch文档的CRUD
- 洛谷P1852:跳跳棋(LCA,树形结构)
- 报名通道开启 | 顶会 ICLR 2021:医疗对话生成与自动诊断国际竞赛,邀你来战!...
- JavaScript基本概念(下)
- python脚本自动统计excel_python脚本实现-excel二级统计
- 怎樣制作线段动画_OPPO又开发布会!这两个PPT动画太炫了,荣获网友清一色好评...
- ASP.NET 页面缓存 @ OutputCache
- 短视频网站源码干货分享:如何实现背景音乐和美颜功能
- html页面乱码解决
- import requests
- 前端框架 — Bootstrap
- 拼车网源码php,拼车网源码-thinkphp PHP微信拼车源码 环境要求: 服务器系统:服务器系统 推荐Linux系统 - 下载 - 搜珍网...
- vant list 分页问题
- 【Audio】声临其境——杜比音效介绍
- openwt dns 解析部分域名出错问题。
- Win10提示无法更新正在撤销安装怎么解决
热门文章
- 征战蓝桥 —— 2015年第六届 —— C/C++A组第10题——灾后重建
- Jeson nano + 思岚激光雷达rplidar_s1 + ubuntu18.04
- Python编码错误的解决办法SyntaxError: Non-ASCII character ‘\xe5‘ in file
- 【STM32】独立看门狗相关函数和类型
- mysql union 放弃索引_MySQL的or/in/union与索引优化
- python3 mysql报警日志_Python监听MySQL日志
- js reduce实现中间件_实现redux中间件-洋葱模型
- java toHalf_MoreThanHalfInArray.java
- 每天一道LeetCode-----摩尔投票法寻找给定数组中出现个数大于n/2或n/3的元素
- iOS Hacker LLDB 和 debugserver 实例调试