4 条题解
-
-3
} return 0; } } } cout << nxt(x) << endl; if (op == 6) { cout << pre(x) << endl; if (op == 5) { } cout << tree[kth(x + 1)].val << endl; if (op == 4) { } cout << rnk(x) - 1 << endl; if (op == 3) { } del(x); if (op == 2) { } insert(x); if (op == 1) { cin >> op >> x; int op, x; for (int i = 1; i <= n; i++) { cin >> n; insert(-Inf); insert(Inf); int main() { int n, root, tot; int Get(int x) { //返回x是左儿子还是右儿子 return x == tree[tree[x].fa].ch[1]; } void pushup(int x) { //更新size if (!x) return; tree[x].size = tree[tree[x].ch[0]].size + tree[tree[x].ch[1]].size + tree[x].cnt; } void rotate(int x) { //将x旋转到父亲之上 int y = tree[x].fa, z = tree[y].fa, chk = Get(x); tree[y].ch[chk] = tree[x].ch[chk ^ 1]; if (tree[x].ch[chk ^ 1]) tree[tree[x].ch[chk ^ 1]].fa = y; tree[x].ch[chk ^ 1] = y; tree[y].fa = x; if (z) tree[z].ch[y == tree[z].ch[1]] = x; tree[x].fa = z; pushup(y); pushup(x); } void splay(int x, int k) { //将x旋转到k的儿子 while (tree[x].fa != k) { int y = tree[x].fa, z = tree[y].fa; if (z != k) { if (Get(x) == Get(y)) rotate(y); else rotate(x); } rotate(x); } if (k == 0) root = x; } void insert(int x) { //插入x int cur = root; int fa = 0; while (cur) { if (tree[cur].val == x) { tree[cur].cnt++; pushup(cur); pushup(fa); splay(cur, 0); return; } fa = cur; cur = tree[cur].ch[x > tree[cur].val]; } cur = ++tot; tree[cur].val = x; tree[cur].fa = fa; tree[cur].cnt = 1; tree[fa].ch[x > tree[fa].val] = cur; pushup(cur); pushup(fa); splay(cur, 0); } int rnk(int x) { //返回x的排名 int res = 0, cur = root; while (cur) { if (x < tree[cur].val) cur = tree[cur].ch[0]; else { res += tree[tree[cur].ch[0]].size; if (x == tree[cur].val) { splay(cur, 0); return res + 1; } else { res += tree[cur].cnt; cur = tree[cur].ch[1]; } } } return res + 1; } int kth(int x) { //返回排名为x的数 int cur = root; while (cur) { if (x <= tree[tree[cur].ch[0]].size) cur = tree[cur].ch[0]; else { x -= tree[tree[cur].ch[0]].size; if (x <= tree[cur].cnt) { splay(cur, 0); return cur; } else { x -= tree[cur].cnt; cur = tree[cur].ch[1]; } } } return -1; } int pre(int x) { //返回x的前驱 int res = -Inf; int cur = root; while (cur) { if (tree[cur].val < x) { res = max(res, tree[cur].val); cur = tree[cur].ch[1]; } else cur = tree[cur].ch[0]; } return res; } int nxt(int x) { //返回x的后继 int res = Inf; int cur = root; while (cur) { if (tree[cur].val > x) { res = min(res, tree[cur].val); cur = tree[cur].ch[0]; } else cur = tree[cur].ch[1]; } return res; } void find(int x) { //将x旋转至根节点 int cur = root; while (cur) { if (tree[cur].val == x) { splay(cur, 0); return; } cur = tree[cur].ch[x > tree[cur].val]; } } void del(int x) { find(x); int L = tree[root].ch[0], R = tree[root].ch[1]; while (tree[L].ch[1]) L = tree[L].ch[1]; while (tree[R].ch[0]) R = tree[R].ch[0]; splay(L, 0); splay(R, L); if (tree[tree[R].ch[0]].cnt > 1) { tree[tree[R].ch[0]].cnt--; pushup(tree[R].ch[0]); pushup(R); pushup(L); } else { tree[R].ch[0] = 0; pushup(R); pushup(L); } } } tree[N]; int size; int ch[2], fa, val, cnt; struct node { const int N = 1e5 + 5, Inf = 1e9 + 1; using namespace std; #include <bits/stdc++.h>
- 1
信息
- ID
- 6965
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 7
- 标签
- 递交数
- 181
- 已通过
- 36
- 上传者