您的位置:首页 > 其它

BZOJ 4864: [BeiJing 2017 Wc]神秘物质

2018-04-01 20:31 246 查看

Description

21ZZ 年,冬。

小诚退休以后, 不知为何重新燃起了对物理学的兴趣。 他从研究所借了些实验仪器,整天研究各种微观粒子。这

一天, 小诚刚从研究所得到了一块奇异的陨石样本, 便迫不及待地开始观测。 在精密仪器的视野下,构成陨石

的每个原子都无比清晰。 小诚发现, 这些原子排成若干列, 每一列的结构具有高度相似性。于是,他决定对单

独一列原子进行测量和测试。被选中的这列共有 N 个顺序排列的原子。 最初, 第 i 个原子具有能量 Ei。 随着

时间推移和人为测试, 这列原子在观测上会产生两种变化:

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 的子区间。

小诚坚信这项研究可以获得诺贝尔物理学奖。为了让小诚早日了结心愿,你能否帮助他实现上述的观测和测量呢?

Input

第一行, 两个整数 N, M, 分别表示最初的原子数目和事件总数。

第二行, N 个整数 E1, E2, …, EN, 由空格隔开。依次表示每个原子的能量。

接下来 M 行, 每行为一个字符串和两个整数, 描述一次事件,格式见题目描述。

N<=100,000,M<=100,000

1 ≤ e, Ei ≤ 109。 设 N’ 为当前时刻原子数目。

对于 merge 类事件, 1 ≤ x ≤ N’-1;

对于 insert 类事件, 1 ≤ x ≤ N’;

对于 max 和 min 类事件, 1 ≤ x < y ≤ N’。

任何时刻,保证 N’ ≥ 2。

Output

输出若干行, 按顺序依次表示每次 max 和 min 类事件的测量结果。

Sample Input

4 3

5 8 10 2

max 1 3

min 1 3

max 2 4

Sample Output

5 2 8

分析

用splay维护即可

代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 200010
using namespace std;
int fa
, c[2]
, si
, w
, maxn
, minn
, dl
, dr
, ms
, root;
char str[10];
int abs(int x)
{
return x > 0 ? x : -x;
}
void pushup(int x)
{
si[x] = si[c[0][x]] + si[c[1][x]] + 1;
dl[x] = c[0][x] ? dl[c[0][x]] : x;
dr[x] = c[1][x] ? dr[c[1][x]] : x;
maxn[x] = max(w[x] , max(maxn[c[0][x]] , maxn[c[1][x]]));
minn[x] = min(w[x] , min(minn[c[0][x]] , minn[c[1][x]]));
ms[x] = min(ms[c[0][x]] , ms[c[1][x]]);
if(c[0][x]) ms[x] = min(ms[x] , abs(w[x] - w[dr[c[0][x]]]));
if(c[1][x]) ms[x] = min(ms[x] , abs(w[x] - w[dl[c[1][x]]]));
}
void build(int l , int r , int f)
{
if(l > r) return;
int mid = (l + r) >> 1;
build(l , mid - 1 , mid) , build(mid + 1 , r , mid);
fa[mid] = f , c[mid > f][f] = mid;
pushup(mid);
}
void rotate(int &k , int x)
{
int y = fa[x] , z = fa[y] , l = (c[1][y] == x) , r = l ^ 1;
if(y == k) k = x;
else c[c[1][z] == y][z] = x;
fa[x] = z , fa[y] = x , fa[c[r][x]] = y , c[l][y] = c[r][x] , c[r][x] = y;
pushup(y) , pushup(x);
}
void splay(int &k , int x)
{
while(x != k)
{
int y = fa[x] , z = fa[y];
if(y != k)
{
if((c[0][z] == y) ^ (c[0][y] == x)) rotate(k , x);
else rotate(k , y);
}
rotate(k , x);
}
}
int find(int k , int x)
{
if(x <= si[c[0][k]]) return find(c[0][k] , x);
else if(x > si[c[0][k]] + 1) return find(c[1][k] , x - si[c[0][k]] - 1);
else return k;
}
int split(int l , int r)
{
int a = find(root , l - 1) , b = find(root , r + 1);
splay(root , a) , splay(c[1][root] , b);
return c[0][c[1][root]];
}
int main()
{
int n , m , i , x , y , t , tot;
scanf("%d%d" , &n , &m) , tot = n + 2;
for(i = 2 ; i <= n + 1 ; i ++ ) scanf("%d" , &w[i]);
maxn[0] = 0 , minn[0] = ms[0] = 0x3fffffff;
build(1 , n + 2 , 0) , root = (n + 3) >> 1;
while(m -- )
{
scanf("%s%d%d" , str , &x , &y);
switch(str[1])
{
case 'e': t = split(x + 1 , x + 2) , c[0][t] = c[1][t] = 0 , w[t] = y ,
pushup(t) , pushup(c[1][root]) , pushup(root); break;
case 'n': split(x + 2 , x + 1) , c[0][c[1][root]] = ++tot , w[tot] = y , fa[tot] = c[1][root] ,
pushup(tot) , pushup(c[1][root]) , pushup(root); break;
case 'a': t = split(x + 1 , y + 1) , printf("%d\n" , maxn[t] - minn[t]); break;
default: printf("%d\n" , ms[split(x + 1 , y + 1)]);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: