正文
增加 18 行删除 18 行
87 行未改动88899091929394959696 行未改动1931941951961971981992002016 行未改动20820921021121221321421521611 行未改动22822923023123223323423523640 行未改动2772782792802812822832842857 行未改动29329429529629729829930030129 行未改动33133233333433533633733833911 行未改动35135235335435535635735835936036136236336436536636736836937037137237311 行未改动3853863873883893903913923933943953963973983994004014024034044054064074082 行未改动4114124134144154164174184199 行未改动42943043143243343443543643734 行未改动472473474475476477478479480124 行未改动全部展开87 行未改动inline void treeadd(ll x, ll y, ll val){while(top[x] != top[y]) // x和y不在一条重链上{if(dis[top[x]] < dis[top[y]]) swap(x, y);// x更新为所在重链开头更深的节点,确保每次向上跳不会错过update(dfn[top[x]], dfn[x], val, 1, n, 1); // 线段树的修改x = fa[top[x]]; // x到所在重链的开头的父节点}96 行未改动}void pushdown(int x){if(!tag[x]) return;tag[lson(x)] = tag[rson(x)] = tag[x];// 每次修改前都pushdown了,新标记在老标记后,颜色应该为新标记的颜色lef[lson(x)] = lef[rson(x)] = rit[lson(x)] = rit[rson(x)] = tag[x];sum[lson(x)] = sum[rson(x)] = sum[x] = 1;tag[x] = 0;6 行未改动sum[p] = 1, lef[p] = rit[p] = a[dfn[l]];return;}int mid = (l + r) >> 1;seg[p][0] = ++idx, build(l, mid, idx),seg[p][1] = ++idx, build(mid + 1, r, idx);pushup(p);}void update(int l, int r, int nl, int nr, int p, int col)11 行未改动}int query(int l, int r, int nl, int nr, int p){if(!l && !r) return 0;if(nl >= l && nr <= r) return sum[p];int mid = (nl + nr) >> 1, ans = 0, flag1 = 0, flag2 = 0;pushdown(p);if(mid >= l) ans += query(l, r, nl, mid, lson(p)), flag1 = 1;if(mid + 1 <= r) ans += query(l, r, mid + 1, nr, rson(p)), flag2 = 1;40 行未改动for(int i = 1; i <= n; i++) scanf("%d", &a[i]);for(int i = 1; i < n; i++){scanf("%d%d", &u, &v);edge[u].push_back(v), edge[v].push_back(u);}dfs1(1, 0), dfs2(1, 1);for(int i = 1; i <= n; i++)if(st[i] && ed[i]) num[i] = ++idx, build(st[i], ed[i], idx);7 行未改动}else printf("%d\n", ask(u, v));}return 0;}```------------#### 3. 树上二分29 行未改动利用 dfs 序完成路径加,单点查,复杂度 $O(n\log^2n)$```cppstruct bit // dfs序{ll sum[N << 2];void add(ll x, ll y, ll val){11 行未改动}}tree;void dfs(ll x){size[x] = 1, dep[x] = dep[fa[x]] + 1, in[x] = ++idx;if(son[x][0]) fa[son[x][0]] = x, dfs(son[x][0]), size[x] += size[son[x][0]];if(son[x][1]) fa[son[x][1]] = x, dfs(son[x][1]), size[x] += size[son[x][1]];mson[x] = !(!son[x][1] || size[son[x][0]] >= size[son[x][1]]);ans += son[x][mson[x]];out[x] = ++idx;}void dfs2(ll x, ll tp){top[x] = tp, dfn[x] = mxdfn[x] = ++cnt, lin[cnt] = x;if(son[x][mson[x]]) dfs2(son[x][mson[x]], tp);if(son[x][!mson[x]]) dfs2(son[x][!mson[x]], son[x][!mson[x]]);mxdfn[x] = max({mxdfn[x], mxdfn[son[x][0]], mxdfn[son[x][1]]});}ll jump(ll x, ll step) // 从 x向上跳 step步{while(step >= dep[x] - dep[top[x]] + 1) step -= dep[x] - dep[top[x]] + 1, x = fa[top[x]];return lin[dfn[x] - step];}11 行未改动tree.add(1, nw, -1), book[nw] = 1;while(1){ll l = 0, r = dep[nw] - dep[root], siz = tree.ask(root);while(l < r) // 二分{ll mid = (l + r + 1) >> 1;if(tree.ask(jump(nw, mid)) <= (siz - 1) / 2) l = mid;else r = mid - 1;}ll nx = jump(nw, l), ny = son[fa[nx]][!wh(nx)];ll sizx = tree.ask(nx), sizy = tree.ask(ny);if(!book[ny] && (sizx < sizy || book[nx]) && wh(nx) == mson[fa[nx]])ans += ny - nx, mson[fa[nx]] = wh(ny); // 换了重儿子,更新答案else if(book[nx] && book[ny]){ans -= son[fa[nx]][mson[fa[nx]]];break;}if(nx == root || book[nx]) break; // 如果没有更新或为叶子节点,退出,把贡献减去root = nx;}print(ans), putchar('\n');}2 行未改动/*当 siz_{nw} \le siz_{root}/2 时,nw才是 root轻儿子二分找到路径上深度最浅的 nwnw不一定是root的子节点,但没关系,nw到 root路径上的点 siz已经 >siz_{root}/2,不可能再成为它们父节点的轻儿子把 root设为 nw,重复*/```#### 4. 边权转点权9 行未改动```cppint lson(int x) {return x << 1;}int rson(int x) {return x << 1 | 1;}void pushup(int p){sum[p] = sum[lson(p)] + sum[rson(p)];mn[p] = min(mn[lson(p)], mn[rson(p)]), mx[p] = max(mx[lson(p)], mx[rson(p)]);}34 行未改动int mid = (nl + nr) >> 1;pushdown(p);if(mid >= l) modify2(l, r, nl, mid, lson(p));if(mid < r) modify2(l, r, mid + 1, nr, rson(p));pushup(p);}int querysum(int l, int r, int nl, int nr, int p){if(l <= nl && nr <= r) return sum[p];124 行未改动全部展开87 行未改动88899091929394959696 行未改动1931941951961971981992002016 行未改动20820921021121221321421521611 行未改动22822923023123223323423523640 行未改动2772782792802812822832842857 行未改动29329429529629729829930030129 行未改动33133233333433533633733833911 行未改动35135235335435535635735835936036136236336436536636736836937037137237311 行未改动3853863873883893903913923933943953963973983994004014024034044054064074082 行未改动4114124134144154164174184199 行未改动42943043143243343443543643734 行未改动472473474475476477478479480124 行未改动全部展开87 行未改动inline void treeadd(ll x, ll y, ll val){while(top[x] != top[y]) // x和y不在一条重链上{if(dis[top[x]] < dis[top[y]]) swap(x, y);// x更新为所在重链开头更深的节点,确保每次向上跳不会错过update(dfn[top[x]], dfn[x], val, 1, n, 1); // 线段树的修改x = fa[top[x]]; // x到所在重链的开头的父节点}96 行未改动}void pushdown(int x){if(!tag[x]) return;tag[lson(x)] = tag[rson(x)] = tag[x];// 每次修改前都pushdown了,新标记在老标记后,颜色应该为新标记的颜色lef[lson(x)] = lef[rson(x)] = rit[lson(x)] = rit[rson(x)] = tag[x];sum[lson(x)] = sum[rson(x)] = sum[x] = 1;tag[x] = 0;6 行未改动sum[p] = 1, lef[p] = rit[p] = a[dfn[l]];return;}int mid = (l + r) >> 1;seg[p][0] = ++idx, build(l, mid, idx),seg[p][1] = ++idx, build(mid + 1, r, idx);pushup(p);}void update(int l, int r, int nl, int nr, int p, int col)11 行未改动}int query(int l, int r, int nl, int nr, int p){if(!l && !r) return 0;if(nl >= l && nr <= r) return sum[p];int mid = (nl + nr) >> 1, ans = 0, flag1 = 0, flag2 = 0;pushdown(p);if(mid >= l) ans += query(l, r, nl, mid, lson(p)), flag1 = 1;if(mid + 1 <= r) ans += query(l, r, mid + 1, nr, rson(p)), flag2 = 1;40 行未改动for(int i = 1; i <= n; i++) scanf("%d", &a[i]);for(int i = 1; i < n; i++){scanf("%d%d", &u, &v);edge[u].push_back(v), edge[v].push_back(u);}dfs1(1, 0), dfs2(1, 1);for(int i = 1; i <= n; i++)if(st[i] && ed[i]) num[i] = ++idx, build(st[i], ed[i], idx);7 行未改动}else printf("%d\n", ask(u, v));}return 0;}```------------#### 3. 树上二分29 行未改动利用 dfs 序完成路径加,单点查,复杂度 $O(n\log^2n)$```cppstruct bit // dfs序{ll sum[N << 2];void add(ll x, ll y, ll val){11 行未改动}}tree;void dfs(ll x){size[x] = 1, dep[x] = dep[fa[x]] + 1, in[x] = ++idx;if(son[x][0]) fa[son[x][0]] = x, dfs(son[x][0]), size[x] += size[son[x][0]];if(son[x][1]) fa[son[x][1]] = x, dfs(son[x][1]), size[x] += size[son[x][1]];mson[x] = !(!son[x][1] || size[son[x][0]] >= size[son[x][1]]);ans += son[x][mson[x]];out[x] = ++idx;}void dfs2(ll x, ll tp){top[x] = tp, dfn[x] = mxdfn[x] = ++cnt, lin[cnt] = x;if(son[x][mson[x]]) dfs2(son[x][mson[x]], tp);if(son[x][!mson[x]]) dfs2(son[x][!mson[x]], son[x][!mson[x]]);mxdfn[x] = max({mxdfn[x], mxdfn[son[x][0]], mxdfn[son[x][1]]});}ll jump(ll x, ll step) // 从 x向上跳 step步{while(step >= dep[x] - dep[top[x]] + 1) step -= dep[x] - dep[top[x]] + 1, x = fa[top[x]];return lin[dfn[x] - step];}11 行未改动tree.add(1, nw, -1), book[nw] = 1;while(1){ll l = 0, r = dep[nw] - dep[root], siz = tree.ask(root);while(l < r) // 二分{ll mid = (l + r + 1) >> 1;if(tree.ask(jump(nw, mid)) <= (siz - 1) / 2) l = mid;else r = mid - 1;}ll nx = jump(nw, l), ny = son[fa[nx]][!wh(nx)];ll sizx = tree.ask(nx), sizy = tree.ask(ny);if(!book[ny] && (sizx < sizy || book[nx]) && wh(nx) == mson[fa[nx]])ans += ny - nx, mson[fa[nx]] = wh(ny); // 换了重儿子,更新答案else if(book[nx] && book[ny]){ans -= son[fa[nx]][mson[fa[nx]]];break;}if(nx == root || book[nx]) break; // 如果没有更新或为叶子节点,退出,把贡献减去root = nx;}print(ans), putchar('\n');}2 行未改动/*当 siz_{nw} \le siz_{root}/2 时,nw才是 root轻儿子二分找到路径上深度最浅的 nwnw不一定是root的子节点,但没关系,nw到 root路径上的点 siz已经 >siz_{root}/2,不可能再成为它们父节点的轻儿子把 root设为 nw,重复*/```#### 4. 边权转点权9 行未改动```cppint lson(int x) {return x << 1;}int rson(int x) {return x << 1 | 1;}void pushup(int p){sum[p] = sum[lson(p)] + sum[rson(p)];mn[p] = min(mn[lson(p)], mn[rson(p)]), mx[p] = max(mx[lson(p)], mx[rson(p)]);}34 行未改动int mid = (nl + nr) >> 1;pushdown(p);if(mid >= l) modify2(l, r, nl, mid, lson(p));if(mid < r) modify2(l, r, mid + 1, nr, rson(p));pushup(p);}int querysum(int l, int r, int nl, int nr, int p){if(l <= nl && nr <= r) return sum[p];124 行未改动全部展开