1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| #include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> #include<cctype> #include<cstdio> #include<vector> #include<string> #include<queue> #include<stack> #include<cmath> #include<ctime> #include<map> #include<set> using namespace std; const int inf=0x7fffffff; const double eps=1e-10; const double pi=acos(-1.0);
inline int read(){ int x=0,f=1;char ch;ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') f=0;ch=getchar();} while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch&15);ch=getchar();} if(f)return x;else return -x; } const int N=1000010; int ans=inf; inline int Abs(const int &a){if(a<0)return -a;return a;} inline int min(const int &a,const int &b){if(a<b)return a;return b;} inline int max(const int &a,const int &b){if(a>b)return a;return b;} struct fhq_treap_val{ #define lson ls[rt] #define rson rs[rt] int val[N],size[N],key[N]; int ls[N],rs[N]; int root,cnt; void pushup(int rt){size[rt]=size[lson]+size[rson]+1;} int merge(int a,int b){ if(!a||!b)return a|b; if(key[a]<key[b]){rs[a]=merge(rs[a],b);pushup(a);return a;} else {ls[b]=merge(a,ls[b]);pushup(b);return b;} } void split(int rt,int x,int &a,int &b){ if(!rt){a=b=0;return;} if(val[rt]<=x){a=rt;split(rson,x,rson,b);} else{b=rt;split(lson,x,a,lson);} pushup(rt); } int Max(int rt){while(rson)rt=rson;return val[rt];} int Min(int rt){while(lson)rt=lson;return val[rt];} int newnode(int x){ int rt=++cnt; val[rt]=x; size[rt]=1;key[rt]=rand(); lson=rson=0; return rt; } void Insert(int x){ int a,b; split(root,x,a,b); int rt=newnode(x); root=merge(merge(a,rt),b); } void Insert_2(int x){ int a,b; split(root,x,a,b); if(size[a])ans=min(ans,abs(x-Max(a))); if(size[b])ans=min(ans,abs(Min(b)-x)); int rt=newnode(x); root=merge(merge(a,rt),b); } void Delete(int x){ int a,b,c; split(root,x,a,c); split(a,x-1,a,b); b=merge(ls[b],rs[b]); root=merge(merge(a,b),c); } }T1,T2; int n,m,a[N],b[N],id,g; char op[20]; int main() { srand(time(NULL)); n=read();m=read(); for(int i=1;i<=n;i++)a[i]=b[i]=read(); for(int i=1;i<n;i++)T1.Insert(Abs(a[i+1]-a[i])); for(int i=1;i<=n;i++)T2.Insert_2(a[i]); while(m--){ scanf("%s",op); if(op[0]=='I'){ id=read();g=read(); T2.Insert_2(g); if(id!=n){ T1.Delete(Abs(a[id+1]-b[id])); T1.Insert(Abs(a[id+1]-g)); } T1.Insert(Abs(b[id]-g)); b[id]=g; } else if(op[4]=='G')printf("%d\n",T1.Min(T1.root)); else printf("%d\n",ans); } return 0; }
|