神秘物质

Time Limit: 10 Sec  Memory Limit: 256 MB

Description

Input

  

Output

  

Sample Input

  

Sample Output

  1
  2
  1
  5

HINT

  

Main idea

  每个点有一个权值,维护一个结构,支持合并相邻两点,删除单点,加入单点,查询区间子集极差的最大值和最小值。

Solution

  首先我们可以发现,区间子集极差的最大值显然就是整个区间的最大值-区间最小值,然后区间子集极差最小值只有相邻点的才会产生贡献

  那么我们用Splay来维护这个结构即可,维护一下子树最大值、子树最小值、子树邻差最小值即可。

Code

 #include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;
typedef long long s64; const int ONE = ;
const int INF = ; int n,m;
int x,y,a[ONE];
int root,cnt;
int lc[ONE],rc[ONE],fa[ONE];
int size[ONE],val[ONE];
int maxx[ONE],minn[ONE],del[ONE];
int Ls[ONE],Rs[ONE];
char ch[]; inline int get()
{
int res=,Q=; char c;
while( (c=getchar())< || c>)
if(c=='-')Q=-;
if(Q) res=c-;
while((c=getchar())>= && c<=)
res=res*+c-;
return res*Q;
} void Up(int i)
{
size[i] = size[lc[i]] + size[rc[i]] + ;
maxx[i] = minn[i] = val[i];
del[i] = INF;
Ls[i] = Rs[i] = i;
if(lc[i])
{
Ls[i] = Ls[lc[i]];
maxx[i] = max(maxx[i], maxx[lc[i]]);
minn[i] = min(minn[i], minn[lc[i]]);
del[i] = min(del[i], del[lc[i]]);
del[i] = min(del[i], abs( val[i]-val[Rs[lc[i]]] ) );
}
if(rc[i])
{
Rs[i] = Rs[rc[i]];
maxx[i] = max(maxx[i], maxx[rc[i]]);
minn[i] = min(minn[i], minn[rc[i]]);
del[i] = min(del[i], del[rc[i]]);
del[i] = min(del[i], abs( val[i]-val[Ls[rc[i]]] ) );
}
} void Turn(int x)
{
int y = fa[x], z = fa[y];
int b = x==lc[y] ? rc[x]:lc[x]; fa[y] = x; fa[x] = z;
if(b) fa[b] = y; if(z)
{
if(y == lc[z]) lc[z] = x;
else rc[z] = x;
} if(x==lc[y]) rc[x] = y,lc[y] = b;
else lc[x] = y, rc[y] = b; Up(y); Up(x);
} void Splay(int x,int pos)
{
while(fa[x] != pos)
{
if(fa[fa[x]] != pos)
{
if( (lc[fa[x]]==x) == (lc[fa[fa[x]]]==fa[x]) ) Turn(fa[x]);
else Turn(x);
}
Turn(x);
}
if(pos == ) root = x;
} int Build(int i,int l,int r)
{
int mid = l+r >> ;
fa[mid] = i;
if(l <= mid-) lc[mid] = Build(mid, l,mid-);
if(mid+ <= r) rc[mid] = Build(mid, mid+,r);
Up(mid);
return mid;
} int Getid(int num)
{
int i = root;
while(size[lc[i]] + != num)
{
if(size[lc[i]] + < num)
num -= size[lc[i]] + , i = rc[i];
else i = lc[i];
}
return i;
} void Delete(int i)
{
int x = Getid(i);
Splay(x, );
int L = Rs[lc[x]]; Splay(L,);
int R = Ls[rc[x]]; Splay(R,L);
lc[R] = ;
Splay(R,);
} void Insert(int i,int Val)
{
int x = Getid(i);
Splay(x,);
int R = Ls[rc[x]]; Splay(R,x);
val[++cnt] = Val; fa[cnt] = R; lc[R] = cnt;
Splay(cnt,);
} int main()
{
n=get(); m=get();
for(int i=;i<=n;i++)
val[i+] = get();
val[] = val[n+] = INF; cnt = n+;
root = n+ >> ;
Build(,,n+); while(m--)
{
scanf("%s",ch+); x=get(); y=get();
x++; if(ch[] == 'r')
{
Insert(x+,y);
Delete(x); Delete(x);
}
if(ch[] == 's')
Insert(x,y);
if(ch[] == 'x')
{
y++;
x = Getid(x-); y = Getid(y+);
Splay(x,); Splay(y,x);
printf("%d\n", maxx[lc[y]] - minn[lc[y]]);
}
if(ch[] == 'n')
{
y++;
x = Getid(x-); y = Getid(y+);
Splay(x,); Splay(y,x);
printf("%d\n", del[lc[y]]);
} }
}

【BZOJ4864】神秘物质 [Splay]的更多相关文章

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

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

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

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

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

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

  4. BZOJ4864: [BeiJing 2017 Wc]神秘物质(Splay)

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

  5. BZOJ 4864 [BJWC2017]神秘物质 (splay)

    题目大意: 让你维护一个序列,支持: 1.合并两个相邻的数为一个新的数 2.在某个位置插入一个数 3.查询一个区间的任意子区间极差的最大值 4.查询一个区间的任意子区间极差的最小值 前两个操作可以用$ ...

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

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

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

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

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

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

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

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

随机推荐

  1. python切片详解

    先从原理上分析切片运算: list的切片,内部是调用__getitem__,__setitem__,__delitem__和slice函数.而slice函数又是和range()函数相关的. 给切片传递 ...

  2. 第二次作业 编程题 PAT 1001A+B Format

    Github的object-oriented仓库:1001.A+BFormat(20) 1.解题的思路过程 在之前学习C语言时曾经碰到过类似的将数字转换成字符输出的情况,这道题目要求输出的数字每三个间 ...

  3. 《剑指offer》---把数组排成最小的数

    本文算法使用python3实现 1 题目描述:   输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组 $ [3,32,321] $ ,则打印出这 ...

  4. Qt-排序

    1.要求传入起始指针,总长度,单元素空间占用大小(sizeof(A[i])),判断函数. 判断函数参数类型为const void *,使用需要在函数内自行转换为对应类型, 返回值为整数型,升序排序时正 ...

  5. 【问题解决】Project facet Java version 1.7 (或者1.8)is not supported.

    在移植eclipse项目时,如果遇到 “Project facet Java version 1.7 is not supported.” 项目中的jdk1.7不支持.说明项目是其他版本jdk编译的, ...

  6. [STL] 如何将一个vector赋给另一个vector

    vector 有个函数assign, 可以帮助执行赋值操作. assign会清空你的容器. assign函数: 函数原型: void assign(const_iterator first,const ...

  7. WPF通过NPIO读写Excel操作

    自已摸索实现了对excel简单的上传和下载并做了一个封装类,下面分享一下. 先去官网:http://npoi.codeplex.com/下载需要引入dll(可以选择.net2.0或者.net4.0的d ...

  8. [Leetcode] search insert position 寻找插入位置

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  9. 洛谷 P2324 [SCOI2005]骑士精神 解题报告

    P2324 [SCOI2005]骑士精神 题目描述 输入输出格式 输入格式: 第一行有一个正整数T(T<=10),表示一共有N组数据.接下来有T个5×5的矩阵,0表示白色骑士,1表示黑色骑士,* ...

  10. syslog服务器配置笔记

    syslog服务器可以用作一个网络中的日志监控中心,rsyslog是一个开源工具,被广泛用于Linux系统以通过TCP/UDP协议转发或接收日志消息.本文我们来讲讲在 Linux 上配置一个 sysl ...