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 222 223 224 225 226 227 228 229 230 231 232 233 234
| #include <bits/stdc++.h> using namespace std; const int N = 15e5 + 5, INF = 2147483647; struct Splay { int s[2], fa, size, v; void inint(int _v, int _fa) { v = _v; fa = _fa; size = 1; } } tr[N]; struct Seg_Tree { int l, r; int rt; } ttr[N]; int n, m, idx = 0; int w[N];
void push_up(int x) { tr[x].size = tr[tr[x].s[0]].size + tr[tr[x].s[1]].size + 1; } void rotate(int x) { int y = tr[x].fa; int z = tr[y].fa; int k = tr[y].s[1] == x; tr[z].s[tr[z].s[1] == y] = x, tr[x].fa = z; tr[y].s[k] = tr[x].s[k ^ 1], tr[tr[x].s[k ^ 1]].fa = y; tr[x].s[k ^ 1] = y, tr[y].fa = x; push_up(y), push_up(x); } void splay(int &root, int x, int k) { while (tr[x].fa != k) { int y = tr[x].fa; int z = tr[y].fa; if (z != k) (tr[y].s[1] == x) ^ (tr[z].s[1] == y) ? rotate(x) : rotate(y); rotate(x); } if (!k) root = x; }
void insert(int &root, int v) { int u = root, p = 0; while (u) p = u, u = tr[u].s[v > tr[u].v]; u = ++idx; if (p) tr[p].s[v > tr[p].v] = u; tr[u].inint(v, p); splay(root, u, 0); }
int get_k(int root, int v) { int u = root, res = 0; while (u) { if (tr[u].v < v) res += tr[tr[u].s[0]].size + 1, u = tr[u].s[1]; else u = tr[u].s[0]; } return res; }
void update(int &root, int x, int y) { int u = root; while (u) { if (tr[u].v == x) break; if (tr[u].v < x) u = tr[u].s[1]; else u = tr[u].s[0]; } splay(root, u, 0); int l = tr[u].s[0], r = tr[u].s[1]; while (tr[l].s[1]) l = tr[l].s[1]; while (tr[r].s[0]) r = tr[r].s[0]; splay(root, l, 0), splay(root, r, l); tr[r].s[0] = 0; push_up(r), push_up(l); insert(root, y); }
int get_pre(int root, int v) { int u = root, res = -INF; while (u) { if (tr[u].v < v) res = max(res, tr[u].v), u = tr[u].s[1]; else u = tr[u].s[0]; } return res; }
int get_suc(int root, int v) { int u = root, res = INF; while (u) { if (tr[u].v > v) res = min(res, tr[u].v), u = tr[u].s[0]; else u = tr[u].s[1]; } return res; }
void build(int u, int l, int r) { ttr[u].l = l, ttr[u].r = r; insert(ttr[u].rt, -INF), insert(ttr[u].rt, INF); for (int i = l; i <= r; ++i) insert(ttr[u].rt, w[i]); if (l == r) return; int mid = (l + r) >> 1; build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r); }
int query(int u, int l, int r, int x) { if (ttr[u].l >= l && ttr[u].r <= r) return get_k(ttr[u].rt, x) - 1; int mid = (ttr[u].l + ttr[u].r) >> 1, res = 0; if (l <= mid) res += query(u << 1, l, r, x); if (r > mid) res += query(u << 1 | 1, l, r, x); return res; }
void change(int u, int p, int x) { update(ttr[u].rt, w[p], x); if (ttr[u].l == ttr[u].r) return; int mid = (ttr[u].l + ttr[u].r) >> 1; if (p <= mid) change(u << 1, p, x); else change(u << 1 | 1, p, x); }
int query_pre(int u, int l, int r, int x) { if (ttr[u].l >= l && ttr[u].r <= r) return get_pre(ttr[u].rt, x); int mid = (ttr[u].l + ttr[u].r) >> 1, res = -INF; if (l <= mid) res = max(res, query_pre(u << 1, l, r, x)); if (r > mid) res = max(res, query_pre(u << 1 | 1, l, r, x)); return res; }
int query_suc(int u, int l, int r, int x) { if (ttr[u].l >= l && ttr[u].r <= r) return get_suc(ttr[u].rt, x); int mid = (ttr[u].l + ttr[u].r) >> 1, res = INF; if (l <= mid) res = min(res, query_suc(u << 1, l, r, x)); if (r > mid) res = min(res, query_suc(u << 1 | 1, l, r, x)); return res; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", &w[i]); build(1, 1, n); while (m--) { int op, a, b, x; scanf("%d", &op); if (op == 1) { scanf("%d%d%d", &a, &b, &x); printf("%d\n", query(1, a, b, x) + 1); } else if (op == 2) { scanf("%d%d%d", &a, &b, &x); int l = 0, r = 1e8, ans; while (l <= r) { int mid = (l + r) >> 1; if (query(1, a, b, mid) + 1 <= x) ans = mid, l = mid + 1; else r = mid - 1; } printf("%d\n", ans); } else if (op == 3) { scanf("%d%d", &a, &x); change(1, a, x); w[a] = x; } else if (op == 4) { scanf("%d%d%d", &a, &b, &x); printf("%d\n", query_pre(1, a, b, x)); } else { scanf("%d%d%d", &a, &b, &x); printf("%d\n", query_suc(1, a, b, x)); } } return 0; }
|