[BZOJ2733][HNOI2012]永无乡(平衡树+启发式合并)
来源:互联网 发布:2016男女对唱新歌网络 编辑:程序博客网 时间:2024/06/06 00:40
首先,构建出
而对于合并两棵平衡树,可以使用启发式合并,即把点数较少的平衡树中的点暴力合并到点数较多的平衡树上。实际上,这样操作,每个点被合并的次数不超过
证明:由于启发式合并是把点数较少的平衡树合并到点数较多的平衡树上,所以一个点每被合并一次,所在的平衡树点数就至少扩大一倍,即被合并的次数不超过
对于询问操作,就是平衡树的「求排名为
代码:
#include <cmath>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;inline int read() { int res = 0; bool bo = 0; char c; while (((c = getchar()) < '0' || c > '9') && c != '-'); if (c == '-') bo = 1; else res = c - 48; while ((c = getchar()) >= '0' && c <= '9') res = (res << 3) + (res << 1) + (c - 48); return bo ? ~res + 1 : res;}inline char get() { char c; while ((c = getchar()) != 'B' && c != 'Q'); return c;}const int N = 1e5 + 5;int n, m, q, f[N], fa[N], lc[N], rc[N], val[N], sze[N];int cx(int x) { if (f[x] != x) f[x] = cx(f[x]); return f[x];}bool zm(int x, int y) { int ix = cx(x), iy = cx(y); if (ix != iy) {f[iy] = ix; return 1;} return 0;}int which(int x) {return rc[fa[x]] == x;}void upt(int x) { sze[x] = 1; if (lc[x]) sze[x] += sze[lc[x]]; if (rc[x]) sze[x] += sze[rc[x]];}void rotate(int x) { int y = fa[x], z = fa[y], b = lc[y] == x ? rc[x] : lc[x]; if (z) (lc[z] == y ? lc[z] : rc[z]) = x; fa[x] = z; fa[y] = x; if (b) fa[b] = y; if (lc[y] == x) rc[x] = y, lc[y] = b; else lc[x] = y, rc[y] = b; upt(y); upt(x);}void splay(int x) { while (fa[x]) { if (fa[fa[x]]) { if (which(x) == which(fa[x])) rotate(fa[x]); else rotate(x); } rotate(x); } upt(x);}void ins(int rt, int z) { int x = rt, y; while (x) { y = x; sze[x]++; if (val[z] < val[x]) x = lc[x]; else x = rc[x]; } if (val[z] < val[y]) lc[fa[z] = y] = z; else rc[fa[z] = y] = z;}void dfs(int y, int x) { if (lc[x]) dfs(y, lc[x]); if (rc[x]) dfs(y, rc[x]); fa[x] = lc[x] = rc[x] = 0; sze[x] = 1; ins(y, x);}void merg(int x, int y) { if (!zm(x, y)) return; splay(x); splay(y); if (sze[x] < sze[y]) swap(x, y); dfs(x, y);}int query(int x, int y) { splay(x); while (x) { int tmp = lc[x] ? sze[lc[x]] : 0; if (y == tmp + 1) return x; else if (y <= tmp) x = lc[x]; else x = rc[x], y -= tmp + 1; } return -1;}int main() { int i, x, y; n = read(); m = read(); char op; for (i = 1; i <= n; i++) f[i] = i, sze[i] = 1, val[i] = read(); while (m--) x = read(), y = read(), merg(x, y); q = read(); while (q--) { op = get(); x = read(); y = read(); if (op == 'B') merg(x, y); else printf("%d\n", query(x, y)); } return 0;}
阅读全文
0 0
- BZOJ2733 [HNOI2012]永无乡 平衡树启发式合并
- [BZOJ2733]HNOI2012永无乡|平衡树启发式合并
- [BZOJ2733][HNOI2012][启发式合并][平衡树]永无乡
- [平衡树+启发式合并] BZOJ2733: [HNOI2012]永无乡
- [BZOJ2733][HNOI2012]永无乡(平衡树+启发式合并)
- bzoj2733 [HNOI2012]永无乡(splay启发式合并)
- [BZOJ2733][HNOI2012]永无乡(并查集+splay启发式合并)
- bzoj2733: [HNOI2012]永无乡 splay+启发式合并
- 【bzoj2733】【HNOI2012】【永无乡】【treap+启发式合并】
- 【BZOJ2733】[HNOI2012]永无乡【启发式合并】【Splay】
- BZOJ2733: [HNOI2012]永无乡 Splay启发式合并
- [BZOJ2733][HNOI2012]永无乡-Treap-启发式合并
- BZOJ 2733 2733: [HNOI2012]永无乡 平衡树启发式合并
- BZOJ2733 永无乡 [启发式合并]
- BZOJ2733 永无乡 【splay启发式合并】
- bzoj 2733: [HNOI2012]永无乡(线段树启发式合并)
- bzoj 2733: [HNOI2012]永无乡(线段树启发式合并)
- BZOJ 2733: [HNOI2012]永无乡(线段树启发式合并)
- MATLAB绘制图形
- CodeForces
- MATLAB编程(1),循环和选择
- 轻量级自动化运维工具fabric使用与安装
- 小明学C++第五篇:操作系统
- [BZOJ2733][HNOI2012]永无乡(平衡树+启发式合并)
- 深入浅出Java MVC(Model View Controller) ---- (JSP + servlet + javabean实例)
- XML
- 1299最长上升子序列
- poj 3107 Godfather(树形dp)
- 数据结构--主席树(不带修改)
- 忘记mysql的root的密码
- captain_paul大佬
- 嵌入式系统硬件组成-3.5.1