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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
| #include <bits/stdc++.h> #define MAX_N 100000 #define INF 0x3f3f3f3f #define flag cur->fa->fa!=tar&&cur->fa->fa->s[sn]==cur->fa using namespace std; template <class T> inline void read(T &x) { x = 0; int c = getchar(), f = 1; for (; !isdigit(c); c = getchar()) if (c == 45) f = -1; for (; isdigit(c); c = getchar()) (x *= 10) += f*(c-'0'); } struct SplayNode { SplayNode *s[2], *fa; int sz, c, w, d, mi, mx; void update() { sz = (s[0]?s[0]->sz:0)+(s[1]?s[1]->sz:0)+1; d = min(min(w,(s[0]?s[0]->d:INF)),(s[1]?s[1]->d:INF)); mi = min(min(c,(s[0]?s[0]->mi:INF)),(s[1]?s[1]->mi:INF)); mx = max(max(c,(s[0]?s[0]->mx:-INF)),(s[1]?s[1]->mx:-INF)); } }; struct SplayTree { SplayNode* rt; SplayNode* newnode(int x, int y) { SplayNode* v = new SplayNode; v->s[0] = v->s[1] = v->fa = NULL; v->sz = 1, v->c = x, v->w = y; v->d = y, v->mi = v->mx = x; return v; } SplayTree() { rt = newnode(INF, INF); rt->s[1] = newnode(INF, INF); rt->mx = -INF, rt->s[1]->mx = -INF; rt->s[1]->fa = rt, rt->update(); } void rotate(SplayNode* v, bool sn) { SplayNode* f = v->fa; f->s[sn^1] = v->s[sn], v->fa = f->fa; if (f->s[sn^1]) f->s[sn^1]->fa = f; if (v->fa) v->fa->s[f == f->fa->s[1]] = v; v->s[sn] = f, f->fa = v, f->update(), v->update(); } void splay(SplayNode* cur, SplayNode* tar) { while (cur != tar && cur->fa != tar) { bool sn = cur->fa->s[1] == cur; if (flag) rotate(cur->fa, sn^1); rotate(cur, sn^1); } if (cur->fa == tar) rotate(cur, cur->fa->s[0] == cur); if (tar == rt) rt = cur; } SplayNode* get_kth(SplayNode* v, int k) { int lsz = v->s[0] ? v->s[0]->sz : 0; if (k <= lsz) return get_kth(v->s[0], k); if (k > lsz+1) return get_kth(v->s[1], k-lsz-1); return v; } void insert(int p, int x, int y) { SplayNode* pre = get_kth(rt, p); SplayNode* suc = get_kth(rt, p+1); splay(pre, rt), splay(suc, rt->s[1]); suc->s[0] = newnode(x, y), suc->s[0]->fa = suc; suc->update(), rt->update(); } void remove(int p) { SplayNode* pre = get_kth(rt, p-1); SplayNode* suc = get_kth(rt, p+1); splay(pre, rt), splay(suc, rt->s[1]); suc->s[0]->fa = NULL, suc->s[0] = NULL; suc->update(), rt->update(); } int querymx(int l, int r) { SplayNode* pre = get_kth(rt, l-1); SplayNode* suc = get_kth(rt, r+1); splay(pre, rt), splay(suc, rt->s[1]); return suc->s[0]->mx-suc->s[0]->mi; } int querymi(int l, int r) { SplayNode* pre = get_kth(rt, l-1); SplayNode* suc = get_kth(rt, r+1); splay(pre, rt), splay(suc, rt->s[1]); return suc->s[0]->d; } } BBST; int n, m, a[MAX_N+5]; int main() { read(n), read(m); for (int i = 1; i <= n; i++) read(a[i]); for (int i = 1; i <= n; i++) BBST.insert(i, a[i], (i<n?abs(a[i]-a[i+1]):INF)); while (m--) { char opt[7]; scanf("%s", opt); if (opt[1] == 'e') { int p, x, y; read(p), read(x); BBST.remove(p+1), BBST.remove(p+1); SplayNode* pre = BBST.get_kth(BBST.rt, p); SplayNode* suc = BBST.get_kth(BBST.rt, p+1); if (p > 1) { int xx = pre->c, yy = abs(x-pre->c); BBST.remove(p), BBST.insert(p-1, xx, yy); } y = suc->c < INF ? abs(x-suc->c) : INF; BBST.insert(p, x, y); } if (opt[1] == 'n') { int p, x, y; read(p), read(x); SplayNode* pre = BBST.get_kth(BBST.rt, p+1); SplayNode* suc = BBST.get_kth(BBST.rt, p+2); int xx = pre->c, yy = abs(x-pre->c); BBST.remove(p+1), BBST.insert(p, xx, yy); y = suc->c < INF ? abs(x-suc->c) : INF; BBST.insert(p+1, x, y); } if (opt[1] == 'a') { int l, r; read(l), read(r); printf("%d\n", BBST.querymx(l+1, r+1)); } if (opt[1] == 'i') { int l, r; read(l), read(r); printf("%d\n", BBST.querymi(l+1, r)); } } return 0; }
|