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】神秘物质的更多相关文章

  1. 【BZOJ4864】[BeiJing 2017 Wc]神秘物质 Splay

    [BZOJ4864][BeiJing 2017 Wc]神秘物质 Description 21ZZ 年,冬. 小诚退休以后, 不知为何重新燃起了对物理学的兴趣. 他从研究所借了些实验仪器,整天研究各种微 ...

  2. [bzoj4864][BeiJing2017Wc]神秘物质_非旋转Treap

    神秘物质 bzoj-4864 BeiJing-2017-Wc 题目大意:给定一个长度为n的序列,支持插入,将相邻两个元素合并并在该位置生成一个指定权值的元素:查询:区间内的任意一段子区间的最大值减最小 ...

  3. 【BZOJ4864】神秘物质 [Splay]

    神秘物质 Time Limit: 10 Sec  Memory Limit: 256 MB Description Input Output Sample Input Sample Output 1 ...

  4. BZOJ_4864_[BeiJing 2017 Wc]神秘物质_Splay

    BZOJ4864_[BeiJing 2017 Wc]神秘物质_Splay Description 21ZZ 年,冬. 小诚退休以后, 不知为何重新燃起了对物理学的兴趣. 他从研究所借了些实验仪器,整天 ...

  5. BZOJ 4864: [BeiJing 2017 Wc]神秘物质 解题报告

    4864: [BeiJing 2017 Wc]神秘物质 Description 21ZZ 年,冬. 小诚退休以后, 不知为何重新燃起了对物理学的兴趣. 他从研究所借了些实验仪器,整天研究各种微观粒子. ...

  6. [BZOJ4864][BeiJing2017Wc]神秘物质(splay)

    首先merge就是先delete两次再insert,Max就是整个区间的最大值减最小值,Min就是区间中所有相邻两数差的最小值. Splay支持区间最大值,区间最小值,区间相邻差最小值即可. #inc ...

  7. BZOJ4864 BeiJing 2017 Wc神秘物质(splay)

    splay维护区间最大值.最小值.相邻两数差的绝对值的最小值即可. #include<iostream> #include<cstdio> #include<cmath& ...

  8. [bzoj4864][BeiJing 2017 Wc]神秘物质

    来自FallDream的博客,未经允许,请勿转载,谢谢. 21ZZ 年,冬. 小诚退休以后, 不知为何重新燃起了对物理学的兴趣. 他从研究所借了些实验仪器,整天研究各种微观粒子.这 一天, 小诚刚从研 ...

  9. BZOJ4864[BeiJing 2017 Wc]神秘物质——非旋转treap

    题目描述 21ZZ 年,冬. 小诚退休以后, 不知为何重新燃起了对物理学的兴趣. 他从研究所借了些实验仪器,整天研究各种微观粒子.这 一天, 小诚刚从研究所得到了一块奇异的陨石样本, 便迫不及待地开始 ...

  10. 【BZOJ4864】【BJWC2017】神秘物质 - Splay

    题意: Description 21ZZ 年,冬.小诚退休以后, 不知为何重新燃起了对物理学的兴趣. 他从研究所借了些实验仪器,整天研究各种微观粒子.这一天, 小诚刚从研究所得到了一块奇异的陨石样本, ...

随机推荐

  1. Benelux Algorithm Programming Contest 2014 Final(第二场)

    B:Button Bashing You recently acquired a new microwave, and noticed that it provides a large number ...

  2. Busy waiting

    In computer systems organization or operating systems, "busy waiting" refers to a process ...

  3. Python解析Socket数据流异常bytes问题

    Python解析Socket数据流异常bytes问题 -- 2019-03-12 python在通过socket发送数据时,英文字符转义后为原来本身的字符,占一个字节(如:s转移后为s),而中文字符在 ...

  4. Python实现快排

    Python实现快排 def quicksort(arr): if len(arr) <= 1: return arr pivot = arr[len(arr) // 2] left = [x ...

  5. 三种记录 MySQL 所执行过的 SQL 语句的方法

    程式 Debug 有時後從前面第一行追起來很辛苦(程式碼太多或 compile 過), 另一種做法就是從後面追起來, 反正最後寫入的是 DB, 那就從 DB 開始往前推, 所以就是要抓程式是執行哪些 ...

  6. ArcGIS api for javascript——设置可见的比例

    描述 ArcGIS JavaScript API的1.2版本加入更多地图如何缓存,切片和地图服务的控制.本例展示了如何限制地图能够接收切片的比例等级.如果不想用户过分的放大或缩小,即使切片存在这些极端 ...

  7. SDSoC使用体验

    本文作者:卜居 转载请保留作者信息.原文网址(http://blog.csdn.net/kkk584520/article/details/47220575). 本文project可到我的资源下载(h ...

  8. 减少UIViewController切换的耦合

    我们一般切换UIViewController的时候用的是例如以下代码 #import "UIViewControllerDemo.h" UIViewControllerDemo * ...

  9. 异步FIFO及verilog原码

    这几天看了Clifford E. Cummings的两篇大作<Simulation and Synthesis Techniques for Asynchronous FIFO Design&g ...

  10. 2.最详细的WSDD配置文件注释

    https://blog.csdn.net/u011063151/article/details/52590282