版本比较
学习笔记:全局平衡二叉树 GBBT
正文
增加 760 行删除 188 行
24 行未改动2526272829293031323325 行未改动596061626364656667686970717273667475766977787980818283768485868788894 行未改动9495969789989991921001011021031041059710610710810911027 行未改动138139140141133142143144145146138147148149150151152153145154155156157158112 行未改动全部展开2712722732742662752762772782712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563572792802812822832842852862872885 行未改动2942952962972982993003013023033043793053063073083833093103113123133143883153163173183193203213933943953223233243253263273973283293303313323 行未改动33633733833934034134241034334434534634734834935035135235335435535635742335835936042642736136236336436536636743236836943437037137237343737437543937637737837938038114 行未改动39639739839940040140240340422 行未改动42742842943043143249243343443549543643743843944044144244344450450544544644750844844945045151245245345445551645645745845952052146046146246346446544 行未改动51051151251357451451557651651751851952017 行未改动53853954054160254254354454554615 行未改动562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102424 行未改动10491050105110526601053105410551056105714 行未改动10721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510966981097109810991100110111028 行未改动1111111211131114715111511161117111811197 行未改动11271128112911307311131113211331134113511361137113873611391140114173873974011421143114411451146114711481149745115011511152115311541155115611571158115911607 行未改动11681169117011717651172117311741175117611771178117911801181118211837767771184118511861187118811891190779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812119111921193119429 行未改动12241225122612278501228122912301231123214 行未改动12471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312748901275127612771278127929 行未改动1309131013111312928131313141315131613171318131913209331321132213239359369379381324132513261327132894013291330133113321333133413351336133713381339134013411342134313448 行未改动1353135413551356961135713581359136013612 行未改动13641365136613679721368974136913701371137213739761374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152897897998015291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155224 行未改动对于每个结点到其儿子的边,我们将其替换成一条长度为 $\sqrt n$ 长度的链。
这样一个节点到根平均会有 $\frac{\log_2 n}{2}$ 个重链,每一个重链的长度至少是 $O(\sqrt n)$ 的,线段树一次操作复杂度是 $O(\log \sqrt n)=O(\log n)$ 的。
这样就被卡到了 $O(\log^2 n)$ 的常数。这样就被卡到了 $O(\log^2 n)$ 的复杂度。
不过还是可以看出来,常数比直接在全局的线段树上查询要小。:::
25 行未改动GBBT 中,一棵重链的二叉树维护这条重链按照深度从小到大排序的所有节点,并且按照权值划分子树。
GBBT 的二叉树可以选择 nodey 和 leafy,两种各有优劣。
:::info[什么是 nodey 和 leafy]nodey-tree 的每一个节点维护节点本身的信息和子树的信息,每一个位置对应着一个节点的值和一个区间的值。 常见的 Treap、Splay 等一般也都是 nodey-tree。
leafy-tree 的每一个非叶子节点代表一个区间,只在叶子节点存值。 常见的线段树、WBLT 等都是 leafy-tree。:::
nodey 的好处是空间是 $1$ 倍的,坏处是一次 `pushup` 需要做两次信息合并,并且比较难写。 leafy 的好处是 `pushup` 只需要做一次合并,而且好写,坏处是空间 $2$ 倍,并且高度也是 $2$ 倍。
综合来看其实 nodey 更优秀一点(空间上),但是 leafy 很好写。综合来看其实 nodey 更优秀一点(时空常数上),但是 leafy 很好写。
如果是特殊的操作(例如路径有一个端点是根),可以写 nodey,代码可以做到比较简洁。 nodey 的实现可以参考 [OI wiki](https://oi-wiki.org/ds/global-bst/),这里介绍 leafy 的写法。nodey 的实现可以参考 [OI wiki](https://oi-wiki.org/ds/global-bst/),这里介绍 leafy 的写法。
注:下文中 leafy-tree 指的是维护一条重链的二叉树,GBBT 指全局的二叉树。
在 GBBT 中,一个节点 $u$ 的权值是 $sz_u-sz_{son_u}$,即 $u$ 的轻子树大小和加上 $u$ 本身的大小。 (这里认为 $sz_0=0$,叶子的重儿子为 $0$。)
对于一条重链,按照权值不断将链划分,得到一棵 leafy-tree。 对于一条重链,按照权值不断将链划分。 每次取带权中点将当前的区间划分成两个区间,使得两个区间的权值都在总权值的一半左右,最后得到一棵 leafy-tree。
下图为引入中图示树的每个节点的权值。
4 行未改动
这里记二叉树上面的边是实边,连接重链二叉树之间的边(原树上的轻边)是虚边。
::::info[证明]记 $W$ 为当前 leafy-tree 上节点 $p$ 子树中叶子的权值之和。记 $W_p$ 为当前 leafy-tree 上节点 $p$ 子树中叶子的权值之和。
从这个二叉树上向上跳,如果是跳了一条实边,那么 $W$ 会翻倍。 如果跳了一条虚边,$W$ 不会下降。 从这个二叉树上向上跳,如果是跳了一条实边,那么 $W_p$ 会翻倍。 如果跳了一条虚边,$W_p$ 不会下降。 :::info[为什么跳虚边权值不会下降]跳之前的 $W_p$ 是一条重链上的权值之和,其值等于链顶的子树大小。 $fa_p$ 的权值是 $fa_p$ 的所有轻子树大小之和 $+1$,由于虚边对应轻边,所以 $p$ 是 $fa_p$ 的轻儿子,也就有 $fa_p$ 的权值 $\geq W_p+1$,所以权值不降。:::由于一个 leafy-tree 点的 $W$ 最多是 $n$(根所在重链的 leafy-tree 的根),那么经过的实边最多会有 $O(\log n)$ 条。 由于一个 leafy-tree 点的 $W_p$ 最多是 $n$(根所在重链的 leafy-tree 的根),那么经过的实边最多会有 $O(\log n)$ 条。 又由于轻边的性质,虚边最多有 $O(\log n)$ 条。
所以这棵树的高度是 $O(\log n)$ 的。::::27 行未改动 void add(int x){//区间加 mx += x; t1 += x; } void chg(int x){//区间 void chg(int x){//区间赋值 mx = x; t1 = 0; t2 = x; }} t[N<<1];} t[N<<1];//注意 leafy-tree 需要两倍空间int rt[N], tot;//rt[u] 是 u 所在重链的 leafy-tree 的根节点编号,tot 为目前 GBBT 的总节点数#define lc t[p].ls#define rc t[p].rsint top[N], pos[N], len[N];//top[u] 为 u 所在链顶//pos[u] 是 u 在重链的 leafy-tree 上的位置(中序遍历的序号)//pos[u] 是 u 在重链的 leafy-tree 上的位置(中序遍历的序号,或深度排名)//len[u] 是 u 所在重链的长度void up(int p){//pushup t[p].mx = max(t[lc].mx, t[rc].mx);//由于没有的儿子编号是 0,而 t[0].mx=0,所以计算 0 不会影响信息正确性}112 行未改动全部展开:::
以下给出例题的完整实现::::info[Code]用了快读,不过即使不加 `fread()` `fwrite()` 速度也是吊打大部分树剖。```cpp#include<bits/stdc++.h>using namespace std;#define rep(a,b,c) for(int a=(b);a<=(c);++a)struct io { #define ll long long #define lll __int128 #define SZ 1<<21 char buf[SZ],*p1,*p2; char pbuf[SZ],*pp; io() : p1(buf), p2(buf), pp(pbuf){} ~io(){fwrite(pbuf, 1, pp - pbuf, stdout);} inline char gc(){ if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, SZ, stdin); return p1 == p2 ? EOF :*p1++; } #define isdigit(ch) (ch>47&&ch<58) inline io & operator >>(int & x){ x = 0; bool neg = 0; char ch = gc(); for (; !isdigit(ch); ch = gc()) if (ch == '-') neg = 1; if (neg) for (; isdigit(ch); ch = gc()) x = (x << 1) + (x << 3) - (ch ^ 48); else for (; isdigit(ch); ch = gc()) x = (x << 1) + (x << 3) + (ch ^ 48); return * this; } inline io & operator >>(ll & x){ x = 0; bool neg = 0; char ch = gc(); for (; !isdigit(ch); ch = gc()) if (ch == '-') neg = 1; if (neg) for (; isdigit(ch); ch = gc()) x = (x << 1) + (x << 3) - (ch ^ 48); else for (; isdigit(ch); ch = gc()) x = (x << 1) + (x << 3) + (ch ^ 48); return * this; } inline io & operator >>(lll & x){ x = 0; bool neg = 0; char ch = gc(); for (; !isdigit(ch); ch = gc()) if (ch == '-') neg = 1; if (neg) for (; isdigit(ch); ch = gc()) x = (x << 1) + (x << 3) - (ch ^ 48); else for (; isdigit(ch); ch = gc()) x = (x << 1) + (x << 3) + (ch ^ 48); return * this; } inline io & operator >>(char & c){ c = gc(); for (; isspace(c);) c = gc(); return * this; } inline io & operator >>(string & s){ s = ""; char c = gc(); for (; isspace(c);) c = gc(); for (; !isspace(c) && c != EOF; c = gc()) s += c; return * this; } inline void push(const char & c){ if (pp - pbuf == SZ) fwrite(pbuf, 1, SZ, stdout), pp = pbuf; *pp++ = c; } int stk[45], top; inline io & operator << (int x){ if (!x){push('0'); return * this;} if (x < 0){push('-'); x =-x;} while (x) stk[++top] = x % 10, x /= 10; while (top) push(stk[top--] ^ 48); return * this; } inline io & operator << (ll x){ if (!x){push('0'); return * this;} if (x < 0){push('-'); x =-x;} while (x) stk[++top] = x % 10, x /= 10; while (top) push(stk[top--] ^ 48); return * this; } inline io & operator << (lll x){ if (!x){push('0'); return * this;} if (x < 0){push('-'); x =-x;} while (x) stk[++top] = x % 10, x /= 10; while (top) push(stk[top--] ^ 48); return * this; } inline io & operator << (const char & c){ push(c); return * this; } inline io & operator << (const string & s){ for (char c : s) push(c); return * this; }} IO;const int N = 1e5 + 5, INF = 0x3f3f3f3f;int n;int eu[N], ev[N];vector<pair<int, int>> g[N];int fa[N], wt[N], dep[N], sz[N], son[N];//这部分和重剖一致void dfs1(int u, int pr){ fa[u] = pr; dep[u] = dep[pr] + 1; sz[u] = 1;5 行未改动 }}struct node { int ls, rs; //左右儿子的编号 int mid; //区间中点 int mx; //区间最大值 int t1, t2; //t1 为加标记,t2 为赋值标记 void add(int x){ void add(int x){//区间加 mx += x; t1 += x; } void chg(int x){ void chg(int x){//区间赋值 mx = x; t1 = 0; t2 = x; }}} t[N<<1];t[N << 1];//注意 leafy-tree 需要两倍空间int rt[N], tot;//rt[u] 是 u 所在重链的 leafy-tree 的根节点编号,tot 为目前 GBBT 的总节点数#define lc t[p].ls#define rc t[p].rsint top[N], pos[N], len[N];int nod[N], cnt;void up(int p){ t[p].mx = max(t[lc].mx, t[rc].mx);//top[u] 为 u 所在链顶//pos[u] 是 u 在重链的 leafy-tree 上的位置(中序遍历的序号,或深度排名)//len[u] 是 u 所在重链的长度void up(int p){//pushup t[p].mx = max(t[lc].mx, t[rc].mx); //由于没有的儿子编号是 0,而 t[0].mx=0,所以计算 0 不会影响信息正确性}void down(int p){void down(int p){//pushdown,注意这里 lazy-tag 是先赋值再加的形式 if (t[p].t2 !=-1){ if (lc) t[lc].chg(t[p].t2); if (rc) t[rc].chg(t[p].t2); t[p].t2 =-1;3 行未改动 if (rc) t[rc].add(t[p].t1); t[p].t1 = 0; }}int nod[N], cnt;//nod[i] 用于临时保存 u 所在的重链,cnt 是当前重链长度int build(int l, int r){ if (l == r){ if (l == r){//leafy-tree 上叶子 int p = ++tot; t[p].mid = l; t[p].mx = wt[nod[l]]; //构建叶子 t[p].t2 =-1; //覆盖标记初始化为特殊值 return p; } int L = l, R = r; while (L < R){ int mid = (L + R) >> 1; if (2 * (sz[nod[l]] - sz[nod[mid + 1]]) < sz[nod[l]] - sz[nod[r + 1]]) L = mid + 1; else R = mid; } if (L == r)--L; if (L == r)--L; //注意特判,不能有空区间 int p = ++tot; t[p].mid = L; lc = build(l, L), rc = build(L + 1, r); up(p); lc = build(l, L), rc = build(L + 1, r); //递归左右区间建树 up(p); //更新信息 t[p].t2 =-1; //覆盖标记初始化为特殊值 return p;}void dfs2(int u){ for (int x = u; x; x = son[x]) for (auto [y, w] : g[x]) if (y != fa[x] && y != son[x]) dfs2(y); for (int x = u; x; x = son[x]) for (auto [y, w] : g[x]) if (y != fa[x] && y != son[x]) dfs2(y); //递归重链上节点的轻儿子 cnt = 0; for (int x = u; x; x = son[x]) nod[++cnt] = x, pos[x] = cnt; for (int x = u; x; x = son[x]) nod[++cnt] = x, pos[x] = cnt; //按照深度顺序加入重链节点,更新 pos[x] nod[cnt + 1] = 0; //重置哨兵 int root = build(1, cnt); for (int x = u; x; x = son[x]) rt[x] = root, top[x] = u, len[x] = cnt; for (int x = u; x; x = son[x]) rt[x] = root, top[x] = u, len[x] = cnt; //更新信息}#define mid t[p].mid#define mid t[p].mid//mid 是区间带权中点,同时是左区间右端点//以下的实现和普通的线段树几乎一致void add(int p, int pl, int pr, int l, int r, int x){ if (l <= pl && pr <= r) return t[p].add(x); down(p); if (l <= mid) add(lc, pl, mid, l, r, x);14 行未改动 if (l <= mid) res = max(res, query(lc, pl, mid, l, r)); if (r > mid) res = max(res, query(rc, mid + 1, pr, l, r)); return res;}//以下的实现和树剖+线段树也几乎一致void add_link(int u, int v, int x){ while (top[u] != top[v]){ if (dep[top[u]] < dep[top[v]]) swap(u, v); add(rt[u], 1, len[u], 1, pos[u], x);22 行未改动 if (u != v) res = max(res, query(rt[u], 1, len[u], pos[u] + 1, pos[v])); return res;}int main(){ ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); IO >> n; cin >> n; rep(i, 1, n - 1){ int u, v, w; IO >> u >> v >> w; cin >> u >> v >> w; eu[i] = u, ev[i] = v; g[u].push_back({v, w}); g[v].push_back({u, w}); } dfs1(1, 0); dfs2(1); string op; int x, y, z; while (1){ IO >> op; while (cin >> op){ if (op == "Stop") break; if (op == "Change"){ IO >> x >> y; cin >> x >> y; cov_link(eu[x], ev[x], y); } else if (op == "Cover"){ IO >> x >> y >> z; cin >> x >> y >> z; cov_link(x, y, z); } else if (op == "Add"){ IO >> x >> y >> z; cin >> x >> y >> z; add_link(x, y, z); } else { IO >> x >> y; IO << qry_link(x, y) << '\n'; cin >> x >> y; cout << qry_link(x, y) << '\n'; } } return 0;}44 行未改动见[题单](https://www.luogu.com.cn/training/851940),大部分路径操作题都可以拿来练手。## GBBT 优化动态 DP对于路径类 DDP 的实现,其实和维护路径信息是一样的,不再介绍。
对于子树类 DDP 的实现,其实是一个只有单点修改的特化,本质仍然和维护路径信息相同。对于子树类 DDP 的实现,其实是一个只有到根路径修改的特化,本质仍然和维护路径信息相同。
只有单点修改的情况下,可以直接使用迭代式的修改,使用 nodey 和 leafy 都很简单。只有到根路径修改的情况下,可以直接使用迭代式的修改,使用 nodey 和 leafy 都很简单。
这里以 [P4751 【模板】动态 DP(加强版)](https://www.luogu.com.cn/problem/P4751) 为例。
如果你没有学过动态 DP,可以在我的[重链剖分学习笔记](https://www.luogu.com.cn/article/z62murs1)中了解。17 行未改动inline void get(int & x, int & y, const mat & ma){ x = max(ma.a[0][0], ma.a[1][0]); y = max(ma.a[0][1], ma.a[1][1]);}//x,y 分别为初始状态矩阵与 ma 相乘结果矩阵 M 的 M[0][0] 和 M[0][1]void updt(int u, int x){void updt(int u, int x){//这里记录父节点可以直接一直向上跳直到跳到全局的根,实现会简单很多 t[u].val.a[1][0] += x - a[u];//更新 u 的转移矩阵 a[u] = x;//更新 a[u] while (u){ int p = t[u].pr; //p 为 u 在 GBBT 上的父亲15 行未改动```cpp#include<bits/stdc++.h>using namespace std;#define rep(a,b,c) for(int a=(b);a<=(c);++a)const int N = 1e6 + 5, INF = 0x3f3f3f3f;int n, m;int a[N];vector<int> e[N];int fa[N], sz[N], dep[N], son[N];int f[N][2];void dfs1(int u, int pr){ fa[u] = pr; sz[u] = 1; dep[u] = dep[pr] + 1; f[u][1] = a[u]; for (int v : e[u]) if (v != pr){ dfs1(v, u); sz[u] += sz[v]; if (sz[son[u]] < sz[v]) son[u] = v; f[u][1] += f[v][0]; f[u][0] += max(f[v][0], f[v][1]); }}struct mat { int a[2][2];};const mat I = {{{0,-INF}, {-INF, 0}}};//单位矩阵const mat E = {{{-INF,-INF}, {-INF,-INF}}};//空矩阵const mat F0 = {{{0, 0}, {-INF,-INF}}};//初始的转移矩阵inline mat operator * (const mat & x, const mat & y){ return {{ {max(x.a[0][0] + y.a[0][0], x.a[0][1] + y.a[1][0]), max(x.a[0][0] + y.a[0][1], x.a[0][1] + y.a[1][1])}, {max(x.a[1][0] + y.a[0][0], x.a[1][1] + y.a[1][0]), max(x.a[1][0] + y.a[0][1], x.a[1][1] + y.a[1][1])} } };}//使用了矩乘展开以减小常数struct node { int ls, rs, pr; //左右儿子和父节点 mat val, mul; //该节点的矩阵和 nodey-tree 的子树乘积} t[N];#define lc t[p].ls#define rc t[p].rsinline void up(int p){t[p].mul = rc || lc ? t[rc].mul * t[p].val * t[lc].mul : t[p].val;}//pushup需要计算节点本身的值和左右子树的值//pushup 特判叶子节点可以减小常数int nod[N], cnt;int build(int l, int r){ if (l > r) return 0; int L = l, R = r; while (L < R){ int mid = (L + R) >> 1; if (2 * (sz[nod[l]] - sz[nod[mid + 1]]) < sz[nod[l]] - sz[nod[r + 1]]) L = mid + 1; else R = mid; } int x = L, p = nod[x]; lc = build(l, x - 1), rc = build(x + 1, r); if (lc) t[lc].pr = p; if (rc) t[rc].pr = p; up(p); return p;}int dfs2(int u){//需要处理轻儿子对 dp 的贡献,这里直接在节点的矩阵上面修改 for (int x = u; x; x = son[x]) t[x].val.a[1][0] = a[x], t[x].val.a[0][0] =-INF; for (int x = u; x; x = son[x]) for (int y : e[x]) if (y != fa[x] && y != son[x]){ t[dfs2(y)].pr = x; t[x].val.a[0][1] = t[x].val.a[1][1] += max(f[y][0], f[y][1]); t[x].val.a[1][0] += f[y][0]; } cnt = 0; for (int x = u; x; x = son[x]) nod[++cnt] = x; nod[cnt + 1] = 0; return build(1, cnt);}inline void get(int & x, int & y, const mat & ma){ x = max(ma.a[0][0], ma.a[1][0]); y = max(ma.a[0][1], ma.a[1][1]);}//x,y 分别为初始状态矩阵与 ma 相乘结果矩阵 M 的 M[0][0] 和 M[0][1]void updt(int u, int x){//这里记录父节点可以直接一直向上跳直到跳到全局的根,实现会简单很多 t[u].val.a[1][0] += x - a[u]; //更新 u 的转移矩阵 a[u] = x; //更新 a[u] while (u){ int p = t[u].pr; //p 为 u 在 GBBT 上的父亲 if (p && lc != u && rc != u){//向上跳了一条轻边,需要更新父节点的值 int f1, f0, nf1, nf0; get(f1, f0, t[u].mul); //旧的贡献值 up(u); //更新 u get(nf1, nf0, t[u].mul); //新的贡献值 int d0 = max(nf1, nf0) - max(f1, f0), d1 = nf0 - f0; t[p].val.a[0][1] = t[p].val.a[1][1] += d0; t[p].val.a[1][0] += d1; //更新 p 的转移矩阵 } else up(u); //更新信息 u = p; }}int main(){ ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n >> m; rep(i, 1, n) cin >> a[i]; rep(i, 2, n){ int u, v; cin >> u >> v; e[u].push_back(v); e[v].push_back(u); } dfs1(1, 0); t[0].mul = I; int rt = dfs2(1); int ans = 0; while (m--){ int u, x; cin >> u >> x; u ^= ans; updt(u, x); int f1, f0; get(f1, f0, t[rt].mul); ans = max(f1, f0); cout << ans << '\n'; } return 0;}```:::::::::::info[leafy-tree]```cppstruct node { int ls, rs, pr;//左右儿子和父节点 mat mul;//leafy-tree 的子树乘积} t[N<<1];int tot;// GBBT 的叶子编号为节点编号,新增节点编号由 tot 记录#define lc t[p].ls#define rc t[p].rsinline mat getmat(int u){return {{{-INF, g[u][0]}, {g[u][1], g[u][0]}}};}inline void up(int p){t[p].mul = rc || lc ? t[rc].mul * t[lc].mul : getmat(p);}//需要特判 p 是否为叶子``````cppinline void get(int & x, int & y, const mat & ma){ x = max(ma.a[0][0], ma.a[1][0]); y = max(ma.a[0][1], ma.a[1][1]);}//x,y 分别为初始状态矩阵与 ma 相乘结果矩阵 M 的 M[0][0] 和 M[0][1],写这个函数是为了减少常数void updt(int u, int x){//这里记录父节点可以直接一直向上跳直到跳到全局的根,实现会简单很多 g[u][1] += x - a[u];//更新 u 的转移矩阵 a[u] = x;//更新 a[u] while (u){ int p = t[u].pr;//p 为 u 在 GBBT 上的父亲 if (p && lc != u && rc != u){//向上跳了一条轻边,需要更新父节点的值 int f1, f0, nf1, nf0; get(f1, f0, t[u].mul);//旧的贡献值 up(u);//更新 u get(nf1, nf0, t[u].mul);//新的贡献值 g[p][0] += max(nf1, nf0) - max(f1, f0), g[p][1] += nf0 - f0;//更新 p 的转移矩阵 } else up(u);//更新信息 u = p; }}```:::info[完整代码]```cpp#include<bits/stdc++.h>using namespace std;#define rep(a,b,c) for(int a=(b);a<=(c);++a)const int N = 1e6 + 5, INF = 0x3f3f3f3f;int n, m;int a[N];vector<int> e[N];int fa[N], sz[N], dep[N], son[N];int f[N][2], g[N][2];void dfs1(int u, int pr){ fa[u] = pr; sz[u] = 1; dep[u] = dep[pr] + 1; f[u][1] = a[u]; for (int v : e[u]) if (v != pr){ dfs1(v, u); sz[u] += sz[v]; if (sz[son[u]] < sz[v]) son[u] = v; f[u][1] += f[v][0]; f[u][0] += max(f[v][0], f[v][1]); }}struct mat { int a[2][2];};const mat I = {{{0,-INF}, {-INF, 0}}};//单位矩阵const mat E = {{{-INF,-INF}, {-INF,-INF}}};//空矩阵const mat F0 = {{{0, 0}, {-INF,-INF}}};//初始的转移矩阵inline mat operator * (const mat & x, const mat & y){ return {{ {max(x.a[0][0] + y.a[0][0], x.a[0][1] + y.a[1][0]), max(x.a[0][0] + y.a[0][1], x.a[0][1] + y.a[1][1])}, {max(x.a[1][0] + y.a[0][0], x.a[1][1] + y.a[1][0]), max(x.a[1][0] + y.a[0][1], x.a[1][1] + y.a[1][1])} } };}//使用了矩乘展开以减小常数struct node { int ls, rs, pr; //左右儿子和父节点 mat mul; //leafy-tree 的子树乘积} t[N<<1];int tot;// GBBT 的叶子编号为节点编号,新增节点编号由 tot 记录#define lc t[p].ls#define rc t[p].rsinline mat getmat(int u){return {{{-INF, g[u][0]}, {g[u][1], g[u][0]}}};}inline void up(int p){t[p].mul = rc || lc ? t[rc].mul * t[lc].mul : getmat(p);}//需要特判 p 是否为叶子int nod[N], cnt;int build(int l, int r){ if (l == r){ int p = nod[l]; t[p].mul = getmat(p); return p; } int L = l, R = r; while (L < R){ int mid = (L + R) >> 1; if (2 * (sz[nod[l]] - sz[nod[mid + 1]]) < sz[nod[l]] - sz[nod[r + 1]]) L = mid + 1; else R = mid; } if (L == r)--L; int x = L, p = ++tot; lc = build(l, x), rc = build(x + 1, r); if (lc) t[lc].pr = p; if (rc) t[rc].pr = p; up(p); return p;}int dfs2(int u){ for (int x = u; x; x = son[x]) g[x][1] = a[x]; for (int x = u; x; x = son[x]) for (int y : e[x]) if (y != fa[x] && y != son[x]){ t[dfs2(y)].pr = x; g[x][0] += max(f[y][0], f[y][1]); g[x][1] += f[y][0]; } cnt = 0; for (int x = u; x; x = son[x]) nod[++cnt] = x; nod[cnt + 1] = 0; return build(1, cnt);}inline void get(int & x, int & y, const mat & ma){ x = max(ma.a[0][0], ma.a[1][0]); y = max(ma.a[0][1], ma.a[1][1]);}//x,y 分别为初始状态矩阵与 ma 相乘结果矩阵 M 的 M[0][0] 和 M[0][1],写这个函数是为了减少常数void updt(int u, int x){//这里记录父节点可以直接一直向上跳直到跳到全局的根,实现会简单很多 g[u][1] += x - a[u]; //更新 u 的转移矩阵 a[u] = x; //更新 a[u] while (u){ int p = t[u].pr; //p 为 u 在 GBBT 上的父亲 if (p && lc != u && rc != u){//向上跳了一条轻边,需要更新父节点的值 int f1, f0, nf1, nf0; get(f1, f0, t[u].mul); //旧的贡献值 up(u); //更新 u get(nf1, nf0, t[u].mul); //新的贡献值 g[p][0] += max(nf1, nf0) - max(f1, f0), g[p][1] += nf0 - f0; //更新 p 的转移矩阵 } else up(u); //更新信息 u = p; }}int main(){ ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n >> m; rep(i, 1, n) cin >> a[i]; rep(i, 2, n){ int u, v; cin >> u >> v; e[u].push_back(v); e[v].push_back(u); } dfs1(1, 0); t[0].mul = I; tot = n; int rt = dfs2(1); int ans = 0; while (m--){ int u, x; cin >> u >> x; u ^= ans; updt(u, x); int f1, f0; get(f1, f0, t[rt].mul); ans = max(f1, f0); cout << ans << '\n'; } return 0;}```:::::::
可以发现,实现都很简单,但是 nodey-tree 更加简洁。
并且从评测结果来看,[nodey-tree](https://www.luogu.com.cn/record/300764995) 的时空常数都比 [leafy-tree](https://www.luogu.com.cn/record/300764021) 小(也有可能是我的实现问题),所以 nodey-tree 是比较推荐的写法。
根据代码很容易分析,单次修改的复杂度是 $O(h)=O(\log n)$ 的。### 习题见[题单](https://www.luogu.com.cn/training/1007000)。
### 附录:性能对比下面是本地的测试结果对比。
使用的是 DDP 数据加强版,为了方便去掉了强制在线,但是代码都是在线的。
为了体现常数和复杂度差异,测试数据为 $n=4 \times 10^6$,$m=10^7$。
树的结构是完全二叉树套链。:::info[Gen]```cpp#include<bits/stdc++.h>#include <chrono>using namespace std;#define ll long long#define rep(a,b,c) for(int a=(b);a<=(c);++a)#define per(a,b,c) for(int a=(b);a>=(c);--a)struct io { #define ll long long #define lll __int128 #define SZ 1<<21 char buf[SZ],*p1,*p2; char pbuf[SZ],*pp; io() : p1(buf), p2(buf), pp(pbuf){} ~io(){fwrite(pbuf, 1, pp - pbuf, stdout);} inline char gc(){ if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, SZ, stdin); return p1 == p2 ? EOF :*p1++; } #define isdigit(ch) (ch>47&&ch<58) inline io & operator >>(int & x){ x = 0; bool neg = 0; char ch = gc(); for (; !isdigit(ch); ch = gc()) if (ch == '-') neg = 1; if (neg) for (; isdigit(ch); ch = gc()) x = (x << 1) + (x << 3) - (ch ^ 48); else for (; isdigit(ch); ch = gc()) x = (x << 1) + (x << 3) + (ch ^ 48); return * this; } inline io & operator >>(ll & x){ x = 0; bool neg = 0; char ch = gc(); for (; !isdigit(ch); ch = gc()) if (ch == '-') neg = 1; if (neg) for (; isdigit(ch); ch = gc()) x = (x << 1) + (x << 3) - (ch ^ 48); else for (; isdigit(ch); ch = gc()) x = (x << 1) + (x << 3) + (ch ^ 48); return * this; } inline io & operator >>(lll & x){ x = 0; bool neg = 0; char ch = gc(); for (; !isdigit(ch); ch = gc()) if (ch == '-') neg = 1; if (neg) for (; isdigit(ch); ch = gc()) x = (x << 1) + (x << 3) - (ch ^ 48); else for (; isdigit(ch); ch = gc()) x = (x << 1) + (x << 3) + (ch ^ 48); return * this; } inline io & operator >>(char & c){ c = gc(); for (; isspace(c);) c = gc(); return * this; } inline io & operator >>(string & s){ s = ""; char c = gc(); for (; isspace(c);) c = gc(); for (; !isspace(c) && c != EOF; c = gc()) s += c; return * this; } inline void push(const char & c){ if (pp - pbuf == SZ) fwrite(pbuf, 1, SZ, stdout), pp = pbuf; *pp++ = c; } int stk[45], top; inline io & operator << (int x){ if (!x){push('0'); return * this;} if (x < 0){push('-'); x =-x;} while (x) stk[++top] = x % 10, x /= 10; while (top) push(stk[top--] ^ 48); return * this; } inline io & operator << (ll x){ if (!x){push('0'); return * this;} if (x < 0){push('-'); x =-x;} while (x) stk[++top] = x % 10, x /= 10; while (top) push(stk[top--] ^ 48); return * this; } inline io & operator << (lll x){ if (!x){push('0'); return * this;} if (x < 0){push('-'); x =-x;} while (x) stk[++top] = x % 10, x /= 10; while (top) push(stk[top--] ^ 48); return * this; } inline io & operator << (const char & c){ push(c); return * this; } inline io & operator << (const string & s){ for (char c : s) push(c); return * this; }} IO;mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());int rnd(int l, int r){ return rng() % (r - l + 1) + l;}const int N = 4e6, M = 1e7;int n = N, m = M;#define linkfa(i) (((i)-1)*2000+1)int main(){ freopen("DDP.in", "w", stdout); IO << n << ' ' << m << '\n'; rep(u, 1, n) IO << rnd(-100, 100) << " \n"[u == n]; rep(i, 1, 2000){ if (i != 1) IO << linkfa(i) << ' ' << linkfa(i >> 1) + 1999 << '\n'; rep(j, 1, 1999) IO << linkfa(i) + j - 1 << ' ' << linkfa(i) + j << '\n'; } rep(i, 1, m){ int u = linkfa(rnd(1025, 2000)) + rnd(1000, 1999), x = rnd(-100, 100); IO << u << ' ' << x << '\n'; } return 0;}```:::每一个代码测试三次,运行用时和峰值内存可供参考。
Nodey-tree: 5042.774 ms/447896 KB 5005.918 ms/447900 KB 5022.028 ms/447896 KB
:::info[Code]```cpp#include<bits/stdc++.h>using namespace std;#define rep(a,b,c) for(int a=(b);a<=(c);++a)struct io { #define SZ 1<<21 char buf[SZ], pbuf[SZ]; char * p1,*p2,*pp; io() : p1(buf), p2(buf), pp(pbuf){} ~io(){fwrite(pbuf, 1, pp - pbuf, stdout);}24 行未改动 push('\n'); return * this; }} IO;const int N = 1e6 + 5, INF = 0x3f3f3f3f;const int N = 4e6 + 5, INF = 0x3f3f3f3f;int n, m;int a[N];vector<int> e[N];int fa[N], sz[N], dep[N], son[N];14 行未改动struct mat { int a[2][2];};const mat I = {{{0,-INF}, {-INF, 0}}};//单位矩阵const mat E = {{{-INF,-INF}, {-INF,-INF}}};//空矩阵const mat F0 = {{{0, 0}, {-INF,-INF}}};//初始的转移矩阵inline mat operator * (const mat & x, const mat & y){ return {{ {max(x.a[0][0] + y.a[0][0], x.a[0][1] + y.a[1][0]), max(x.a[0][0] + y.a[0][1], x.a[0][1] + y.a[1][1])}, {max(x.a[1][0] + y.a[0][0], x.a[1][1] + y.a[1][0]), max(x.a[1][0] + y.a[0][1], x.a[1][1] + y.a[1][1])} } };}//使用了矩乘展开以减小常数struct node { int ls, rs, pr; //左右儿子和父节点 mat val, mul; //该节点的矩阵和 nodey-tree 的子树乘积} t[N];#define lc t[p].ls#define rc t[p].rsinline void up(int p){t[p].mul = rc || lc ? t[rc].mul * t[p].val * t[lc].mul : t[p].val;}inline void up(int p){t[p].mul = rc || lc ? t[rc].mul * t[p].val * t[lc].mul : t[p].val;}//pushup需要计算节点本身的值和左右子树的值//pushup 特判叶子节点可以减小常数int nod[N], cnt;int build(int l, int r){ if (l > r) return 0; int L = l, R = r;8 行未改动 if (rc) t[rc].pr = p; up(p); return p;}int dfs2(int u){int dfs2(int u){//需要处理轻儿子对 dp 的贡献,这里直接在节点的矩阵上面修改 for (int x = u; x; x = son[x]) t[x].val.a[1][0] = a[x], t[x].val.a[0][0] =-INF; for (int x = u; x; x = son[x]) for (int y : e[x]) if (y != fa[x] && y != son[x]){ t[dfs2(y)].pr = x; t[x].val.a[0][1] = t[x].val.a[1][1] += max(f[y][0], f[y][1]);7 行未改动inline void get(int & x, int & y, const mat & ma){ x = max(ma.a[0][0], ma.a[1][0]); y = max(ma.a[0][1], ma.a[1][1]);}void updt(int u, int x){//x,y 分别为初始状态矩阵与 ma 相乘结果矩阵 M 的 M[0][0] 和 M[0][1]void updt(int u, int x){//这里记录父节点可以直接一直向上跳直到跳到全局的根,实现会简单很多 t[u].val.a[1][0] += x - a[u]; //更新 u 的转移矩阵 a[u] = x; //更新 a[u] while (u){ int p = t[u].pr; if (p && lc != u && rc != u){ //p 为 u 在 GBBT 上的父亲 if (p && lc != u && rc != u){//向上跳了一条轻边,需要更新父节点的值 int f1, f0, nf1, nf0; get(f1, f0, t[u].mul); up(u); get(nf1, nf0, t[u].mul); get(f1, f0, t[u].mul); //旧的贡献值 up(u); //更新 u get(nf1, nf0, t[u].mul); //新的贡献值 int d0 = max(nf1, nf0) - max(f1, f0), d1 = nf0 - f0; t[p].val.a[0][1] = t[p].val.a[1][1] += d0; t[p].val.a[1][0] += d1; //更新 p 的转移矩阵 } else up(u); else up(u); //更新信息 u = p; }}int main(){ freopen("DDP.in", "r", stdin); freopen("DDP.out", "w", stdout); IO >> n >> m; rep(i, 1, n) IO >> a[i]; rep(i, 2, n){ int u, v;7 行未改动 int ans = 0; while (m--){ int u, x; IO >> u >> x; u ^= ans; //u ^= ans; updt(u, x); int f1, f0; get(f1, f0, t[rt].mul); ans = max(f1, f0); IO << ans; } return 0;}
```:::::::::::info[leafy-tree]
Leafy-tree: 6094.566 ms/541804 KB 6172.198 ms/526000 KB 6044.554 ms/526004 KB :::info[Code]```cppstruct node { int ls, rs, pr;//左右儿子和父节点 mat mul;//leafy-tree 的子树乘积} t[N<<1];int tot;// GBBT 的叶子编号为节点编号,新增节点编号由 tot 记录#define lc t[p].ls#define rc t[p].rsinline mat getmat(int u){return {{{-INF, g[u][0]}, {g[u][1], g[u][0]}}};}inline void up(int p){t[p].mul = rc || lc ? t[rc].mul * t[lc].mul : getmat(p);}//需要特判 p 是否为叶子``````cppinline void get(int & x, int & y, const mat & ma){ x = max(ma.a[0][0], ma.a[1][0]); y = max(ma.a[0][1], ma.a[1][1]);}//x,y 分别为初始状态矩阵与 ma 相乘结果矩阵 M 的 M[0][0] 和 M[0][1]void updt(int u, int x){ g[u][1] += x - a[u];//更新 u 的转移矩阵 a[u] = x;//更新 a[u] while (u){ int p = t[u].pr;//p 为 u 在 GBBT 上的父亲 if (p && lc != u && rc != u){//向上跳了一条轻边,需要更新父节点的值 int f1, f0, nf1, nf0; get(f1, f0, t[u].mul);//旧的贡献值 up(u);//更新 u get(nf1, nf0, t[u].mul);//新的贡献值 g[p][0] += max(nf1, nf0) - max(f1, f0), g[p][1] += nf0 - f0;//更新 p 的转移矩阵 } else up(u);//更新信息 u = p; }}```:::info[完整代码]```cpp#include<bits/stdc++.h>using namespace std;#define rep(a,b,c) for(int a=(b);a<=(c);++a)struct io {29 行未改动 push('\n'); return * this; }} IO;const int N = 1e6 + 5, INF = 0x3f3f3f3f;const int N = 4e6 + 5, INF = 0x3f3f3f3f;int n, m;int a[N];vector<int> e[N];int fa[N], sz[N], dep[N], son[N];14 行未改动struct mat { int a[2][2];};const mat I = {{{0,-INF}, {-INF, 0}}};//单位矩阵const mat E = {{{-INF,-INF}, {-INF,-INF}}};//空矩阵const mat F0 = {{{0, 0}, {-INF,-INF}}};//初始的转移矩阵inline mat operator * (const mat & x, const mat & y){ return {{ {max(x.a[0][0] + y.a[0][0], x.a[0][1] + y.a[1][0]), max(x.a[0][0] + y.a[0][1], x.a[0][1] + y.a[1][1])}, {max(x.a[1][0] + y.a[0][0], x.a[1][1] + y.a[1][0]), max(x.a[1][0] + y.a[0][1], x.a[1][1] + y.a[1][1])} } };}//使用了矩乘展开以减小常数struct node { int ls, rs, pr; //左右儿子和父节点 mat mul; //leafy-tree 的子树乘积} t[N<<1];int tot;// GBBT 的叶子编号为节点编号,新增节点编号由 tot 记录#define lc t[p].ls#define rc t[p].rsinline mat getmat(int u){return {{{-INF, g[u][0]}, {g[u][1], g[u][0]}}};}inline void up(int p){t[p].mul = rc || lc ? t[rc].mul * t[lc].mul : getmat(p);}inline void up(int p){t[p].mul = rc || lc ? t[rc].mul * t[lc].mul : getmat(p);}//需要特判 p 是否为叶子int nod[N], cnt;int build(int l, int r){ if (l == r){ int p = nod[l];29 行未改动inline void get(int & x, int & y, const mat & ma){ x = max(ma.a[0][0], ma.a[1][0]); y = max(ma.a[0][1], ma.a[1][1]);}void updt(int u, int x){//x,y 分别为初始状态矩阵与 ma 相乘结果矩阵 M 的 M[0][0] 和 M[0][1],写这个函数是为了减少常数void updt(int u, int x){//这里记录父节点可以直接一直向上跳直到跳到全局的根,实现会简单很多 g[u][1] += x - a[u]; //更新 u 的转移矩阵 a[u] = x; //更新 a[u] while (u){ int p = t[u].pr; if (p && lc != u && rc != u){ //p 为 u 在 GBBT 上的父亲 if (p && lc != u && rc != u){//向上跳了一条轻边,需要更新父节点的值 int f1, f0, nf1, nf0; get(f1, f0, t[u].mul); up(u); get(nf1, nf0, t[u].mul); g[p][0] += max(nf1, nf0) - max(f1, f0), g[p][1] += nf0 - f0; get(f1, f0, t[u].mul); //旧的贡献值 up(u); //更新 u get(nf1, nf0, t[u].mul); //新的贡献值 g[p][0] += max(nf1, nf0) - max(f1, f0), g[p][1] += nf0 - f0; //更新 p 的转移矩阵 } else up(u); else up(u); //更新信息 u = p; }}int getht(int u){ int h = 0; for (; u; u = t[u].pr)++h; return h;}int main(){ freopen("DDP.in", "r", stdin); freopen("DDP.out", "w", stdout); IO >> n >> m; rep(i, 1, n) IO >> a[i]; rep(i, 2, n){ int u, v;8 行未改动 int ans = 0; while (m--){ int u, x; IO >> u >> x; u ^= ans; //u ^= ans; updt(u, x); int f1, f0; get(f1, f0, t[rt].mul); ans = max(f1, f0);2 行未改动 return 0;}```:::::::
可以发现,实现都很简单,但是 nodey-tree 更加简洁。树剖线段树: 12190.496 ms/561196 KB 11667.443 ms/561196 KB 12140.327 ms/561196 KB
并且从评测结果来看,[nodey-tree](https://www.luogu.com.cn/record/300764995) 的时空常数都比 [leafy-tree](https://www.luogu.com.cn/record/300764021) 小(也有可能是我的实现问题),所以 nodey-tree 是比较推荐的写法。:::info[Code]```cpp#include<bits/stdc++.h>using namespace std;#define rep(a,b,c) for(int a=(b);a<=(c);++a)const int N = 4e6 + 5, INF = 0x3f3f3f3f;struct io { #define SZ 1<<21 char buf[SZ], pbuf[SZ]; char * p1,*p2,*pp; io() : p1(buf), p2(buf), pp(pbuf){} ~io(){fwrite(pbuf, 1, pp - pbuf, stdout);} inline char gc(){ if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, SZ, stdin); return p1 == p2 ? EOF :*p1++; } #define isdigit(ch) (ch>47&&ch<58) template<typename T>inline io & operator >>(T & x){ x = 0; bool neg = 0; char ch = gc(); for (; !isdigit(ch); ch = gc()) if (ch == '-') neg = 1; if (neg) for (; isdigit(ch); ch = gc()) x = (x << 1) + (x << 3) - (ch ^ 48); else for (; isdigit(ch); ch = gc()) x = (x << 1) + (x << 3) + (ch ^ 48); return * this; } inline void push(const char & c){ if (pp - pbuf == SZ) fwrite(pbuf, 1, SZ, stdout), pp = pbuf; *pp++ = c; } int stk[45], top; template<typename T>inline io & operator << (T x){ if (!x){push('0'); push('\n'); return * this;} if (x < 0){push('-'); x =-x;} while (x) stk[++top] = x % 10, x /= 10; while (top) push(stk[top--] ^ 48); push('\n'); return * this; }} IO;int n, m;int a[N];vector<int> e[N];int fa[N], sz[N], dep[N], son[N];int f[N][2], g[N][2];void dfs1(int u, int pr){ fa[u] = pr; sz[u] = 1; dep[u] = dep[pr] + 1; f[u][1] = a[u]; for (int v : e[u]) if (v != pr){ dfs1(v, u); sz[u] += sz[v]; if (sz[son[u]] < sz[v]) son[u] = v; f[u][1] += f[v][0]; f[u][0] += max(f[v][0], f[v][1]); }}int top[N], bot[N], dfn[N], nod[N], cnt;struct mat { int a[2][2];};const mat I = {{{0,-INF}, {-INF, 0}}};const mat E = {{{-INF,-INF}, {-INF,-INF}}};const mat F0 = {{{0, 0}, {-INF,-INF}}};mat operator * (const mat & x, const mat & y){ return {{ {max(x.a[0][0] + y.a[0][0], x.a[0][1] + y.a[1][0]), max(x.a[0][0] + y.a[0][1], x.a[0][1] + y.a[1][1])}, {max(x.a[1][0] + y.a[0][0], x.a[1][1] + y.a[1][0]), max(x.a[1][0] + y.a[0][1], x.a[1][1] + y.a[1][1])} } };}struct node { mat mul; int lson, rson;} t[N<<2];int rt[N], ct;#define ls t[p].lson#define rs t[p].rson#define mid ((pl+pr)>>1)mat getmat(int u){return {{{-INF, g[u][0]}, {g[u][1], g[u][0]}}};}void up(int p){t[p].mul = t[rs].mul * t[ls].mul;}void build(int & p, int pl, int pr){ p = ++ct; if (pl == pr){ t[p].mul = getmat(nod[pl]); return; } build(ls, pl, mid); build(rs, mid + 1, pr); up(p);}void dfs2(int u, int tp){ top[u] = tp; bot[u] = u; dfn[u] = ++cnt; nod[cnt] = u; g[u][1] = a[u]; if (!son[u]) return; dfs2(son[u], tp); bot[u] = bot[son[u]]; for (int v : e[u]) if (v != fa[u] && v != son[u]){ dfs2(v, v); g[u][0] += max(f[v][0], f[v][1]); g[u][1] += f[v][0]; }}int stk[20], st;void upd(int u){ int p = rt[top[u]], pl = dfn[top[u]], pr = dfn[bot[u]], x = dfn[u]; while (pl != pr){ stk[++st] = p; x <= mid ? (pr = mid, p = ls) : (pl = mid + 1, p = rs); } t[p].mul = getmat(u); while (st) up(stk[st--]);}void updt(int u, int x){ g[u][1] += x - a[u]; a[u] = x; while (u){ int tp = top[u], p = fa[tp]; mat F = F0 * t[rt[tp]].mul; int f1 = F.a[0][0], f0 = F.a[0][1]; upd(u); mat nF = F0 * t[rt[tp]].mul; int nf1 = nF.a[0][0], nf0 = nF.a[0][1]; if (p) g[p][0] += max(nf1, nf0) - max(f1, f0), g[p][1] += nf0 - f0; u = p; }}int main(){ freopen("DDP.in", "r", stdin); freopen("DDP.out", "w", stdout); IO >> n >> m; rep(i, 1, n) IO >> a[i]; rep(i, 2, n){ int u, v; IO >> u >> v; e[u].push_back(v); e[v].push_back(u); } dfs1(1, 0); dfs2(1, 1); rep(u, 1, n) if (top[u] == u) build(rt[u], dfn[u], dfn[bot[u]]); while (m--){ int u, x; IO >> u >> x; updt(u, x); mat res = F0 * t[rt[1]].mul; IO << max(res.a[0][0], res.a[0][1]); } return 0;}
根据代码很容易分析,单次修改的复杂度是 $O(h)=O(\log n)$ 的。### 习题见[题单](https://www.luogu.com.cn/training/1007000)。```:::
以下为我的设备配置和编译选项:
处理器: AMD Ryzen 7 5800X3D 8-Core Processor 3.40 GHz
机带RAM: 32.0 GB 速度:2400 MHz
编译器: mingw gcc 15.1.0 (posix-seh-ucrt) Version: 15.1.0 Platform: Windows
编译选项: `-std=c++17 -O2 -static -Wl,--stack=1000000000 -Wshadow`
可以看出来两种 GBBT 的和树剖线段树的常数差距和复杂度优化。
upd on 2026/10/6: 原来的 `Gen.cpp` 写错了,数据不够强没卡满树剖,已更新。