BZOJ_4864_[BeiJing 2017 Wc]神秘物质_Splay
BZOJ4864_[BeiJing 2017 Wc]神秘物质_Splay
Description
Input
Output
Sample Input
5 8 10 2
max 1 3
min 1 3
max 2 4
Sample Output
- #include <stdio.h>
- #include <string.h>
- #include <algorithm>
- using namespace std;
- #define N 200050
- #define ls ch[p][0]
- #define rs ch[p][1]
- #define get(x) (ch[f[x]][1]==x)
- int ch[N][2],f[N],siz[N],mn[N],mx[N],rt,sz,mxrange[N],mnrange[N],val[N],tot;
- int n,m,a[N],head[N],tail[N];
- char opt[10];
- int fabs(int x){return x>0?x:-x;}
- void pushup(int p) {
- siz[p]=siz[ls]+siz[rs]+1;
- mx[p]=max(val[p],max(mx[ls],mx[rs]));
- mn[p]=min(val[p],min(mn[ls],mn[rs]));
- head[p]=ls?head[ls]:p;
- tail[p]=rs?tail[rs]:p;
- mnrange[p]=min(mnrange[ls],mnrange[rs]);
- if(ls) mnrange[p]=min(mnrange[p],fabs(val[p]-val[tail[ls]]));
- if(rs) mnrange[p]=min(mnrange[p],fabs(val[p]-val[head[rs]]));
- }
- void rotate(int x) {
- int y=f[x],z=f[y],k=get(x);
- ch[y][k]=ch[x][!k]; f[ch[y][k]]=y;
- ch[x][!k]=y; f[y]=x; f[x]=z;
- if(z) ch[z][ch[z][1]==y]=x;
- pushup(y); pushup(x);
- if(rt==y) rt=x;
- }
- void splay(int x,int y) {
- for(int fa;(fa=f[x])!=y;rotate(x))
- if(f[fa]!=y)
- rotate(get(fa)==get(x)?fa:x);
- }
- int find(int x) {
- int p=rt;
- while(1) {
- if(x<=siz[ls]) p=ls;
- else {
- x-=siz[ls]+1;
- if(!x) return p;
- p=rs;
- }
- }
- }
- void build(int fa,int l,int r) {
- if(l>r) return ;
- int mid=(l+r)>>1;
- ch[fa][mid>fa]=mid;
- f[mid]=fa;
- val[mid]=a[mid-1];
- build(mid,l,mid-1);
- build(mid,mid+1,r);
- pushup(mid);
- }
- void print() {
- int i;
- printf("tot=%d\n",tot);
- for(i=1;i<=tot;i++) {
- int p=find(i);
- printf("p=%d,val[p]=%d,siz[p]=%d\n",p,val[p],siz[p]);
- }
- }
- int main() {
- scanf("%d%d",&n,&m);
- int i,x,y,p;
- mx[0]=0; mn[0]=mnrange[0]=0x3fffffff;
- for(i=1;i<=n;i++) scanf("%d",&a[i]);
- build(0,1,n+2);
- rt=(n+3)>>1; sz=n+2; tot=n+2;
- while(m--) {
- scanf("%s%d%d",opt,&x,&y);
- //print();
- if(opt[1]=='e') {
- tot--;
- p=x+3; x=find(x); p=find(p); splay(x,0); splay(p,x);
- ch[ls][0]=ch[ls][1]=0; val[ls]=y;
- pushup(ls); pushup(p); pushup(x);
- }else if(opt[0]=='i') {
- tot++;
- x++; p=x+1; x=find(x); p=find(p); splay(x,0); splay(p,x);
- sz++; ls=sz; f[sz]=p; val[sz]=y;
- pushup(sz); pushup(p); pushup(x);
- }else if(opt[1]=='a') {
- x=find(x); p=find(y+2); splay(x,0); splay(p,x);
- printf("%d\n",mx[ls]-mn[ls]);
- }else {
- x=find(x); p=find(y+2); splay(x,0); splay(p,x);
- printf("%d\n",mnrange[ls]);
- }
- }
- }
BZOJ_4864_[BeiJing 2017 Wc]神秘物质_Splay的更多相关文章
- BZOJ 4864: [BeiJing 2017 Wc]神秘物质 解题报告
4864: [BeiJing 2017 Wc]神秘物质 Description 21ZZ 年,冬. 小诚退休以后, 不知为何重新燃起了对物理学的兴趣. 他从研究所借了些实验仪器,整天研究各种微观粒子. ...
- 【BZOJ4864】[BeiJing 2017 Wc]神秘物质 Splay
[BZOJ4864][BeiJing 2017 Wc]神秘物质 Description 21ZZ 年,冬. 小诚退休以后, 不知为何重新燃起了对物理学的兴趣. 他从研究所借了些实验仪器,整天研究各种微 ...
- [bzoj4864][BeiJing 2017 Wc]神秘物质
来自FallDream的博客,未经允许,请勿转载,谢谢. 21ZZ 年,冬. 小诚退休以后, 不知为何重新燃起了对物理学的兴趣. 他从研究所借了些实验仪器,整天研究各种微观粒子.这 一天, 小诚刚从研 ...
- BZOJ4864[BeiJing 2017 Wc]神秘物质——非旋转treap
题目描述 21ZZ 年,冬. 小诚退休以后, 不知为何重新燃起了对物理学的兴趣. 他从研究所借了些实验仪器,整天研究各种微观粒子.这 一天, 小诚刚从研究所得到了一块奇异的陨石样本, 便迫不及待地开始 ...
- BZOJ4864 BeiJing 2017 Wc神秘物质(splay)
splay维护区间最大值.最小值.相邻两数差的绝对值的最小值即可. #include<iostream> #include<cstdio> #include<cmath& ...
- BZOJ4864: [BeiJing 2017 Wc]神秘物质(Splay)
Description 21ZZ 年,冬. 小诚退休以后, 不知为何重新燃起了对物理学的兴趣. 他从研究所借了些实验仪器,整天研究各种微观粒子.这 一天, 小诚刚从研究所得到了一块奇异的陨石样本, 便 ...
- BZOJ 4864: [BeiJing 2017 Wc]神秘物质 (块状链表/平衡树 )
这就是一道数据结构裸题啊,最大极差就是区间最大值减最小值,最小极差就是相邻两个数差的最小值.然后平衡树splay/treap或者块状链表维护就行了. 第一次自己写块状链表,蛮好写,就是长..然后就BZ ...
- #4864. [BeiJing 2017 Wc]神秘物质 [FHQ Treap]
这题其实挺简单的,有个东西可能稍微难维护了一点点.. \(merge\ x\ e\) 当前第 \(x\) 个原子和第 \(x+1\) 个原子合并,得到能量为 \(e\) 的新原子: \(insert\ ...
- 【BZOJ4864】神秘物质 [Splay]
神秘物质 Time Limit: 10 Sec Memory Limit: 256 MB Description Input Output Sample Input Sample Output 1 ...
随机推荐
- 关于在vim中的查找和替换
1,查找 在normal模式下按下/即可进入查找模式,输入要查找的字符串并按下回车. Vim会跳转到第一个匹配.按下n查找下一个,按下N查找上一个. Vim查找支持正则表达式,例如/vim$匹配行尾的 ...
- 收藏 - android
收藏 - android开发 2018-05-04 16:39:36 介绍:这篇文章是收藏系列的开山第一篇,主要收藏了跟android开发有关的一些内容,也算是内容汇总,后期会持续更新: 内容目录 1 ...
- JVM如何理解Java泛型类
//泛型代码 public class Pair<T>{ private T first=null; private T second=null; public Pair(T fir,T ...
- JavaScript中将对象数组中的某个属性值,批量替换成另一个数值
原文链接 https://segmentfault.com/q/1010000010352622 希望将下列数组中的sh替换成沪,sz替换成深 var stooges = [ {label:1,val ...
- 发现DELL笔记本一个很弱智的问题
以前用联想的笔记本,最近联想笔记本坏了,用的是公司的DELL笔记本,发现DELL笔记本一个很弱智的问题. 关于禁用触摸板的问题. 起因: 由于要经常写程序,我配置的有有线鼠标,但是打字时经常碰到触摸板 ...
- vfd电子时钟制作
17年也没干个啥,年后就去折腾着玩意儿了,也不知道我折腾它还是它折腾我.反正总之现在勉强可以交作业了,呵呵 硬件: 1.罗耶振荡电路输出一路4v交流,一路25v交流 其中4v直接驱动灯丝,另一路经电桥 ...
- Spring框架碰壁日常更新
关于Spring环境搭建遇到的问题:
- mysql 关于表与字段的增删改查操作
1.mysql 命令登陆 形式: mysql -u用户名 -p密码 mysql -uroot -proot 2.mysql 显示数据库 形式: show databases; 3.mysql 进入某一 ...
- flex与js通信、在浏览器中打开新窗口
一.flex与js通信(通过flex调用js方法) var urlR:URLRequest = new URLRequest("javascript:test('from flex')&qu ...
- builder设计模式(摘录ITeye文章lintomny)
对于Builder模式很简单,但是一直想不明白为什么要这么设计,为什么要向builder要Product而不是向知道建造过程的Director要.刚才google到一篇文章,总算清楚了.在这里转贴一下 ...