【bzoj4864】神秘物质
Description
给出一个长度为n的序列,第i个数为ai,进行以下四种操作:
merge x e:将当前第x个数和第x+1个数合并,得到一个新的数e;
insert x e:在当前第x个数和第x+1个数之间插入一个新的数e;
max x y:求当前第x个数到第y个数之间任意子区间中区间极差的最大值;
min x y:求当前第x个数到第y个数之间任意子区间中区间极差的最小值
(区间极差:区间内最大值与最小值之差)
(子区间长度至少为2)
Solution
splay模板题。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,a[];
namespace splay{
#define lson(x) t[x].lc
#define rson(x) t[x].rc
#define fa(x) t[x].fa
struct tree{
int lc,rc,fa;
int size,val,maxn,minn;
int delta,ans;
}t[]={};
int root=,cnt=;
void pushup(int x){
t[x].size=t[lson(x)].size+t[rson(x)].size+;
t[x].maxn=max(t[x].val,max(t[lson(x)].maxn,t[rson(x)].maxn));
t[x].minn=t[x].val;
if(lson(x))
t[x].minn=min(t[x].minn,t[lson(x)].minn);
if(rson(x))
t[x].minn=min(t[x].minn,t[rson(x)].minn);
t[x].ans=t[x].delta;
if(lson(x))
t[x].ans=min(t[x].ans,t[lson(x)].ans);
if(rson(x))
t[x].ans=min(t[x].ans,t[rson(x)].ans);
return;
}
int build(int l,int r){
int mid=(l+r)>>,s=++cnt;
t[s].val=a[mid];
t[s].delta=(mid)?abs(a[mid]-a[mid-]):0x3f3f3f3f;
if(l<mid){
lson(s)=build(l,mid-);
fa(lson(s))=s;
}
if(mid<r){
rson(s)=build(mid+,r);
fa(rson(s))=s;
}
pushup(s);
return s;
}
int find(int k){
int p=root;
while(true){
if(t[lson(p)].size>=k)
p=lson(p);
else if(t[lson(p)].size+==k)
return p;
else{
k-=t[lson(p)].size+;
p=rson(p);
}
}
}
void rotate(int x){
int fa=fa(x);
if(fa==root)
root=x;
if(x==lson(fa)){
lson(fa)=rson(x);
fa(rson(x))=fa;
rson(x)=fa;
fa(x)=fa(fa);
if(root!=x){
if(fa==lson(fa(fa)))
lson(fa(fa))=x;
else
rson(fa(fa))=x;
}
fa(fa)=x;
}
else{
rson(fa)=lson(x);
fa(lson(x))=fa;
lson(x)=fa;
fa(x)=fa(fa);
if(root!=x){
if(fa==lson(fa(fa)))
lson(fa(fa))=x;
else
rson(fa(fa))=x;
}
fa(fa)=x;
}
pushup(fa);
pushup(x);
return;
}
void splay(int x,int y){
while(fa(x)!=y){
if(fa(fa(x))==y){
rotate(x);
return;
}
int a=(x==lson(fa(x)))?:-;
int b=(fa(x)==lson(fa(fa(x))))?:-;
if(a*b==){
rotate(fa(x));
rotate(x);
}
else{
rotate(x);
rotate(x);
}
}
return;
}
void insert(int x,int y){
splay(find(x),);
splay(find(x+),root);
lson(rson(root))=++cnt;
fa(cnt)=rson(root);
t[cnt].size=;
t[cnt].val=t[cnt].maxn=t[cnt].minn=y;
t[cnt].delta=t[cnt].ans=abs(t[root].val-y);
t[rson(root)].delta=abs(t[rson(root)].val-y);
pushup(rson(root));
pushup(root);
return;
}
void del(int x){
splay(find(x-),);
splay(find(x+),root);
lson(rson(root))=;
t[rson(root)].delta=abs(t[root].val-t[rson(root)].val);
pushup(rson(root));
pushup(root);
}
int querymaxn(int l,int r){
splay(find(l-),);
splay(find(r+),root);
return t[lson(rson(root))].maxn-t[lson(rson(root))].minn;
}
int queryminn(int l,int r){
splay(find(l-),);
splay(find(r+),root);
return t[lson(rson(root))].ans;
}
#undef lson
#undef rson
#undef fa
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%d",a+i);
a[]=0x3f3f3f3f;
a[n+]=-0x3f3f3f3f;
splay::root=splay::build(,n+);
scanf("%d",&m);
while(m--){
char op[];int x,y;
scanf("%s%d%d",op,&x,&y);
if(op[]=='i')
splay::insert(x+,y);
else if(op[]=='e'){
splay::del(x+);
splay::del(x+);
splay::insert(x,y);
}
else if(op[]=='a')
printf("%d\n",splay::querymaxn(x+,y+));
else
printf("%d\n",splay::queryminn(x+,y+));
}
return ;
}
【bzoj4864】神秘物质的更多相关文章
- 【BZOJ4864】[BeiJing 2017 Wc]神秘物质 Splay
[BZOJ4864][BeiJing 2017 Wc]神秘物质 Description 21ZZ 年,冬. 小诚退休以后, 不知为何重新燃起了对物理学的兴趣. 他从研究所借了些实验仪器,整天研究各种微 ...
- [bzoj4864][BeiJing2017Wc]神秘物质_非旋转Treap
神秘物质 bzoj-4864 BeiJing-2017-Wc 题目大意:给定一个长度为n的序列,支持插入,将相邻两个元素合并并在该位置生成一个指定权值的元素:查询:区间内的任意一段子区间的最大值减最小 ...
- 【BZOJ4864】神秘物质 [Splay]
神秘物质 Time Limit: 10 Sec Memory Limit: 256 MB Description Input Output Sample Input Sample Output 1 ...
- BZOJ_4864_[BeiJing 2017 Wc]神秘物质_Splay
BZOJ4864_[BeiJing 2017 Wc]神秘物质_Splay Description 21ZZ 年,冬. 小诚退休以后, 不知为何重新燃起了对物理学的兴趣. 他从研究所借了些实验仪器,整天 ...
- BZOJ 4864: [BeiJing 2017 Wc]神秘物质 解题报告
4864: [BeiJing 2017 Wc]神秘物质 Description 21ZZ 年,冬. 小诚退休以后, 不知为何重新燃起了对物理学的兴趣. 他从研究所借了些实验仪器,整天研究各种微观粒子. ...
- [BZOJ4864][BeiJing2017Wc]神秘物质(splay)
首先merge就是先delete两次再insert,Max就是整个区间的最大值减最小值,Min就是区间中所有相邻两数差的最小值. Splay支持区间最大值,区间最小值,区间相邻差最小值即可. #inc ...
- BZOJ4864 BeiJing 2017 Wc神秘物质(splay)
splay维护区间最大值.最小值.相邻两数差的绝对值的最小值即可. #include<iostream> #include<cstdio> #include<cmath& ...
- [bzoj4864][BeiJing 2017 Wc]神秘物质
来自FallDream的博客,未经允许,请勿转载,谢谢. 21ZZ 年,冬. 小诚退休以后, 不知为何重新燃起了对物理学的兴趣. 他从研究所借了些实验仪器,整天研究各种微观粒子.这 一天, 小诚刚从研 ...
- BZOJ4864[BeiJing 2017 Wc]神秘物质——非旋转treap
题目描述 21ZZ 年,冬. 小诚退休以后, 不知为何重新燃起了对物理学的兴趣. 他从研究所借了些实验仪器,整天研究各种微观粒子.这 一天, 小诚刚从研究所得到了一块奇异的陨石样本, 便迫不及待地开始 ...
- 【BZOJ4864】【BJWC2017】神秘物质 - Splay
题意: Description 21ZZ 年,冬.小诚退休以后, 不知为何重新燃起了对物理学的兴趣. 他从研究所借了些实验仪器,整天研究各种微观粒子.这一天, 小诚刚从研究所得到了一块奇异的陨石样本, ...
随机推荐
- core组件进阶
访问图像像素 存储方式 BGR连续存储有助于提升图像扫描速度. isContinuous()判断是否是连续存储. 颜色空间缩减 仅用这些颜色中具有代表性的很小的部分,就足以达到同样的效果. 将现有颜色 ...
- COGS——T 1215. [Tyvj Aug11] 冗余电网
http://www.cogs.pro/cogs/problem/problem.php?pid=1215 ★ 输入文件:ugrid.in 输出文件:ugrid.out 简单对比时间限制: ...
- ArcGIS api for javascript——查找任务-在地图上查找要素
描述 本例展示了如何使用查找任务搜索数据.本例在地图上用图表显示结果并用DojoX的grid格式化结果为表格样式. FindTask构造函数需要一个ArcGIS Server地图服务的URL.本例使用 ...
- [PostCss] Easily Load Google Fonts with PostCSS Font Magician
Configuring Google Fonts can be quite an annoying process to setup. Using Font Magician with PostCSS ...
- thinkphp路由的作用
thinkphp路由的作用 问题 请问一下什么是thinkPHP路由,路由有什么作用?谢谢 解答 网络访问地址从来都是映射访问的,最初是这样,主机名(电脑名称)=>ip地址(如局域网192.16 ...
- Centos7 zabbix3.4.6的安装部署 (二)
接着安装zabbix客户端 直接安装在服务器上 监控服务器 ip 192.168.161.25 yum -y install zabbix-agent #通过Yum安装zabbix客户端 接着配置za ...
- shrio 授权
授权,也叫访问控制,即在应用中控制谁能访问哪些资源(如访问页面/编辑数据/页面操作等).在授权中需了解的几个关键对象:主体(Subject).资源(Resource).权限(Permission).角 ...
- 21.MFC进制转换工具
相关代码:链接:https://pan.baidu.com/s/1pKVVUZL 密码:e3vf #include <stdlib.h> #include <stdio.h> ...
- 关于HTML5和CSS3的几个“新增”
html5和css3分别是目前最新的web前端编程的标准,加入了新的标准和要求. 1.HTML5新增input输入类型,即type后面的值 文本域 <input type="text& ...
- Python(四) 分支、循环、条件与枚举
一.什么是表达式 表达式(Expression)是运算符(operator)和操作数(operand)所构成的序列 二.表达式的优先级 三.表达式优先级练习 优先级同级 从左往右计算 1 or 2 a ...