Qtree4

· · 题解

点分治应该都会了吧,我就不写了。

长链剖分

我们将原树剖作很多重链,并在每条重链上各建一颗线段树,而不是整体建一棵线段树。

我们规定对于一条重链上的一条子链,其左端点为这条子链上 dfn 最小的点,右端点为这条子链上 \text{dfn} 最大的点。

对于线段树上一个节点,维护:

$\text{RmaxDis}$,表示区间右端点所能到达最远的白点的距离。 $\text{MaxDis}$,表示任意两个 LCA 在区间中的白点(这两个点并不一定在区间内)间的最大距离。 `pushup` 就可以这样写: ```cpp void pushup(int p, int l, int r) { int mid = (l + r) >> 1; t[p].lmax = max(t[t[p].ls].lmax, dep[dfn[mid + 1]] - dep[dfn[l]] + t[t[p].rs].lmax); t[p].rmax = max(t[t[p].rs].rmax, dep[dfn[r]] - dep[dfn[mid]] + t[t[p].ls].rmax); t[p].maxx = max(max(t[t[p].ls].maxx, t[t[p].rs].maxx), t[t[p].ls].rmax + t[t[p].rs].lmax + dep[dfn[mid + 1]] - dep[dfn[mid]]); } ``` 跟最大子段和比较类似。 再对于每个点 $x$ 都用一个数据结构维护 $x$ 向下第一步不走重边能到的所有白点的极大距离,可以使用 $lmax$ + 深度距离差来求。 这个数据结构需要支持插入、删除、求最大值,所以选择堆。 线段树维护好了。 然后在考虑修改。设修改点为 $x$,先修改它所在的链上,再一直向上跳更新。 我们用一个大根堆 $heapans$ 维护每条重链线段树的 $MaxDis$,便可 $O(1)$ 查询。 代码: ```cpp #include <bits/stdc++.h> #include <ext/pb_ds/priority_queue.hpp> using namespace std; using namespace __gnu_pbds; using ll = long long; namespace Main { const int N = 100000 + 5; int n, m; struct edge { int nxt, to, val; } e[N * 2]; int col[N]; int h[N], num; void addedge(int x, int y, int z) { e[++num].nxt = h[x]; e[num].to = y; e[num].val = z; h[x] = num; } int fa[N], top[N], son[N], dep[N]; int len[N], cnt, sz[N]; int id[N], dfn[N]; void dfs1(int x, int f) { fa[x] = f; sz[x] = 1; for (int i = h[x]; i; i = e[i].nxt) { int y = e[i].to; if (y == f) continue; dep[y] = dep[x] + e[i].val; dfs1(y, x); sz[x] += sz[y]; if (sz[y] > sz[son[x]]) son[x] = y; } } void dfs2(int x, int f) { top[x] = f; ++len[top[x]]; id[x] = ++cnt; dfn[cnt] = x; if (son[x]) dfs2(son[x], f); for (int i = h[x]; i; i = e[i].nxt) { int y = e[i].to; if (y == fa[x] || y == son[x]) continue; dfs2(y, y); } } struct Heap { multiset<int,greater<int> > s; void insert(int v) { s.insert(v); } void del(int v) { multiset<int, greater<int> > :: iterator it=s.lower_bound(v); if(it!=s.end()) s.erase(it); } int top() { if(s.empty()) return -0x3f3f3f3f; return *s.begin(); } }; Heap ans, stk[N]; struct tree { int ls, rs; int maxx, lmax, rmax; } t[N << 2]; int TreeCnt; int rt[N]; void pushup(int p, int l, int r) { int mid = (l + r) >> 1; t[p].lmax = max(t[t[p].ls].lmax, dep[dfn[mid + 1]] - dep[dfn[l]] + t[t[p].rs].lmax); t[p].rmax = max(t[t[p].rs].rmax, dep[dfn[r]] - dep[dfn[mid]] + t[t[p].ls].rmax); t[p].maxx = max(max(t[t[p].ls].maxx, t[t[p].rs].maxx), t[t[p].ls].rmax + t[t[p].rs].lmax + dep[dfn[mid + 1]] - dep[dfn[mid]]); } void build(int p, int l, int r) { if (l == r) { int x = dfn[l]; for (int i = h[x]; i; i = e[i].nxt) { int y = e[i].to; if (y == fa[x] || y == son[x]) continue; stk[x].insert(t[rt[y]].lmax + e[i].val); } // int jk = 0; // while (stk[x].s.lower_bound(1) != stk[x].s.end()) ++jk, stk[x].s.erase(1); // cout << "jk = " << jk << '\n'; int max1 = stk[x].top(); stk[x].del(max1); int max2 = stk[x].top(); stk[x].insert(max1); t[p].lmax = t[p].rmax = max(max1, 0); t[p].maxx = max(0, max(max1, max1 + max2)); // cout << "#####" << max1 << ' ' << max2 << '\n'; return; } int mid = (l + r) >> 1; if (!t[p].ls)t[p].ls = ++TreeCnt; build(t[p].ls, l, mid); if (!t[p].rs)t[p].rs = ++TreeCnt; build(t[p].rs, mid + 1, r); pushup(p, l, r); } void change(int p, int l, int r, int x, int y) { if (l == r) { if (x != y) stk[x].insert(t[rt[y]].lmax + dep[y] - dep[x]); int max1 = stk[x].top(); stk[x].del(max1); int max2 = stk[x].top(); stk[x].insert(max1); if (col[x]) { t[p].lmax = t[p].rmax = max1; t[p].maxx = max1 + max2; } else { t[p].lmax = t[p].rmax = max(max1, 0); t[p].maxx = max(0, max(max1, max1 + max2)); } return; } int mid = (l + r) >> 1; if (id[x] <= mid) change(t[p].ls, l, mid, x, y); else change(t[p].rs, mid + 1, r, x, y); pushup(p, l, r); } void update(int x) { int lst = x; while (x) { int top_max1 = t[rt[top[x]]].maxx; if (fa[top[x]]) { stk[fa[top[x]]].del(t[rt[top[x]]].lmax + dep[top[x]] - dep[fa[top[x]]]); } change(rt[top[x]], id[top[x]], id[top[x]] + len[top[x]] - 1, x, lst); int top_max2 = t[rt[top[x]]].maxx; // cout << "Del = " << top_max2 <<'\n'; if (top_max1 != top_max2) { ans.del(top_max1); ans.insert(top_max2); } lst = top[x]; x = fa[top[x]]; } } int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> n; for (int i = 1, x, y, z; i < n; ++i) { cin >> x >> y >> z; addedge(x, y, z); addedge(y, x, z); } dfs1(1, 0); dfs2(1, 1); // return 0; // for (int i = 1; i <= n; ++i) { // cout << id[i] << '\n'; // } // return 0; for (int i = n; i >= 1; --i) { int x = dfn[i]; if (x == top[x]) { if (!rt[x]) rt[x] = ++TreeCnt; build(rt[x], id[x], id[x] + len[x] - 1); ans.insert(t[rt[x]].maxx); } } // return 0; cin >> m; char op; int Cnt = n; for (int ti = 1, x; ti <= m; ++ti) { cin >> op; if (op == 'C') { cin >> x; col[x] ^= 1; if (col[x] == 0) ++Cnt; else --Cnt; update(x); } else { if (Cnt == 0) cout << "They have disappeared.\n"; else cout << ans.top() << '\n'; } } return 0; } } int main() { Main :: main(); return 0; } ```