「SDOI2013」森林
传送门
树上主席树 + 启发式合并
锻炼码力,没什么好说的。
细节见代码。
参考代码:
#include <algorithm> #include <cstdio> #define rg register #define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout) using std ::sort; using std ::swap; using std ::unique; using std ::lower_bound; inline char _getchar() { static char buf[100000], *p1 = buf, *p2 = buf; return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++; } template < class T > inline void read(T& s) { s = 0; rg int f = 0; rg char c = _getchar(); while ('0' > c || c > '9') f |= c == '-', c = _getchar(); while ('0' <= c && c <= '9') s = s * 10 + c - 48, c = _getchar(); s = f ? -s : s; } const int _ = 8e4 + 5; int tot, head[_]; struct Edge { int ver, nxt; } edge[_ << 1]; inline void Add_edge(int u, int v) { edge[++tot] = (Edge) { v, head[u] }, head[u] = tot; } int n, m, q, vis[_], a[_], X0, X[_], top[_], Siz[_], fa[18][_], dep[_]; int tt, rt[_]; struct node { int lc, rc, cnt; } t[_ * 266]; inline void build(int& p, int l = 1, int r = X0) { p = ++tt, t[p].cnt = 0; if (l == r) return ; int mid = (l + r) >> 1; build(t[p].lc, l, mid), build(t[p].rc, mid + 1, r); } inline void update(int& p, int q, int x, int l = 1, int r = X0) { p = ++tt, t[p] = t[q], ++t[p].cnt; if (l == r) return ; rg int mid = (l + r) >> 1; if (x <= mid) update(t[p].lc, t[q].lc, x, l, mid); else update(t[p].rc, t[q].rc, x, mid + 1, r); } inline int query(int u, int v, int lca, int flca, int k, int l = 1, int r = X0) { if (l == r) return l; rg int mid = (l + r) >> 1; rg int num = t[t[u].lc].cnt + t[t[v].lc].cnt - t[t[lca].lc].cnt - t[t[flca].lc].cnt; if (num >= k) return query(t[u].lc, t[v].lc, t[lca].lc, t[flca].lc, k, l, mid); else return query(t[u].rc, t[v].rc, t[lca].rc, t[flca].rc, k - num, mid + 1, r); } inline void dfs(int u, int f, int topf, int d) { vis[u] = 1; dep[u] = d, fa[0][u] = f, top[u] = topf, ++Siz[topf]; rt[u] = 0, update(rt[u], rt[f], a[u]); for (rg int i = 1; i <= 16; ++i) fa[i][u] = fa[i - 1][fa[i - 1][u]]; for (rg int i = head[u]; i; i = edge[i].nxt) if (edge[i].ver != f) dfs(edge[i].ver, u, topf, d + 1); } inline int LCA(int x, int y) { if (dep[x] < dep[y]) swap(x, y); for (rg int i = 16; ~i; --i) if (dep[fa[i][x]] >= dep[y]) x = fa[i][x]; if (x == y) return x; for (rg int i = 16; ~i; --i) if (fa[i][x] != fa[i][y]) x = fa[i][x], y = fa[i][y]; return fa[0][x]; } int main() { int T; read(T); read(n), read(m), read(q); for (rg int i = 1; i <= n; ++i) read(a[i]), X[i] = a[i]; sort(X + 1, X + n + 1); X0 = unique(X + 1, X + n + 1) - X - 1; for (rg int i = 1; i <= n; ++i) a[i] = lower_bound(X + 1, X + X0 + 1, a[i]) - X; build(rt[0]); for (rg int x, y, o = 1; o <= m; ++o) read(x), read(y), Add_edge(x, y), Add_edge(y, x); for (rg int i = 1; i <= n; ++i) if (!vis[i]) dfs(i, 0, i, 1); for (rg int ans = 0, x, y, k, lca, o = 1; o <= q; ++o) { rg char C = _getchar(); while (C != 'Q' && C != 'L') C = _getchar(); read(x), x ^= ans; read(y), y ^= ans; if (C == 'Q') { read(k), k ^= ans, lca = LCA(x, y); ans = X[query(rt[x], rt[y], rt[lca], rt[fa[0][lca]], k)]; printf("%d\n", ans); } else { Add_edge(x, y), Add_edge(y, x); if (Siz[top[x]] < Siz[top[y]]) swap(x, y); dfs(y, x, top[x], dep[x] + 1); } } return 0; }
来源:https://www.cnblogs.com/zsbzsb/p/12231685.html