读题观察发现:
操作一只用一个平衡数维护单调区间即可。
且差值绝对值变化一定是变小,那么在插入的时候更新即可。
操作二只用一个线段树维护区间最小值即可.
观察发现在某个位置增加数以后更改的差值只与这个位置上一次操作的数有关,以及下一个位置的第一个数有关.
线段树维护即可.
c++代码如下:
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cctype>
#include<iostream>
#define rep(i,x,y) for(register int i = x ;i <= y; ++ i)
#define repd(i,x,y) for(register int i = x ;i >= y; -- i)
typedef long long ll;
using namespace std;
template<typename T>inline void read(T&x)
{
x = 0;char c;int sign = 1;
do { c = getchar(); if(c == '-') sign = -1; }while(!isdigit(c));
do { x = x * 10 + c - '0'; c = getchar(); }while(isdigit(c));
x *= sign;
}
const int N = 1e6 + 500,inf = 1e9+7;
int n,m,a[N];
char s[101];
struct Splay
{
int sz,root,val,t[N],ch[N][2],fa[N];
#define lc ch[x][0]
#define rc ch[x][1]
inline void clear() { sz = 0; val = inf; }
inline int get(int x) { return x == ch[fa[x]][1]; }
inline void rotate(int x)
{
int p = fa[x],t = get(x);
if(fa[p]) ch[fa[p]][get(p)] = x;
fa[x] = fa[p]; fa[p] = x;
ch[p][t] = ch[x][t^1];
if(ch[p][t]) fa[ch[p][t]] = p;
ch[x][t^1] = p;
if(p == root) root = x;
}
inline void splay(int x)
{
while(fa[x] != 0)
{
if(fa[fa[x]]) rotate(get(x) == get(fa[x]) ? fa[x] : x);
rotate(x);
}
}
int pre(int x)
{
if(!x) return inf;
if(rc) return pre(rc);
return t[x];
}
int lst(int x)
{
if(!x) return inf;
if(lc) return lst(lc);
return t[x];
}
void update(int x)
{
splay(x);
val = min(val,min(abs(pre(lc) - t[x]),abs(lst(rc) - t[x])));
}
inline void insert(int w)
{
int x = root;
if(!root)
{
root = ++ sz;
t[root] = w;
return;
}
while(ch[x][t[x] < w])
x = ch[x][t[x] < w];
ch[x][t[x] < w] = ++sz;
fa[sz] = x; t[sz] = w;
update(sz);
}
void print(int x)
{
if(lc) print(lc);
printf("%d ",t[x]);
if(rc) print(rc);
if(x == root) puts("");
}
inline int query() { return val; }
}splay;
struct Segment_tree
{
int val[N << 2],lst[N << 2],t[N << 2];
void build(int id,int l,int r)
{
if(l == r)
{
if(l != 1) val[id] = abs(a[l] - a[l - 1]);
else val[id] = inf;
t[id] = inf;
lst[id] = a[l];
return ;
}
int mid = l + r >> 1;
build(id << 1 ,l,mid);
build(id << 1|1,mid + 1,r);
val[id] = min(val[id<<1],val[id<<1|1]);
}
void update(int id,int l,int r,int x,int w)
{
if(l == r)
{
t[id] = min(t[id],abs(w - lst[id]));
if(l != n) val[id] = min(abs(a[l + 1] - w),t[id]);
lst[id] = w;
return;
}
int mid = l + r >> 1;
if(x <= mid) update(id << 1 ,l,mid,x,w);
else update(id << 1|1,mid + 1,r,x,w);
val[id] = min(val[id<<1],val[id<<1|1]);
}
int query() { return val[1]; }
}seg;
int main()
{
read(n); read(m);
rep(i,1,n) read(a[i]);
seg.build(1,1,n);
splay.clear();
rep(i,1,n) splay.insert(a[i]);
rep(i,1,m)
{
scanf("%s",s);
if(s[0] == 'I')
{
int p,w;
read(p); read(w);
seg.update(1,1,n,p,w);
splay.insert(w);
}
else if(s[4] == 'G')
printf("%d\n",seg.query());
else
printf("%d\n",splay.query());
}
return 0;
}