【LG3250】[HNOI2016]網絡
阿新 • • 發佈:2019-02-28
wap line ons 題解 git www 平衡樹 top getch 。
【LG3250】[HNOI2016]網絡
題面
洛谷
題解
\(30pts\)
對於\(m\leq 2000\),直接判斷一下這個個點是否斷掉一個交互,沒斷掉的裏面取\(max\)即可,復雜度\(O(m^2\log n)\)。
另\(20pts\)
對於無刪除操作的,用線段樹維護,
我們將一條路徑的補集全部打上那條路徑重要度的標記,這樣我們斷一條邊時直接單點查詢即可。
據說這樣子再改改可以變成一種樹剖加堆的做法,但是好像現在在bzoj被卡了
\(100pts\)
想到二分答案,若所有權值\(\geq mid\)的路徑都過\(x\),那麽\(ans<mid\),反之若有不過\(x\)的,則說明\(ans\geq mid\)
這個可以用樹套樹維護,因為樹狀數組套線段樹會炸空間,所以就線段樹套平衡樹,復雜度\(O(m\log ^3n)\)。
考慮整體二分,那麽我們按照套路按時間加入,將一條路徑樹上差分,
碰到一次查詢直接看經過它的路徑條數即可,復雜度\(O(m\log^2 n)\)。
代碼
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> using namespace std; inline int gi() { register int data = 0, w = 1; register char ch = 0; while (!isdigit(ch) && ch != '-') ch = getchar(); if (ch == '-') w = -1, ch = getchar(); while (isdigit(ch)) data = 10 * data + ch - '0', ch = getchar(); return w * data; } const int MAX_N = 1e5 + 5, MAX_M = 2e5 + 5; struct Graph { int to, next; } e[MAX_N << 1]; int fir[MAX_N], e_cnt; void clearGraph() { memset(fir, -1, sizeof(fir)); e_cnt = 0; } void Add_Edge(int u, int v) { e[e_cnt] = (Graph){v, fir[u]}; fir[u] = e_cnt++; } int dep[MAX_N], fa[MAX_N], size[MAX_N], top[MAX_N], son[MAX_N]; int L[MAX_N], R[MAX_N], tim; void dfs1(int x) { dep[x] = dep[fa[x]] + 1, size[x] = 1; for (int i = fir[x]; ~i; i = e[i].next) { int v = e[i].to; if (v == fa[x]) continue; fa[v] = x, dfs1(v), size[x] += size[v]; if (size[son[x]] < size[v]) son[x] = v; } } void dfs2(int x, int tp) { top[x] = tp, L[x] = ++tim; if (son[x]) dfs2(son[x], tp); for (int i = fir[x]; ~i; i = e[i].next) { int v = e[i].to; if (v == son[x] || v == fa[x]) continue; dfs2(v, v); } R[x] = tim; } int LCA(int u, int v) { while (top[u] != top[v]) { if (dep[top[u]] < dep[top[v]]) swap(u, v); u = fa[top[u]]; } return dep[u] < dep[v] ? u : v; } int N, M; struct Option { int op, a, b, lca, v, id; } q[MAX_M], lq[MAX_M], rq[MAX_M]; int ans[MAX_M]; int c[MAX_N]; inline int lb(int x) { return x & -x; } int sum(int x) { int res = 0; while (x) res += c[x], x -= lb(x); return res; } void add(int x, int v) { while (x <= N) c[x] += v, x += lb(x); } void Div(int lval, int rval, int st, int ed) { if (st > ed) return ; if (lval == rval) { for (int i = st; i <= ed; i++) if (q[i].op == 2) ans[q[i].id] = lval; return ; } int mid = (lval + rval) >> 1, lt = 0, rt = 0, cnt = 0; for (int i = st; i <= ed; i++) { if (q[i].op != 2) { if (q[i].v > mid) { int v = q[i].op ? -1 : 1; cnt += v; add(L[q[i].a], v), add(L[q[i].b], v), add(L[q[i].lca], -v); if (fa[q[i].lca]) add(L[fa[q[i].lca]], -v); rq[++rt] = q[i]; } else lq[++lt] = q[i]; } else { int res = sum(R[q[i].a]) - sum(L[q[i].a] - 1); if (cnt <= res) lq[++lt] = q[i]; else rq[++rt] = q[i]; } } for (int i = 1; i <= rt; i++) { if (rq[i].op != 2) { if (rq[i].v > mid) { int v = rq[i].op ? 1 : -1; add(L[rq[i].a], v), add(L[rq[i].b], v), add(L[rq[i].lca], -v); if (fa[rq[i].lca]) add(L[fa[rq[i].lca]], -v); } } } for (int i = 1; i <= lt; i++) q[st + i - 1] = lq[i]; for (int i = 1; i <= rt; i++) q[st + lt - 1 + i] = rq[i]; Div(lval, mid, st, st + lt - 1); Div(mid + 1, rval, st + lt, ed); } int h[MAX_N], cnt; int main () { #ifndef ONLINE_JUDGE freopen("cpp.in", "r", stdin); #endif clearGraph(); N = gi(), M = gi(); for (int i = 1; i < N; i++) { int u = gi(), v = gi(); Add_Edge(u, v), Add_Edge(v, u); } dfs1(1), dfs2(1, 1); int q_cnt = 0; for (int i = 1; i <= M; i++) { int op = gi(); if (op == 0) { int a = gi(), b = gi(); q[i] = (Option){op, a, b, LCA(a, b), gi(), 0}; h[++cnt] = q[i].v; } if (op == 1) q[i] = q[gi()], q[i].op = 1; if (op == 2) q[i] = (Option){op, gi(), 0, 0, 0, ++q_cnt}; } h[++cnt] = -1; sort(&h[1], &h[cnt + 1]); cnt = unique(&h[1], &h[cnt + 1]) - h - 1; for (int i = 1; i <= M; i++) if (q[i].op != 2) q[i].v = lower_bound(&h[1], &h[cnt + 1], q[i].v) - h; Div(1, cnt, 1, M); for (int i = 1; i <= q_cnt; i++) printf("%d\n", h[ans[i]]); return 0; }
【LG3250】[HNOI2016]網絡