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
| # include<cstdio> # include<iostream> # include<algorithm>
# define lid id<<1 # define rid id<<1|1 # define mid ((A[id].l+A[id].r)>>1)
using namespace std;
const int INF = 0x7fffffff; const int N = 50005; int n, m, a[N], opt, ql, qr, qk, qpos, tmp;
struct splay{ int size, times, val, son[2], fa; }B[(int)4e6];
struct segTree{ int l, r, root; }A[N<<2];
struct OPT_splay{ int cnt; inline void pushup(int x){ if(x){ B[x].size = B[x].times; if(B[x].son[0]) B[x].size += B[B[x].son[0]].size; if(B[x].son[1]) B[x].size += B[B[x].son[1]].size; } } inline void rotate(int x, int kind){ int y = B[x].fa, z = B[y].fa, a = B[y].son[kind], b = B[x].son[kind], c = B[x].son[!kind]; B[x].fa = z, B[x].son[kind] = y; B[y].fa = x, B[y].son[!kind] = b; B[z].son[B[z].son[1] == y] = x; B[b].fa = y; pushup(y), pushup(x); } inline void splay(int x, int goal, int id){ if(x == goal) return; while(B[x].fa != goal){ int y = B[x].fa, z = B[y].fa; int isrson1 = B[y].son[1] == x, isrson2 = B[z].son[1] == y; if(z == goal) rotate(x, !isrson1); else{ if(isrson1 == isrson2) rotate(y, !isrson2); else rotate(x, !isrson1); rotate(x, !isrson2); } } if(goal == 0) A[id].root = x; } inline int newNode(int val, int fa){ cnt++; B[cnt].fa = fa; B[cnt].val = val; B[cnt].size = B[cnt].times = 1; B[cnt].son[0] = B[cnt].son[1] = 0; return cnt; } inline int getPre(int x, int id){ int now = A[id].root, res = -INF; while(now){ if(B[now].val < x){ res = max(res, B[now].val); now = B[now].son[1]; } else now = B[now].son[0]; } return res; } inline int getSub(int x, int id){ int now = A[id].root, res = INF; while(now){ if(B[now].val > x){ res = min(res, B[now].val); now = B[now].son[0]; } else now = B[now].son[1]; } return res; } inline int select(int x, int id){ int now = A[id].root; while(now){ if(B[now].val == x) break; else if(B[now].val > x) now = B[now].son[0]; else if(B[now].val < x) now = B[now].son[1]; } if(!now) return -1; return now; } inline int getRank(int x, int id){ if(select(x, id) != -1) splay(select(x, id), 0, id); else splay(select(getSub(x, id), id), 0, id); return B[B[A[id].root].son[0]].size; } inline void insert(int val, int id){ splay(select(getPre(val, id), id), 0, id); splay(select(getSub(val, id), id), A[id].root, id); int t = B[B[A[id].root].son[1]].son[0]; if(!t) B[B[A[id].root].son[1]].son[0] = newNode(val, B[A[id].root].son[1]); else B[t].times++, B[t].size++; pushup(B[A[id].root].son[1]); pushup(A[id].root); } inline void del(int val, int id){ splay(select(getPre(val, id), id), 0, id); splay(select(getSub(val, id), id), A[id].root, id); int t = B[B[A[id].root].son[1]].son[0]; if(!t || B[t].times == 0) return; B[t].times--, B[t].size--; if(B[t].times == 0) B[B[A[id].root].son[1]].son[0] = 0; pushup(B[A[id].root].son[1]); pushup(A[id].root); } }Splay;
struct OPT_segTree{ void build(int id, int l, int r){ A[id].root = Splay.newNode(-INF, 0); B[A[id].root].son[1] = Splay.newNode(INF, A[id].root); A[id].l = l, A[id].r = r; if(A[id].l == A[id].r) return; build(lid, l, mid); build(rid, mid+1, r); } void insert(int id, int pos, int val){ Splay.insert(val, id); if(A[id].l == A[id].r) return; if(pos <= mid) insert(lid, pos, val); else insert(rid, pos, val); } int getRank(int id, int l, int r, int x){ if(A[id].l == l && A[id].r == r) return Splay.getRank(x, id) - 1; if(r <= mid) return getRank(lid, l, r, x); else if(l > mid) return getRank(rid, l, r, x); else return getRank(lid, l, mid, x) + getRank(rid, mid+1, r, x); } int getKth(int l, int r, int k){ int ans = -1, L = 0, R = 1e8; while(L <= R){ int Mid = (L + R) >> 1; int t1 = getRank(1, l, r, Mid) + 1; int t2 = getRank(1, l, r, Mid+1); if(t1 <= k && k <= t2){ ans = Mid; break; } if(t2 < k) L = Mid+1; else if(t1 > k) R = Mid-1; } return ans; } void modify(int id, int pos, int val){ Splay.del(a[pos], id); Splay.insert(val, id); if(A[id].l == A[id].r) return; if(pos <= mid) modify(lid, pos, val); else modify(rid, pos, val); } int getPre(int id, int l, int r, int x){ if(A[id].l == l && A[id].r == r) return Splay.getPre(x, id); if(r <= mid) return getPre(lid, l, r, x); else if(l > mid) return getPre(rid, l, r, x); else return max(getPre(lid, l, mid, x), getPre(rid, mid+1, r, x)); } int getSub(int id, int l, int r, int x){ if(A[id].l == l && A[id].r == r) return Splay.getSub(x, id); if(r <= mid) return getSub(lid, l, r, x); else if(l > mid) return getSub(rid, l, r, x); else return min(getSub(lid, l, mid, x), getSub(rid, mid+1, r, x)); } }Seg;
int main(){ scanf("%d%d", &n, &m); Seg.build(1, 1, n); for(int i = 1; i <= n; i++){ scanf("%d", &a[i]); Seg.insert(1, i, a[i]); } while(m--){ scanf("%d", &opt); if(opt == 3) scanf("%d%d", &qpos, &qk); else scanf("%d%d%d", &ql, &qr, &qk); if(opt == 1) printf("%d\n", Seg.getRank(1, ql, qr, qk) + 1); else if(opt == 2) printf("%d\n", Seg.getKth(ql, qr, qk)); else if(opt == 3) Seg.modify(1, qpos, qk), a[qpos] = qk; else if(opt == 4) printf("%d\n", Seg.getPre(1, ql, qr, qk)); else if(opt == 5) printf("%d\n", Seg.getSub(1, ql, qr, qk)); } return 0; }
|