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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
| #include <bits/stdc++.h> #define reg register #define lc t[x].ch[0] #define rc t[x].ch[1] using namespace std; const int MaxN = 50010; const int inf = 2147483647; int n, m, tot; int a[MaxN]; struct node { int ch[2]; int cnt, size, val, rnd; }; node t[MaxN << 5]; struct treap { int l, r, root; void update(int x) { t[x].size = t[lc].size + t[rc].size + t[x].cnt; } void rotate(int &x, int c) { int s = t[x].ch[c]; t[x].ch[c] = t[s].ch[c ^ 1]; t[s].ch[c ^ 1] = x; update(x); update(x = s); } void insert(int &x, int val) { if (!x) { x = ++tot; t[x].cnt = t[x].size = 1; t[x].rnd = rand(), t[x].val = val; return; } t[x].size++; if (t[x].val == val) { ++t[x].cnt; return; } int c = val > t[x].val; insert(t[x].ch[c], val); if (t[x].rnd > t[t[x].ch[c]].rnd) rotate(x, c); } void del(int &x, int val) { if (!x) return; if (t[x].val == val) { if (t[x].cnt > 1) { t[x].cnt--, t[x].size--; return; } bool c = t[lc].rnd > t[rc].rnd; if (lc == 0 || rc == 0) x = lc + rc; else rotate(x, c), del(x, val); } else --t[x].size, del(t[x].ch[t[x].val < val], val); } int rank(int x, int val) { if (!x) return 0; if (t[x].val == val) return t[lc].size; if (t[x].val > val) return rank(lc, val); else return t[lc].size + t[x].cnt + rank(rc, val); } int query_val(int x, int val) { while (1) { if (val <= t[lc].size) x = lc; else if (val > t[lc].size + t[x].cnt) val -= t[lc].size + t[x].cnt, x = rc; else return t[x].val; } } int query_pre(int x, int val) { if (!x) return -inf; if (t[x].val >= val) return query_pre(lc, val); else return cmax(t[x].val, query_pre(rc, val)); } int query_sub(int x, int val) { if (!x) return inf; if (t[x].val <= val) return query_sub(rc, val); else return cmin(t[x].val, query_sub(lc, val)); } }; treap tr[MaxN << 2]; struct tree { void build(int id, int l, int r) { tr[id].l = l, tr[id].r = r; for (int i = l; i <= r; i++) tr[id].insert(tr[id].root, a[i]); if (l == r) return; int mid = (l + r) >> 1; build(id << 1, l, mid); build(id << 1 | 1, mid + 1, r); } int query_rank(int id, int l, int r, int val) { if (tr[id].l > r || tr[id].r < l) return 0; if (l <= tr[id].l && tr[id].r <= r) { int ans = tr[id].rank(tr[id].root, val); return ans; } int ans = 0; ans += query_rank(id << 1, l, r, val); ans += query_rank(id << 1 | 1, l, r, val); return ans; } int query_val(int l, int r, int k) { int L = 0, R = 1e8; while (L < R) { int mid = (L + R + 1) >> 1; if (query_rank(1, l, r, mid) + 1 <= k) L = mid; else R = mid - 1; } return L; } void modify(int id, int pos, int val) { if (pos < tr[id].l || tr[id].r < pos) return; tr[id].del(tr[id].root, a[pos]); tr[id].insert(tr[id].root, val); if (tr[id].l == tr[id].r) return; modify(id << 1, pos, val); modify(id << 1 | 1, pos, val); } int query_pre(int id, int l, int r, int val) { if (tr[id].l > r || tr[id].r < l) return -inf; if (l <= tr[id].l && tr[id].r <= r) return tr[id].query_pre(tr[id].root, val); int ans = max(query_pre(id << 1, l, r, val), query_pre(id << 1 | 1, l, r, val)); return ans; } int query_sub(int id, int l, int r, int val) { if (tr[id].l > r || tr[id].r < l) return inf; if (l <= tr[id].l && tr[id].r <= r) return tr[id].query_sub(tr[id].root, val); int ans = min(query_sub(id << 1, l, r, val), query_sub(id << 1 | 1, l, r, val)); return ans; } } T; inline int read() { int x = 0; char ch = getchar(); while (ch > '9' || ch < '0') ch = getchar(); while (ch <= '9' && ch >= '0') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); return x; } int main() { srand(19260817); int n = read(), m = read(); for (int i = 1; i <= n; i++) a[i] = read(); int op; T.build(1, 1, n); for (int i = 1; i <= m; i++) { op = read(); int l, r, val, pos; if (op == 3) { pos = read(), val = read(); T.modify(1, pos, val); a[pos] = val; continue; } l = read(), r = read(), val = read(); if (op == 1) printf("%d\n", T.query_rank(1, l, r, val) + 1); else if (op == 2) printf("%d\n", T.query_val(l, r, val)); else if (op == 4) printf("%d\n", T.query_pre(1, l, r, val)); else if (op == 5) printf("%d\n", T.query_sub(1, l, r, val)); } return 0; }
|