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
解题思路:
Splay维护插入,删除,区间统计答案。
极差最大值为区间最值差,最小值为相邻元素之差。
然后愉快地AC
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#define lll tr[spc].ch[0]
#define rrr tr[spc].ch[1]
#define ls ch[0]
#define rs ch[1]
const int N=;
struct trnt{
int ch[];
int fa;
int E;
int wgt;
int maxval;
int minval;
int abst;
int minab;
}tr[N];
int siz;
int root;
int n,m;
int energy[N];
char cmd[];
bool whc(int spc)
{
return tr[tr[spc].fa].rs==spc;
}
void pushup(int spc)
{
tr[spc].wgt=;
tr[spc].maxval=tr[spc].minval=tr[spc].E;
tr[spc].minab=tr[spc].abst;
if(lll)
{
tr[spc].wgt+=tr[lll].wgt;
tr[spc].maxval=std::max(tr[spc].maxval,tr[lll].maxval);
tr[spc].minval=std::min(tr[spc].minval,tr[lll].minval);
tr[spc].minab=std::min(tr[spc].minab,tr[lll].minab);
}
if(rrr)
{
tr[spc].wgt+=tr[rrr].wgt;
tr[spc].maxval=std::max(tr[spc].maxval,tr[rrr].maxval);
tr[spc].minval=std::min(tr[spc].minval,tr[rrr].minval);
tr[spc].minab=std::min(tr[spc].minab,tr[rrr].minab);
}
return ;
}
void rotate(int spc)
{
int f=tr[spc].fa;
bool k=whc(spc);
tr[f].ch[k]=tr[spc].ch[!k];
tr[spc].ch[!k]=f;
tr[tr[f].fa].ch[whc(f)]=spc;
tr[spc].fa=tr[f].fa;
tr[f].fa=spc;
tr[tr[f].ch[k]].fa=f;
pushup(f);
pushup(spc);
return ;
}
void splay(int spc,int f)
{
while(tr[spc].fa!=f)
{
int ft=tr[spc].fa;
if(tr[ft].fa==f)
{
rotate(spc);
break;
}
if(whc(spc)^whc(ft))
rotate(spc);
else
rotate(ft);
rotate(spc);
}
if(!f)
root=spc;
return ;
}
void Build(int l,int r,int &spc,int f)
{
if(l>r)
return ;
int mid=(l+r)>>;
spc=++siz;
tr[spc].fa=f;
tr[spc].E=energy[mid];
tr[spc].abst=std::abs(energy[mid]-energy[mid+]);
Build(l,mid-,lll,spc);
Build(mid+,r,rrr,spc);
pushup(spc);
return ;
}
int plc(int spc,int v)
{
if(tr[lll].wgt>=v)
return plc(lll,v);
if(tr[lll].wgt+==v)
return spc;
return plc(rrr,v-tr[lll].wgt-);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%d",&energy[i]);
Build(,n+,root,);
while(m--)
{
int x,y;
scanf("%s",cmd+);
scanf("%d%d",&x,&y);
if(cmd[]=='i')
{
int spc1=plc(root,x+);
int spc2=plc(root,x+);
int spc=++siz;
splay(spc1,);
splay(spc2,root);
tr[spc1].abst=std::abs(tr[spc1].E-y);
tr[spc].abst=std::abs(tr[spc2].E-y);
tr[spc].fa=spc2;
tr[spc].E=y;
tr[spc2].ls=spc;
pushup(spc);
pushup(spc2);
pushup(root);
}else if(cmd[]=='e')
{
int spc1=plc(root,x),spc2=plc(root,x+);
splay(spc1,);
splay(spc2,root);
tr[spc1].abst=std::abs(y-tr[spc1].E);
int spc=++siz;
tr[spc].fa=tr[root].rs;
tr[spc].E=y;
tr[spc].abst=std::abs(y-tr[spc2].E);
tr[tr[root].rs].ls=spc;
pushup(spc);
pushup(spc2);
pushup(root);
}else if(cmd[]=='i')
{
int spc1=plc(root,x),spc2=plc(root,y+);
splay(spc1,);
splay(spc2,root);
int ans=tr[tr[tr[root].rs].ls].minab;
printf("%d\n",ans);
}else{
int spc1=plc(root,x),spc2=plc(root,y+);
splay(spc1,);
splay(spc2,root);
int ans=tr[tr[tr[root].rs].ls].maxval-tr[tr[tr[root].rs].ls].minval;
printf("%d\n",tr[tr[tr[root].rs].ls].maxval-tr[tr[tr[root].rs].ls].minval);
}
}
return ;
}
BZOJ4864: [BeiJing 2017 Wc]神秘物质(Splay)的更多相关文章
- 【BZOJ4864】[BeiJing 2017 Wc]神秘物质 Splay
[BZOJ4864][BeiJing 2017 Wc]神秘物质 Description 21ZZ 年,冬. 小诚退休以后, 不知为何重新燃起了对物理学的兴趣. 他从研究所借了些实验仪器,整天研究各种微 ...
- BZOJ4864 BeiJing 2017 Wc神秘物质(splay)
splay维护区间最大值.最小值.相邻两数差的绝对值的最小值即可. #include<iostream> #include<cstdio> #include<cmath& ...
- [bzoj4864][BeiJing 2017 Wc]神秘物质
来自FallDream的博客,未经允许,请勿转载,谢谢. 21ZZ 年,冬. 小诚退休以后, 不知为何重新燃起了对物理学的兴趣. 他从研究所借了些实验仪器,整天研究各种微观粒子.这 一天, 小诚刚从研 ...
- BZOJ4864[BeiJing 2017 Wc]神秘物质——非旋转treap
题目描述 21ZZ 年,冬. 小诚退休以后, 不知为何重新燃起了对物理学的兴趣. 他从研究所借了些实验仪器,整天研究各种微观粒子.这 一天, 小诚刚从研究所得到了一块奇异的陨石样本, 便迫不及待地开始 ...
- BZOJ_4864_[BeiJing 2017 Wc]神秘物质_Splay
BZOJ4864_[BeiJing 2017 Wc]神秘物质_Splay Description 21ZZ 年,冬. 小诚退休以后, 不知为何重新燃起了对物理学的兴趣. 他从研究所借了些实验仪器,整天 ...
- BZOJ 4864: [BeiJing 2017 Wc]神秘物质 解题报告
4864: [BeiJing 2017 Wc]神秘物质 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 ...
随机推荐
- js实现复选框的操作-------Day41
不知道之前的一篇为什么一直处于审核阶段.难道有哪个词语是敏感词被河蟹了? 无论了,又一次写了这篇,也算是加深记忆吧. 首先要写的是今天在进行表格数据操作时用到的对复选框checkbox的全选和全不选, ...
- 30.angularJS第一个实例
转自:https://www.cnblogs.com/best/tag/Angular/ AngularJS 通过 ng-directives 扩展了 HTML. ng-app 指令定义一个 Angu ...
- BZOJ 1503 treap
思路: treap (算是基本操作吧-..) 加减的操作数很少 就暴力好啦 每回判断一下最小的数是不是比M小 如果是 就删,继续判断 搞定. //By SiriusRen #include <c ...
- SQL函数_Floor和Celling
1 floor()函数用于获得小于或者等于数值表达式的最大整数,也就是向下取整;celling()函数用于获得大于或者等于数值表达式的最小整数,也就是向上取整: 2 floor()函数和round函数 ...
- Inter-process Communication (IPC)
For Developers > Design Documents > Inter-process Communication (IPC) 目录 1 Overview 1.1 I ...
- python code 1_username registration & login
This tiny program consists of 2 parts - registration and login. In the part of registration, the key ...
- 无法在WEB服务器上启动调试
错误:站点使用 IP 地址 Visual Studio 2012 调试器尝试自动附加到正在使用 IP 地址的 Web 应用程序时,会发生该错误. 如果在 IIS 中将“网站标识”更改为“使用特定 IP ...
- ASP.NET通过JavaScript实现Button 的Enabled=false
正常代码控制无法满足需求,所以记录一下方法: <input id="Button5" onclick=" CX()" type="button& ...
- C/C++获取本地时间常见方法
跨平台方法 方法一:手动暴力法 #include <iostream> using namespace std; #include <time.h> time_t t = ti ...
- [DLX反复覆盖] hdu 2828 Lamp
题意: 有N个灯M个开关 每一个灯的ON和OFF状态都能控制一个灯是否亮 给出N行,代表对于每一个灯 哪些开关的哪个状态能够使得第i个灯亮 思路: 这里须要注意一个问题 假设开关1的ON 状态和开关2 ...