NOI2005 维护数列(splay)
学了半天平衡树,选择了一道题来写一写,发现题目是裸的splay模板,但是还是写不好,这个的精髓之处在于在数列的某一个位置加入一个数列,类似于treap里面的merge,然后还学到了题解里面的的回收空间做法,利用时间换取空间也是一种非常不错的做法,由于题目中有区间翻转的操作,所以只能用splay来写,然后对于一些区间最大值和区间最大子序列的询问我们可以用类似于线段树的合并操作来维护,总的来说这是一道对于树形数据结构非常好的运用的题目。——by VANE
- #include<bits/stdc++.h>
- using namespace std;
- const int inf=0x3f3f3f3f;
- const int N=1e6+;
- int n,m,rt,cnt;
- int a[N],id[N],fa[N],c[N][];
- int sum[N],sz[N],v[N],mx[N],lx[N],rx[N];
- bool tag[N],rev[N];
- queue<int> q;
- inline int read()
- {
- int x=,f=;char ch=getchar();
- while(ch<''||ch>'') {if(ch=='-') f=-f; ch=getchar();}
- while(ch>=''&&ch<='') {x=x*+ch-'';ch=getchar();}
- return x*f;
- }
- inline void pushup(int x)
- {
- int l=c[x][],r=c[x][];
- sum[x]=sum[l]+sum[r]+v[x];
- sz[x]=sz[l]+sz[r]+;
- mx[x]=max(mx[l],max(mx[r],rx[l]+v[x]+lx[r]));
- lx[x]=max(lx[l],sum[l]+v[x]+lx[r]);
- rx[x]=max(rx[r],sum[r]+v[x]+rx[l]);
- }
- inline void pushdown(int x)
- {
- int l=c[x][],r=c[x][];
- if(tag[x])
- {
- rev[x]=tag[x]=;
- if(l) tag[l]=,v[l]=v[x],sum[l]=v[x]*sz[l];
- if(r) tag[r]=,v[r]=v[x],sum[r]=v[x]*sz[r];
- if(v[x]>=)
- {
- if(l) lx[l]=rx[l]=mx[l]=sum[l];
- if(r) lx[r]=rx[r]=mx[r]=sum[r];
- }
- else
- {
- if(l) lx[l]=rx[l]=,mx[l]=v[x];
- if(r) lx[r]=rx[r]=,mx[r]=v[x];
- }
- }
- if(rev[x])
- {
- rev[x]=;rev[l]^=;rev[r]^=;
- swap(lx[l],rx[l]);swap(lx[r],rx[r]);
- swap(c[l][],c[l][]);swap(c[r][],c[r][]);
- }
- }
- inline void rotate(int x,int &k)
- {
- int y=fa[x],z=fa[y],l=(c[y][]==x),r=l^;
- if(y==k) k=x;else c[z][c[z][]==y]=x;
- fa[c[x][r]]=y;fa[y]=x;fa[x]=z;
- c[y][l]=c[x][r];c[x][r]=y;
- pushup(y);pushup(x);
- }
- inline void splay(int x,int &k)
- {
- while(x!=k)
- {
- int y=fa[x],z=fa[y];
- if(y!=k)
- {
- if(c[z][]==y ^ c[y][]==x) rotate(x,k);
- else rotate(y,k);
- }
- rotate(x,k);
- }
- }
- inline int find(int x,int rk)
- {
- pushdown(x);
- int l=c[x][],r=c[x][];
- if(sz[l]+==rk) return x;
- if(sz[l]>=rk) return find(l,rk);
- return find(r,rk-sz[l]-);
- }
- inline void recycle(int x)
- {
- int &l=c[x][],&r=c[x][];
- if(l) recycle(l);
- if(r) recycle(r);
- q.push(x);
- fa[x]=l=r=tag[x]=rev[x]=;
- }
- inline int split(int k,int tot)
- {
- int x=find(rt,k),y=find(rt,k+tot+);
- splay(x,rt);splay(y,c[x][]);
- return c[y][];
- }
- inline void query(int k,int tot)
- {
- int x=split(k,tot);
- printf("%d\n",sum[x]);
- }
- inline void modify(int k,int tot,int val)
- {
- int x=split(k,tot),y=fa[x];
- v[x]=val;tag[x]=;sum[x]=sz[x]*val;
- if(val>=) lx[x]=rx[x]=mx[x]=sum[x];
- else lx[x]=rx[x]=,mx[x]=val;
- pushup(y);pushup(fa[y]);
- }
- inline void rever (int k,int tot)
- {
- int x=split(k,tot),y=fa[x];
- if(!tag[x])
- {
- rev[x]^=;
- swap(c[x][],c[x][]);
- swap(lx[x],rx[x]);
- pushup(y);pushup(fa[y]);
- }
- }
- inline void erase(int k,int tot)
- {
- int x=split(k,tot),y=fa[x];
- recycle(x);c[y][]=;
- pushup(y);pushup(fa[y]);
- }
- inline void build(int l,int r,int f)
- {
- int mid=l+r>>,now=id[mid],pre=id[f];
- if(l==r)
- {
- mx[now]=sum[now]=a[l];
- lx[now]=rx[now]=max(a[l],);
- sz[now]=;
- }
- if(l<mid) build(l,mid-,mid);
- if(mid<r) build(mid+,r,mid);
- v[now]=a[mid];fa[now]=pre;
- pushup(now);
- c[pre][mid>=f]=now;
- }
- inline void insert(int k,int tot)
- {
- for(int i=;i<=tot;++i) a[i]=read();
- for(int i=;i<=tot;++i)
- {
- if(!q.empty()) id[i]=q.front(),q.pop();
- else id[i]=++cnt;
- }
- build(,tot,);
- int z=id[+tot>>];
- int x=find(rt,k+),y=find(rt,k+);
- splay(x,rt);splay(y,c[x][]);
- fa[z]=y;c[y][]=z;
- pushup(y);pushup(x);
- }
- int main()
- {
- n=read();m=read();
- mx[]=a[]=a[n+]=-inf;
- for(int i=;i<=n;++i) a[i+]=read();
- for(int i=;i<=n+;++i) id[i]=i;
- build(,n+,);
- rt=n+>>;cnt=n+;
- int k,tot,val;char ch[];
- while(m--)
- {
- scanf("%s",ch);
- if(ch[]!='M'||ch[]!='X') k=read(),tot=read();
- if(ch[]=='I') insert(k,tot);
- if(ch[]=='D') erase(k,tot);
- if(ch[]=='M')
- {
- if(ch[]=='X') printf("%d\n",mx[rt]);
- else val=read(),modify(k,tot,val);
- }
- if(ch[]=='R') rever(k,tot);
- if(ch[]=='G') query(k,tot);
- }
- }
NOI2005 维护数列(splay)的更多相关文章
- P2042 [NOI2005]维护数列 && Splay区间操作(四)
到这里 \(A\) 了这题, \(Splay\) 就能算入好门了吧. 今天是个特殊的日子, \(NOI\) 出成绩, 大佬 \(Cu\) 不敢相信这一切这么快, 一下子机房就只剩我和 \(zrs\) ...
- 洛谷 P2042 [NOI2005]维护数列-Splay(插入 删除 修改 翻转 求和 最大的子序列)
因为要讲座,随便写一下,等讲完有时间好好写一篇splay的博客. 先直接上题目然后贴代码,具体讲解都写代码里了. 参考的博客等的链接都贴代码里了,有空再好好写. P2042 [NOI2005]维护数列 ...
- P2042 [NOI2005]维护数列[splay或非旋treap·毒瘤题]
P2042 [NOI2005]维护数列 数列区间和,最大子列和(必须不为空),支持翻转.修改值.插入删除. 练码力的题,很毒瘤.个人因为太菜了,对splay极其生疏,犯了大量错误,在此记录,望以后一定 ...
- 洛谷.2042.[NOI2005]维护数列(Splay)
题目链接 2017.12.24 第一次写: 时间: 2316ms (1268ms) 空间: 19.42MB (19.5MB)(O2) 注:洛谷测的时间浮动比较大 /* 插入一段数:将这些数先单独建一棵 ...
- BZOJ 1500 Luogu P2042 [NOI2005] 维护数列 (Splay)
手动博客搬家: 本文发表于20180825 00:34:49, 原地址https://blog.csdn.net/suncongbo/article/details/82027387 题目链接: (l ...
- 数据结构(Splay平衡树):COGS 339. [NOI2005] 维护数列
339. [NOI2005] 维护数列 时间限制:3 s 内存限制:256 MB [问题描述] 请写一个程序,要求维护一个数列,支持以下 6 种操作:(请注意,格式栏 中的下划线‘ _ ’表示实际 ...
- [NOI2005]维护数列(区间splay)
[NOI2005]维护数列(luogu) 打这玩意儿真是要了我的老命 Description 请写一个程序,要求维护一个数列,支持以下 6 种操作:(请注意,格式栏 中的下划线‘ _ ’表示实际输入文 ...
- [NOI2005] 维护数列
[NOI2005] 维护数列 题目 传送门 请写一个程序,要求维护一个数列,支持以下 6 种操作:(请注意,格式栏 中的下划线‘ _ ’表示实际输入文件中的空格) 操作编号 输入文件中的格式 说明 1 ...
- Luogu P2042 [NOI2005]维护数列(平衡树)
P2042 [NOI2005]维护数列 题意 题目描述 请写一个程序,要求维护一个数列,支持以下\(6\)种操作:(请注意,格式栏中的下划线'_'表示实际输入文件中的空格) 输入输出格式 输入格式: ...
随机推荐
- 【BZOJ】2142 礼物
[算法]中国剩余定理 [题意]给定n件物品分给m个人,每人分到wi件,求方案数%p.p不一定是素数. [题解] 首先考虑n全排列然后按wi划分成m份,然后对于每份内都是全排列,除以wi!消除标号影响, ...
- MongoDB中的基础概念:Databases、Collections、Documents
MongoDB以BSON格式的文档(Documents)形式存储.Databases中包含集合(Collections),集合(Collections)中存储文档(Documents). BSON是一 ...
- F - Warm up HDU - 4612 tarjan缩点 + 树的直径 + 对tajan的再次理解
题目链接:https://vjudge.net/contest/67418#problem/F 题目大意:给你一个图,让你加一条边,使得原图中的桥尽可能的小.(谢谢梁学长的帮忙) 我对重边,tarja ...
- 深入理解Spring系列之一:开篇
转载 https://mp.weixin.qq.com/s?__biz=MzI0NjUxNTY5Nw==&mid=2247483810&idx=1&sn=a2df14fdb63 ...
- WPF之DataGrid--列的前台及后台实现
一.前台实现 在xaml里可以很轻松地实现一个如下图所示的DataGrid <StackPanel> <ComboBox Width="50" Horizonta ...
- ios 不支持iframe 解决方案
在iframe外层在包一层,通过appendChild()把内容增加到容器中,完整代码如下: @section Css { <link href="@ViewHelper.Conten ...
- centos 6.5配置ftp服务器,亲测可用
设置开机启动 1 chkconfig vsftpd on 启动服务 1 /sbin/service vsftpd start 配置FTP用户组/用户以及相应权限 添加用户组 1 groupadd ft ...
- angular项目中使用angular-material2
Step 1: Install Angular Material and Angular CDK npm install --save @angular/material @angular/cdk n ...
- tftp 开发板ping不通PC机
开发板:JZ2440(天下2440开发板是一家) 当进入uboot界面时:输入命令print则显示: 将PC端的IP设置为192.168.1.11 在开发板上ping 192.168.1.11,若 ...
- Python学习笔记——迭代器和生成器
1.手动遍历迭代器 使用next函数,并捕获StopIteration异常. def manual_iter(): with open('./test.py') as f: try: while Tr ...