P2596 [ZJOI2006]书架(fhq treap)
P2596 [ZJOI2006]書架
我們用fhq treap來完成這一題
對于一個新插入的節(jié)點我們?nèi)?quán)值為其索引值,其所記錄的valuevaluevalue是其當前索引所在位置。
操作一:把索引為valuevaluevalue的點放到平衡樹前面,分別別得到三顆子樹x,y,zx, y, zx,y,z(前段子樹,索引為valuevaluevalue所代表的子樹,后段子樹),同時把val[y]val[y]val[y]修改成全局最小,然后按照順序merge(y,x,z)merge(y, x, z)merge(y,x,z)。
操作二:與操作一類似得到x,y,zx, y, zx,y,z,然后把val[y]val[y]val[y]改成全局最大,按照順序merge(x,z,y)merge(x, z, y)merge(x,z,y)。
操作三:t=0t = 0t=0不做操作,t=1t = 1t=1得到四顆子樹x,y,z,wx, y, z, wx,y,z,w(前段子樹,valuevaluevalue所代表的子樹,valuevaluevalue的后繼,后端子樹),交換信息,然后按照順序merge(x,z,y,w)merge(x, z, y, w)merge(x,z,y,w),t=?1t = -1t=?1得到四顆子樹x,y,z,wx, y, z, wx,y,z,w(前段子樹,valuevaluevalue的前驅(qū),valuevaluevalue所代表的子樹,后端子樹),交換信息,然后按照順序merge(x,z,y,w)merge(x, z, y, w)merge(x,z,y,w)。
操作四:按照值分裂成兩棵樹,然后輸出第一顆樹的大小即可。
操作五:直接查找第kkk大即可。
#include <bits/stdc++.h>using namespace std;const int N = 1e5 + 10;mt19937 rnd(233);int minn, maxn;struct Tree {int ls[N], rs[N], val[N], sz[N], key[N], root, cnt;void push_up(int rt) {sz[rt] = sz[ls[rt]] + sz[rs[rt]] + 1;}int new_node(int value, int pos) {val[value] = pos, sz[value] = 1, key[value] = rnd();return value;}void split(int rt, int value, int &x, int &y) {if (!rt) {x = y = 0;return ;}if (val[rt] <= value) {x = rt;split(rs[rt], value, rs[rt], y);}else {y = rt;split(ls[rt], value, x, ls[rt]);}push_up(rt);}int merge(int x, int y) {if (!x || !y) {return x | y;}if (key[x] < key[y]) {rs[x] = merge(rs[x], y);push_up(x);return x;}else {ls[y] = merge(x, ls[y]);push_up(y);return y;}}int get_num(int rt, int rank) {while (rt) {if (sz[ls[rt]] + 1 == rank) {break;}if (sz[ls[rt]] >= rank) {rt = ls[rt];}else {rank -= sz[ls[rt]] + 1;rt = rs[rt];}}return rt;}int get_num(int rank) {return get_num(root, rank);}void insert(int value, int pos) {int x, y;split(root, pos, x, y);root = merge(merge(x, new_node(value, pos)), y);}void update(int value, int op) {int x, y, z;split(root, val[value], x, z);split(x, val[value] - 1, x, y);if (op) {val[value] = --minn;root = merge(merge(y, x), z);}else {val[value] = ++maxn;root = merge(merge(x, z), y);}}void reverse(int value, int op) {if (!op) {return ;}if (op == 1) {int x, y, z, w;split(root, val[value], x, z);split(x, val[value] - 1, x, y);int t = get_num(z, 1);split(z, val[t], z, w);swap(val[y], val[z]);root = merge(merge(x, z), merge(y, w));}else {int x, y, z, w;split(root, val[value] - 1, x, z);split(z, val[value], z, w);int t = get_num(x, sz[x]);split(x, val[t] - 1, x, y);swap(val[y], val[z]);root = merge(merge(x, z), merge(y, w));}}int get_rank(int value) {int x, y, ans;split(root, val[value] - 1, x, y);ans = sz[x];root = merge(x, y);return ans;} }tree;int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);int n, m;scanf("%d %d", &n, &m);minn = 1, maxn = n;for (int i = 1, x; i <= n; i++) {scanf("%d", &x);tree.insert(x, i);}char op[10];for (int i = 1, s, t; i <= m; i++) {scanf("%s %d", op, &s);if (op[0] == 'T') {tree.update(s, 1);}else if (op[0] == 'B') {tree.update(s, 0);}else if (op[0] == 'I') {scanf("%d", &t);tree.reverse(s, t);}else if (op[0] == 'A') {printf("%d\n", tree.get_rank(s));}else {printf("%d\n", tree.get_num(s));}}return 0; }總結(jié)
以上是生活随笔為你收集整理的P2596 [ZJOI2006]书架(fhq treap)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 之前的路由器怎么设置才能用wifi路由器
- 下一篇: 内存贩子的小秘密! AMD专用条与ECC