[Fun]压行版FhqTreap

· · 个人记录

丑的一批

struct Fhq {
    struct node {int ls, rs, val, sum, rnk;}nd[N + 5];int rt, cnt, x, y, z, t, ans;
    void ud(int c) {nd[c].sum = nd[nd[c].ls].sum + nd[nd[c].rs].sum + 1;}
    int nw(int d) {return nd[++cnt] = (node) {0, 0, d, 1, rand() << 15 | 114514}, cnt;}
    int mg(int L, int R) {return (!L || !R) ? L + R : ((nd[L].rnk < nd[R].rnk) ? (nd[L].rs = mg(nd[L].rs, R), ud(L), L) : (nd[R].ls = mg(L, nd[R].ls), ud(R), R));}
    void sp(int &L, int &R, int c, int d) { (!c) ? (L = R = 0) : ((nd[c].val <= d) ? (L = c, sp(nd[L].rs, R, nd[L].rs, d), 0) : (R = c, sp(L, nd[R].ls, nd[R].ls, d), 0), ud(c), 0);}
    void ins(int d) {sp(x, y, rt, d), rt = mg(mg(x, nw(d)), y);}
    void del(int d) {sp(x, y, rt, d), sp(x, z, x, d - 1), rt = mg(x, y);}
    int kth(int c, int d) {return d < (t = nd[nd[c].ls].sum + 1) ? kth(nd[c].ls, d) : (d == t ? c : kth(nd[c].rs, d - t));}
    int pre(int d) {return sp(x, y, rt, d - 1), ans = nd[kth(x, nd[x].sum)].val, rt = mg(x, y), ans;}
    int nxt(int d) {return sp(x, y, rt, d), ans = nd[kth(y, 1)].val, rt = mg(x, y), ans;}
    Fhq() : cnt(0), rt(0) {}
}fhq;

Just for fun