BZOJ 4864: [BeiJing 2017 Wc]神秘物质 (块状链表/平衡树 )
这就是一道数据结构裸题啊,最大极差就是区间最大值减最小值,最小极差就是相邻两个数差的最小值。然后平衡树splay/treap或者块状链表维护就行了。
第一次自己写块状链表,蛮好写,就是长。。然后就BZOJ rank1了(2019.5.11求不打脸 )
CODE
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
char cb[1<<15],*cs=cb,*ct=cb;
#define getc() (cs==ct&&(ct=(cs=cb)+fread(cb,1,1<<15,stdin),cs==ct)?0:*cs++)
template<class T>inline void read(T &res) {
char ch; for(;!isdigit(ch=getc()););
for(res=ch-'0';isdigit(ch=getc());res=res*10+ch-'0');
}
const int Bsz = 500;
const int Cnt = 500;
const int inf = 0x3f3f3f3f;
stack<int>bin;
int n, q, num[MAXN], m;
struct Block{
int a[Bsz+5], use, nxt;
int mx, mn, d;
Block() {
memset(a, 0, sizeof a);
use = nxt = 0;
mx = 0, mn = d = inf;
}
inline void Clear() {
memset(a, 0, sizeof a);
use = nxt = 0;
mx = 0, mn = d = inf;
}
inline void getmax() {
mx = 0, mn = d = inf;
for(int i = 1; i <= use; ++i) {
mx = max(mx, a[i]),
mn = min(mn, a[i]);
if(i > 1) d = min(d, abs(a[i]-a[i-1]));
}
}
}b[Cnt+5];
inline int Query1(int x, int y) {
int l = 1;
while(x > b[l].use) x -= b[l].use, y -= b[l].use, l = b[l].nxt;
int r = l;
while(y > b[r].use) y -= b[r].use, r = b[r].nxt;
int Mx = 0, Mn = inf;
if(l == r) {
for(int i = x; i <= y; ++i)
Mx = max(Mx, b[l].a[i]),
Mn = min(Mn, b[l].a[i]);
return Mx-Mn;
}
for(int i = x; i <= b[l].use; ++i)
Mx = max(Mx, b[l].a[i]),
Mn = min(Mn, b[l].a[i]);
for(int i = 1; i <= y; ++i)
Mx = max(Mx, b[r].a[i]),
Mn = min(Mn, b[r].a[i]);
for(int i = b[l].nxt; i != r; i = b[i].nxt)
Mx = max(Mx, b[i].mx),
Mn = min(Mn, b[i].mn);
return Mx-Mn;
}
inline int Query2(int x, int y) {
int l = 1;
while(x > b[l].use) x -= b[l].use, y -= b[l].use, l = b[l].nxt;
int r = l;
while(y > b[r].use) y -= b[r].use, r = b[r].nxt;
int re = inf;
if(l == r) {
for(int i = x; i < y; ++i)
re = min(re, abs(b[l].a[i]-b[l].a[i+1]));
return re;
}
for(int i = x; i < b[l].use; ++i)
re = min(re, abs(b[l].a[i]-b[l].a[i+1]));
for(int i = 1; i < y; ++i)
re = min(re, abs(b[r].a[i]-b[r].a[i+1]));
for(int i = b[l].nxt; i != r; i = b[i].nxt)
re = min(re, b[i].d);
for(int i = l; i != r; i = b[i].nxt)
re = min(re, abs(b[i].a[b[i].use]-b[b[i].nxt].a[1]));
return re;
}
inline int Newnode() {
int re;
if(!bin.empty()) re = bin.top(), bin.pop();
else re = ++m;
b[re].Clear();
return re;
}
inline void Merge(int x, int val) {
int l = 1, pre = 0;
while(x > b[l].use) x -= b[l].use, pre = l, l = b[l].nxt;
if(x == b[l].use) {
int tmp = b[l].nxt;
b[tmp].a[1] = val;
b[tmp].getmax();
if(--b[l].use) b[l].getmax();
else b[pre].nxt = b[l].nxt, bin.push(l);
}
else {
b[l].a[x+1] = val;
--b[l].use;
for(int i = x; i <= b[l].use; ++i)
b[l].a[i] = b[l].a[i+1];
b[l].getmax();
}
}
inline void Insert(int x, int val) {
int l = 1, pre = 0;
while(x > b[l].use) x -= b[l].use, pre = l, l = b[l].nxt;
++b[l].use;
for(int i = b[l].use; i > x+1; --i) b[l].a[i] = b[l].a[i-1];
b[l].a[x+1] = val;
if(b[l].use > Bsz) {
int tmp = Newnode();
b[tmp].nxt = b[l].nxt; b[l].nxt = tmp;
b[tmp].use = b[l].use/2;
b[l].use -= b[tmp].use;
for(int i = 1; i <= b[tmp].use; ++i)
b[tmp].a[i] = b[l].a[b[l].use+i];
b[l].getmax(), b[tmp].getmax();
}
else b[l].getmax();
}
int main () {
read(n), read(q);
for(int i = 1; i <= n; ++i)
read(num[i]);
for(int i = 1; i*Bsz <= n; ++i) {
b[++m].use = Bsz; b[m-1].nxt = m;
for(int j = 1; j <= b[m].use; ++j)
b[m].a[j] = num[(m-1)*Bsz + j];
b[m].getmax();
}
if(n%Bsz) {
b[++m].use = n%Bsz; b[m-1].nxt = m;
for(int j = 1; j <= b[m].use; ++j)
b[m].a[j] = num[(m-1)*Bsz + j];
b[m].getmax();
}
char ch;
int x, y;
while(q--) {
while(!isalpha(ch=getc()));
ch=getc();
read(x), read(y);
switch(ch) {
case 'a':
printf("%d\n", Query1(x, y));
break;
case 'i':
printf("%d\n", Query2(x, y));
break;
case 'n':
Insert(x, y);
break;
case 'e':
Merge(x, y);
break;
}
}
}
BZOJ 4864: [BeiJing 2017 Wc]神秘物质 (块状链表/平衡树 )的更多相关文章
- BZOJ 4864: [BeiJing 2017 Wc]神秘物质 解题报告
4864: [BeiJing 2017 Wc]神秘物质 Description 21ZZ 年,冬. 小诚退休以后, 不知为何重新燃起了对物理学的兴趣. 他从研究所借了些实验仪器,整天研究各种微观粒子. ...
- #4864. [BeiJing 2017 Wc]神秘物质 [FHQ Treap]
这题其实挺简单的,有个东西可能稍微难维护了一点点.. \(merge\ x\ e\) 当前第 \(x\) 个原子和第 \(x+1\) 个原子合并,得到能量为 \(e\) 的新原子: \(insert\ ...
- BZOJ_4864_[BeiJing 2017 Wc]神秘物质_Splay
BZOJ4864_[BeiJing 2017 Wc]神秘物质_Splay 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 1507】【NOI 2003】&【Tyvj P2388】Editor 块状链表模板题
2016-06-18 当时关于块状链表的想法是错误的,之前维护的是一个动态的$\sqrt{n}$,所以常数巨大,今天才知道原因TwT,请不要参照这个程序为模板!!! 模板题水啊水~~~ 第一次写块状链 ...
随机推荐
- java积累的细节问题
一.将几个数字进行拼接,拼接成字符串 一定要将""放到几个数字之前,否则就会把数字相加之后的数转化成字符串 如你需要拼接年份,月份,和日 如果:2018+12+13+"& ...
- SrpingBoot入门到入坟02-HelloWorld的细节和初始自动配置
关于SpringBoot的第一个HelloWorld的一些细节: 1.父项目 首先查看项目中的pom.xml文件 文件中有个父项目,点进去则: 它里面也有一个父项目,再点进去: 可以发现有很多的依赖版 ...
- lubuntu踩坑全记录
为了降低系统占用,毕业之后一直用lubuntu不用ubuntu...操作其实差不多,就是lubuntu有一些小坑坑:P 本文是我的踩坑全记录.长期更新. 调分辨率 升级命令lubuntu不出登录页面 ...
- opencv实现人脸识别(一)opencv的相关知识了解
这回进行了人脸识别的项目,对学习过程进行记录. 首先进行的就是一系列环境的配置,如 python3.7的安装, python的IDE pycharm的安装,然后进行opencv库的安装,可以通过py ...
- Django中的Object Relational Mapping(ORM)
ORM 介绍 ORM 概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术. 简单的说,ORM是通过使用 ...
- electron客户端开发
如何新建一个 Electron 项目? electron快速入门笔记: https://www.jianshu.com/p/f134878af30f 然后自己新建一个 Electron 项目,在项目中 ...
- javascript 正则表达式的简单操作
前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! RegExp 正则表达式是描述字符模式的对象. 正则表达式用于对字符串模式匹配及检索替换,是对字符串执行模 ...
- 利用Filter和HttpServletRequestWrapper实现请求体中token校验
先说一下项目的背景,系统传参为json格式,token为其中一个必传参数,此时如果在过滤器中直接读取request,则后续controller中通过RequestBody注解封装请求参数是会报stre ...
- ES6 Proise 简单理解
Promise 这是ES6中增加的一个处理异步的对象. 传统变成写异步函数的时候,经常会遇到回调套回调: Promise 是异步编程的一种解决方案,比传统的解决方案 -----回调函数和事件----- ...
- vue的$nextTick使用后的js代码执行顺序问题
一.问题产生背景: 父组件已经获得子组件实例,并能直接触发子组件的方法,在父组件中调用了子组件的两个方法 // 父组件调用子组件,this.picker是获取的子组件整个实例,先调用update,再调 ...