大白上的treap模板

来源:互联网 发布:浦口行知基地图片 编辑:程序博客网 时间:2024/04/29 03:21

#include <cstdio>#include <cstdlib>#include<cstring>typedef long long LL;const int MAXN = 20005;const int MAXL = 60005;const int MAXC = 500005;struct Node {Node *ch[2];int r;int v;int s;Node(int v = 0) : v(v) {ch[0] = ch[1] = NULL;r = rand();s = 1;}int cmp(int x) {return x == v ? -1 : (x < v ? 0 : 1);}void maintain() {s = 1;if (ch[0] != NULL)s += ch[0]->s;if (ch[1] != NULL)s += ch[1]->s;}};struct Line {int u, v;void input() {scanf("%d%d", &u, &v);}};struct Command {char type;int x, v;Command() {}Command(char type, int x, int v) : type(type), x(x), v(v) {}};Node *root[MAXN];Line line[MAXL];Command command[MAXC];int weight[MAXN];int n, m, q;int p[MAXN];int Move[MAXL];int query_cnt;long long  query_tot;void rotate(Node *&o, int d) {Node *k = o->ch[d ^ 1];o->ch[d ^ 1] = k->ch[d];k->ch[d] = o;o->maintain();k->maintain();o = k;}void insert(Node *&o, int x) //插入{if (o == NULL) {o = new Node(x);}else {int d = x < o->v ? 0 : 1;insert(o->ch[d], x);if (o->ch[d]->r > o->r)rotate(o, d ^ 1);}o->maintain();}void remove(Node *&o, int x) {int d = o->cmp(x);if (d == -1) {Node *u = o;if (o->ch[0] != NULL && o->ch[1] != NULL) {int d2 = o->ch[0]->r > o->ch[1]->r ? 1 : 0;rotate(o, d2);remove(o->ch[d2], x);}else {if (o->ch[0] == NULL)o = o->ch[1];elseo = o->ch[0];delete u;}}elseremove(o->ch[d], x);if (o != NULL)o->maintain();}void remove_tree(Node *&o) //释放空间{if (o->ch[0] != NULL)remove_tree(o->ch[0]);if (o->ch[1] != NULL)remove_tree(o->ch[1]);delete o;o = NULL;}int find(int x) {return p[x] == x ? x : (p[x] = find(p[x]));}void merge(Node *&src, Node *&des) {if (src->ch[0] != NULL)merge(src->ch[0], des);if (src->ch[1] != NULL)merge(src->ch[1], des);insert(des, src->v);delete src;src = NULL;}void add_edge(int i) {int x = find(line[i].u);int y = find(line[i].v);if (x != y) {if (root[x]->s < root[y]->s) {p[x] = y;merge(root[x], root[y]);}else {p[y] = x;merge(root[y], root[x]);}}}void change_weight(int x, int v) {int o = find(x);remove(root[o], weight[x]);insert(root[o], v);weight[x] = v;}int kth_small(Node* o, int k) //数字小优先{if (o == NULL || k <= 0 || k > o->s)return 0;int s = (o->ch[0] == NULL ? 0 : o->ch[0]->s);if (k == s + 1) return o->v;else if (k <= s) return kth_small(o->ch[0], k);else return kth_small(o->ch[1], k - s - 1);}int kth_big(Node *o, int k)//数字大优先{if (o == NULL || k <= 0 || k > o->s)return 0;int s = o->ch[1] == NULL ? 0 : o->ch[1]->s;if (s + 1 == k)return o->v;else {if (s >= k)return kth_big(o->ch[1], k);elsereturn kth_big(o->ch[0], k - s - 1);}}void query(int x, int k) {x = find(x);++query_cnt;query_tot += kth_big(root[x], k);}void init() {query_cnt = 0;query_tot = 0;memset(Move, 0, sizeof(Move));}


0 0