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
| #include <bits/stdc++.h> #define MAX_N 200000 #define INF 2147483647 #define mid ((s+t)>>1) #define flag (cur->fa->fa!=tar&&cur->fa->fa->s[sn]==cur->fa) using namespace std; int n, m, c[MAX_N+5]; struct SplayNode { SplayNode *s[2], *fa; int val, w, sz; void updata() {sz = w+(s[0]?s[0]->sz:0)+(s[1]?s[1]->sz:0);} }; struct SplayTree { SplayNode* rt; SplayNode* newnode(int _val) { SplayNode* v = new SplayNode; v->s[0] = v->s[1] = v->fa = NULL; v->val = _val, v->w = v->sz = 1; return v; } SplayTree() { rt = newnode(-INF), rt->s[1] = newnode(INF); rt->s[1]->fa = rt, rt->updata(); } 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->updata(), v->updata(); } 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* predecessor(int _val) { SplayNode *cur = rt, *cpy = rt; for (; cur; cur = cur->s[_val > cur->val]) if (cur->val < _val) cpy = cur; return cpy; } SplayNode* successor(int _val) { SplayNode *cur = rt, *cpy = rt; for (; cur; cur = cur->s[_val >= cur->val]) if (cur->val > _val) cpy = cur; return cpy; } void insert(int _val) { SplayNode* pre = predecessor(_val); SplayNode* suc = successor(_val); splay(pre, rt), splay(suc, rt->s[1]); if (suc->s[0]) suc->s[0]->w++, suc->s[0]->sz++; else suc->s[0] = newnode(_val), suc->s[0]->fa = suc; suc->updata(), rt->updata(); } void remove(int _val) { SplayNode* pre = predecessor(_val); SplayNode* suc = successor(_val); splay(pre, rt), splay(suc, rt->s[1]); suc->s[0]->w--, suc->s[0]->sz--; if (!suc->s[0]->w) suc->s[0] = NULL; suc->updata(), rt->updata(); } int get_rank(int _val) { SplayNode* pre = predecessor(_val); SplayNode* suc = successor(_val); splay(pre, rt), splay(suc, rt->s[1]); if (suc == NULL) return rt->sz-1; return rt->sz-suc->sz-1; } } tr[(MAX_N<<2)+5]; void ins(int v, int s, int t, int p, int x) { tr[v].insert(x); if (s == t) return; if (p <= mid) ins(v<<1, s, mid, p, x); else ins(v<<1|1, mid+1, t, p, x); } void modify(int v, int s, int t, int p, int x, int o) { tr[v].remove(o), tr[v].insert(x); if (s == t) return; if (p <= mid) modify(v<<1, s, mid, p, x, o); else modify(v<<1|1, mid+1, t, p, x, o); } int query(int v, int s, int t, int l, int r, int x) { if (s >= l && t <= r) return tr[v].get_rank(x); int ret = 0; if (l <= mid) ret += query(v<<1, s, mid, l, r, x); if (r >= mid+1) ret += query(v<<1|1, mid+1, t, l, r, x); return ret; } int get_ind(int l, int r, int k) { int s = 0, t = INF, ret = -1; while (s <= t) if (query(1, 1, n, l, r, mid) >= k) t = mid-1; else ret = mid, s = mid+1; return ret; } int get_pre(int v, int s, int t, int l, int r, int x) { if (s >= l && t <= r) return tr[v].predecessor(x)->val; int ret = -INF; if (l <= mid) ret = max(ret, get_pre(v<<1, s, mid, l, r, x)); if (r >= mid+1) ret = max(ret, get_pre(v<<1|1, mid+1, t, l, r, x)); return ret; } int get_suc(int v, int s, int t, int l, int r, int x) { if (s >= l && t <= r) return tr[v].successor(x)->val; int ret = INF; if (l <= mid) ret = min(ret, get_suc(v<<1, s, mid, l, r, x)); if (r >= mid+1) ret = min(ret, get_suc(v<<1|1, mid+1, t, l, r, x)); return ret; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", c+i), ins(1, 1, n, i, c[i]); while (m--) { int opt, l, r, p, k, x; scanf("%d", &opt); if (opt == 1) scanf("%d%d%d", &l, &r, &x), printf("%d\n", query(1, 1, n, l, r, x)+1); if (opt == 2) scanf("%d%d%d", &l, &r, &k), printf("%d\n", get_ind(l, r, k)); if (opt == 3) scanf("%d%d", &p, &x), modify(1, 1, n, p, x, c[p]), c[p] = x; if (opt == 4) scanf("%d%d%d", &l, &r, &x), printf("%d\n", get_pre(1, 1, n, l, r, x)); if (opt == 5) scanf("%d%d%d", &l, &r, &x), printf("%d\n", get_suc(1, 1, n, l, r, x)); } }
|