#平衡树#洛谷 1110 [ZJOI2007]报表统计
分析
最小值只需要开两棵平衡树,一棵维护所有元素,一棵维护相邻最小值,
对于全局最小值,对于每次插入查找前驱后继更新最小值即可,
相邻最小值,对于每个原数列的数维护它的开头和结尾是什么数,
然后在往后插入一个数时直接先把原来的相邻删除再加进去,
求平衡树的最小值即可
代码
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#define rr register
using namespace std;
const int N=500011,inf=1e9; char str[21];
const double alp=0.75; int ans,n,m,len,St[N],Ed[N],a[N];
inline signed iut(){
rr int ans=0,f=1; rr char c=getchar();
while (!isdigit(c)) f=(c=='-')?-f:f,c=getchar();
while (isdigit(c)) ans=(ans<<3)+(ans<<1)+(c^48),c=getchar();
return ans*f;
}
inline void print(int ans){
if (ans<0) putchar('-'),ans=-ans;
if (ans>9) print(ans/10);
putchar(ans%10+48);
}
inline signed min(int a,int b){return a<b?a:b;}
inline signed max(int a,int b){return a>b?a:b;}
struct ScapeGoat_Tree{
int siz[N<<1],son[N<<1][2],root,tot=0,fat[N<<1],w[N<<1],stac[N<<1],TOP;
inline void BUILD(){
tot=2,root=1;
w[1]=-inf,siz[1]=2,son[1][1]=2,
w[2]=inf,siz[2]=1,fat[2]=1;
}
inline bool balance(int x){return alp*siz[x]>=(max(siz[son[x][0]],siz[son[x][1]]));}
inline void recycle(int x){
if (son[x][0]) recycle(son[x][0]);
stac[++TOP]=x;
if (son[x][1]) recycle(son[x][1]);
}
inline signed build(int l,int r){
if (l>r) return 0;
rr int mid=(l+r)>>1,x=stac[mid];
fat[son[x][0]=build(l,mid-1)]=x;
fat[son[x][1]=build(mid+1,r)]=x;
siz[x]=siz[son[x][0]]+siz[son[x][1]]+1;
return x;
}
inline void rebuild(int x){
TOP=0; recycle(x);
rr int fa=fat[x],wh=son[fat[x]][1]==x;
rr int now=build(1,TOP);
fat[son[fa][wh]=now]=fa;
if (root==x) root=now;
}
inline void Insert(int x){
rr int now=root,renew=++tot;
siz[renew]=1,w[renew]=x;
while (1){
++siz[now];
rr bool wh=x>=w[now];
if (son[now][wh]) now=son[now][wh];
else {fat[son[now][wh]=renew]=now; break;}
}
rr int UP=0;
for (rr int i=renew;i;i=fat[i]) if (!balance(i)) UP=i;
if (UP) rebuild(UP);
}
inline void Delete(int x){
if (son[x][0]&&son[x][1]){
rr int now=son[x][0];
while (son[now][1]) now=son[now][1];
w[x]=w[now],x=now;
}
rr int cho=son[x][0]?son[x][0]:son[x][1],wh=son[fat[x]][1]==x;
fat[son[fat[x]][wh]=cho]=fat[x];
for (rr int i=fat[x];i;i=fat[i]) --siz[i];
if (x==root) root=cho;
}
inline signed arr(int x){
rr int now=root;
while (1){
if (w[now]==x) return now;
else now=son[now][w[now]<x];
}
}
inline signed rank(int x){
rr int now=root,ans=0;
while (now){
if (x>w[now]) ans+=siz[son[now][0]]+1,now=son[now][1];
else now=son[now][0];
}
return ans;
}
inline signed kth(int x){
rr int now=root;
while (1){
if (siz[son[now][0]]==x-1) return now;
else if (siz[son[now][0]]>=x) now=son[now][0];
else x-=siz[son[now][0]]+1,now=son[now][1];
}
}
inline signed pre(int x){
rr int now=root,ans=-inf;
while (now){
if (x>w[now]) ans=max(ans,w[now]),now=son[now][1];
else now=son[now][0];
}
return ans;
}
inline signed suf(int x){
rr int now=root,ans=inf;
while (now){
if (x<w[now]) ans=min(ans,w[now]),now=son[now][0];
else now=son[now][1];
}
return ans;
}
}Tre[2];
inline signed Abs(int x){return x<0?-x:x;}
signed main(){
n=iut(),m=iut(),Tre[0].BUILD(),Tre[1].BUILD();
for (rr int i=1;i<=n;++i) St[i]=Ed[i]=a[i]=iut();
for (rr int i=1;i<n;++i) Tre[0].Insert(Abs(St[i+1]-St[i]));
for (rr int i=1;i<=n;++i) Tre[1].Insert(St[i]);
sort(a+1,a+1+n),ans=Abs(a[2]-a[1]);
for (rr int i=3;i<=n;++i) ans=min(ans,Abs(a[i]-a[i-1]));
for (rr int i=1;i<=m;++i){
scanf("%s",str+1),len=strlen(str+1);
if (len==6){
rr int x=iut(),w=iut();
if (x<n) Tre[0].Delete(Tre[0].arr(Abs(St[x+1]-Ed[x])));
if (x<n) Tre[0].Insert(Abs(St[x+1]-w));
Tre[0].Insert(Abs(w-Ed[x])),Ed[x]=w;
rr int t1=Tre[1].suf(-inf),t2=Tre[1].pre(inf);
if (w<t1) ans=min(ans,t1-w);
else if (t2<w) ans=min(ans,w-t2);
else {
if (Tre[1].suf(w-1)==w) {ans=0; continue;}
ans=min(ans,min(w-Tre[1].pre(w),Tre[1].suf(w)-w));
}
Tre[1].Insert(w);
}else if (len==7) print(Tre[0].suf(-inf)),putchar(10);
else print(ans),putchar(10);
}
return 0;
}
#平衡树#洛谷 1110 [ZJOI2007]报表统计的更多相关文章
- BZOJ1058或洛谷1110 [ZJOI2007]报表统计
BZOJ原题链接 洛谷原题链接 STL 本题可以直接使用\(\mathtt{STL\ multiset}\)水过去. 因为本题插入数的操作实际上就是将原数列分为\(n\)段,在每一段的末尾插入数,所以 ...
- 洛谷.1110.[ZJOI2007]报表统计(Splay Heap)
题目链接 附纯SplayTLE代码及主要思路: /* 可以看做序列有n段,Insert是每次在每一段最后插入一个元素 只有插入,没有删除,所以插入一个元素对于询问1影响的只有该元素与前边一个元素(同段 ...
- 洛谷.1110.[ZJOI2007]报表统计(Multiset Heap)
题目链接 主要思路 /* 对于询问1,用堆代替multiset/Splay 对于询问2,multiset 1.注意哨兵元素 2.注意multiset中删除时是删除某元素的一个位置,而不是这个元素!这个 ...
- 洛谷.1110.[ZJOI2007]报表统计(Multiset)
题目链接 主要思路 /* 其实只需要multiset即可 对于询问1,删除.插入差值,输出最小元素 对于询问2,插入后用前驱后继更新 1.注意哨兵元素 2.注意multiset中删除时是删除某元素的一 ...
- 洛谷 P1110 [ZJOI2007]报表统计 解题报告
P1110 [ZJOI2007]报表统计 题目描述 \(Q\)的妈妈是一个出纳,经常需要做一些统计报表的工作.今天是妈妈的生日,小\(Q\)希望可以帮妈妈分担一些工作,作为她的生日礼物之一. 经过仔细 ...
- 2018.11.09 洛谷P1110 [ZJOI2007]报表统计(multiset)
传送门 sb题. 直接用两个multisetmultisetmultiset维护相邻两个数的差值和所有数的前驱后继. 插入一个数的时候更新一下就行了. 代码: #include<bits/std ...
- bzoj P1058 [ZJOI2007]报表统计——solution
1058: [ZJOI2007]报表统计 Time Limit: 15 Sec Memory Limit: 162 MB Submit: 4099 Solved: 1390 [Submit][St ...
- 【BZOJ1058】[ZJOI2007]报表统计 STL
[BZOJ1058][ZJOI2007]报表统计 Description 小Q的妈妈是一个出纳,经常需要做一些统计报表的工作.今天是妈妈的生日,小Q希望可以帮妈妈分担一些工作,作为她的生日礼物之一.经 ...
- [ZJOI2007]报表统计(splay,堆)
[ZJOI2007]报表统计(luogu) Description 题目描述 Q的妈妈是一个出纳,经常需要做一些统计报表的工作.今天是妈妈的生日,小Q希望可以帮妈妈分担一些工作,作为她的生日礼物之一. ...
- bzoj1058: [ZJOI2007]报表统计
set.操作:insert(u,v)在u后面插入v,若u后面已插入过,在插入过的后面插入.mingap求出序列两两之间差值的最小值.minsortgap求出排序后的序列两两之间的最小值.用multis ...
随机推荐
- 进程,join的使用,守护进程---day30
1.进程 # ### 进程 import os,time #当前进程id(子进程) res = os.getpid() print(res) #1772 #当前进程id(父进程) res = os.g ...
- 【C++ OOP 03 友元】各种友元例子以及如何类外写成员函数
[友元] 在程序里,有些私有属性 也想让类外特殊的一些函数或者类进行访问,就需要用到友元的技术 友元的目的就是让一个函数或者类 访问另一个类中私有成员 友元的关键字为 friend 友元的三种实现 全 ...
- HAProxy端口资源耗尽的解决办法
项目背景 系统使用HAProxy为mq和部分应用的负载均衡服务.近期,瞬时流量过大,导致出现连锁反应,HA开始波动. HAProxy版本:1.6.3 问题分析 心跳检测大量失败,项目状态极不稳定.观察 ...
- 【Azure 应用服务】App Service 使用Tomcat运行Java应用,如何设置前端网页缓存的相应参数呢(-Xms512m -Xmx1204m)?
问题描述 App Service 使用Tomcat运行Java应用,如何设置前端网页缓存的相应参数呢(-Xms512m -Xmx1204m)? 问题回答 App Service在Windows的环境中 ...
- C++ STL 容器-Deque
C++ STL 容器-Deque std::deque(双端队列)是C++标准模板库(STL)中的一个容器,它支持在序列的两端快速插入和删除元素.与std::vector和std::list等其他序列 ...
- Java 接口的使用
1 package com.bytezreo.interfacetest; 2 3 /** 4 * 5 * @Description 接口的使用 6 * @author Bytezero·zhengl ...
- 有了net/http, 为什么还要有gin
1. 简介 在Go语言中,net/http 包提供了一个强大且灵活的标准HTTP库,可以用来构建Web应用程序和处理HTTP请求.这个包是Go语言标准库的一部分,因此所有的Go程序都可以直接使用它.既 ...
- 并行化优化KD树算法:使用C#实现高效的最近邻搜索
本文信息 中文名:<并行化优化KD树算法:使用C#实现高效的最近邻搜索> 英文名:"Parallelized Optimization of KD-Tree Algorithm: ...
- 新零售SaaS架构:什么是线上商城系统?
零售商家为什么要建设线上商城 传统的实体门店服务范围有限,只能吸引周边500米内的消费者.因此,如何拓展服务范围,吸引更多消费者到店,成为了店家迫切需要解决的问题. 缺乏忠实顾客,客户基础不稳,往往是 ...
- java中webSocket发送图片文件数据非常慢
一.问题由来 目前在开发的这个小程序中有一个功能需要和Unity客户端进行互动操作,互动的大致流程为在微信小程序中点击一个操作,发送一个HTTP请求, Java后台收到这个请求后,会给Unity客户端 ...