Splay
merge和insert都是常规操作。merge的话把x和x+1提出来,删掉x+1,修改x的权值即可。,nsert反过来。
区间极差最大值就是区间内最大值减最小值。而区间极差最小值显然在相邻两个数之间取到。那么我们分别记录一下当前位置左边和右边的节点(左边对应左子树的最右边节点,右边对应右子树的最左边节点)即可。
代码:
#include<cctype>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 100005
#define F inline
using namespace std;
struct tree{ int mx,mn,p,x,fa,to[2],sz,l,r; }t[N<<1];
int n,m,rt,nd,a[N],id[N<<1];
char s[10];
F int _read(){
int x=0; char ch=getchar();
while (!isdigit(ch)) ch=getchar();
while (isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
return x;
}
F void writec(int x){ if (x>9) writec(x/10); putchar(x%10+48); }
F void _write(int x){ writec(x),puts(""); }
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)>(y)?(y):(x))
#define abs(x) ((x)>0?(x):-(x))
F void pshp(int x){
int l=t[x].to[0],r=t[x].to[1];
t[x].sz=t[l].sz+t[r].sz+1;
t[x].mx=max(max(t[l].mx,t[r].mx),t[x].x);
t[x].mn=min(min(t[l].mn,t[r].mn),t[x].x);
t[x].l=(l?t[l].l:x),t[x].r=(r?t[r].r:x);
t[x].p=min(t[l].p,t[r].p);
if (l) t[x].p=min(t[x].p,abs(t[x].x-t[t[l].r].x));
if (r) t[x].p=min(t[x].p,abs(t[x].x-t[t[r].l].x));
}
F void rtt(int x,int &w){
int y=t[x].fa,z=t[y].fa,l=t[y].to[0]==x;
y==w?w=x:t[z].to[t[z].to[1]==y]=x;
t[x].fa=z,t[y].fa=x,t[t[x].to[l]].fa=y;
t[y].to[l^1]=t[x].to[l],t[x].to[l]=y;
pshp(y),pshp(x);
}
F void splay(int x,int &w){
for (int y=t[x].fa,z=t[y].fa;x!=w;rtt(x,w),y=t[x].fa,z=t[y].fa)
if (y!=w) rtt((t[z].to[0]==y^t[y].to[0]==x?x:y),w);
}
void build(int l,int r,int fa){
if (l>r) return; int mid=l+r>>1,x=id[mid];
if (l==r) t[x]=(tree){a[x],a[x],1e9};
build(l,mid-1,mid),build(mid+1,r,mid);
t[x].x=a[x],t[x].fa=id[fa],pshp(x);
t[id[fa]].to[mid>=fa]=x;
}
int srch(int x,int w){
int l=t[x].to[0];
if (t[l].sz+1==w) return x;
if (t[l].sz>=w) return srch(l,w);
return srch(t[x].to[1],w-t[l].sz-1);
}
F int sprt(int l,int r){
int x=srch(rt,l),y=srch(rt,r+2);
splay(x,rt),splay(y,t[x].to[1]);
return t[y].to[0];
}
int main(){
n=_read(),m=_read();
for (int i=1;i<=n+2;i++) id[i]=i;
for (int i=1;i<=n;i++) a[i+1]=_read();
t[0].mx=0,t[0].mn=t[0].p=1e9;
build(1,n+2,0),rt=(n+3)>>1,nd=n+2;
for (int x,y,z;m;m--){
scanf("%s",s),x=_read(),y=_read();
if (s[1]=='e'){
z=sprt(x,x+1),t[z].to[0]=t[z].to[1]=0,t[z].x=y;
pshp(z),pshp(t[z].fa),pshp(rt);
}
else if (s[1]=='n'){
sprt(x+1,x),z=t[t[rt].to[1]].to[0]=++nd;
t[z].x=y,t[z].fa=t[rt].to[1];
pshp(z),pshp(t[z].fa),pshp(rt);
}
else z=sprt(x,y),_write(s[1]=='i'?t[z].p:t[z].mx-t[z].mn);
}
return 0;
}