SP6779 GSS7 - Can you answer these queries VII (树链剖分 树链信息合并)

题目链接: SP6779 GSS7 - Can you answer these queries VII

大致题意

给定一棵有 n n n个节点的树, 每个节点上有权值 w i ( ∣ w i ∣ ≤ 10000 ) w_i(|w_i| \le 10000) wi(wi10000). 有 m m m次操作:

1 a b 查询 ( a , b ) (a, b) (a,b)路径上的最大连续子段和.

2 a b c ( a , b ) (a, b) (a,b)路径上的所有点权修改为 c c c.

解题思路

树链剖分

我们考虑如果这不是一个树上问题, 而是对序列进行操作. 那么本题就是一道十分经典的线段树维护最大子段和问题.

考虑到增加了树上操作, 难点是如何维护树链与树链之间的区间信息.

我们考虑 ( a , b ) (a, b) (a,b)不在同一链上时: 我们用 l a s t last last表示上一次查询的信息. 当前区间的查询信息记为 n o w now now, 那么每次区间信息合并时, 我们是把 n o w now now当作左区间, l a s t last last当作右区间进行合并.
由于每次向上跳的点是不确定的(可能是 a a a, 也可能是 b b b), 因此我们需要分别用 l a , l b la, lb la,lb来维护 l a s t last last的区间信息.

考虑到 ( a , b ) (a, b) (a,b)现在已经到同一条链上时: 我们保证 d e p a < d e p b dep_a < dep_b depa<depb, 那么最后一次的信息合并, 应当是要把 n o w now now作为左区间, l b lb lb作为右区间进行合并. (也可以先把 l a la la链翻转后, 把 n o w now now合并到 l a la la上, 此时 l a la la作为左区间, n o w now now作为右区间).
最后我们需要再把 l a , l b la, lb la,lb两条链的信息合并, 我们发现需要翻转一条链后再进行合并. (如果翻转 l a la la, 那么最终是 l a la la作为左区间, l b lb lb作为右区间进行合并).

关于上文, 如果不理解的地方, 推荐大家画个图看一下, 就明白了.

树链上涉及到区间合并时, 你会发现树链上的区间信息是"有方向的".


特别的, 本题中由于路径 a → b a \rarr b ab b → a b \rarr a ba的答案是相同的, 倘若我们进行了奇数次交换 l a , l b la, lb la,lb 但最终求得的答案实际上是 b → a b \rarr a ba的.

当然, 如果这个题本身是维护LCS的话, 那就不一样了.

➡️树上LCIS点这里⬅️

AC代码

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 1; i <= (n); ++i)
using namespace std;
typedef long long ll;
const int N = 1E5 + 10, INF = 0x3f3f3f3f;
int w[N]; //给定的树中各个顶点的权值
vector<int> edge[N]; //树上各个点之间的边

int p[N], dep[N], sz[N], son[N];
// 父节点   深度   节点大小 重儿子
void dfs1(int x = 1, int fa = 0) { // x = 树根节点
	p[x] = fa, dep[x] = dep[fa] + 1, sz[x] = 1; // son[x] = 0;
	for (auto& to : edge[x]) {
		if (to == fa) continue;
		dfs1(to, x);
		sz[x] += sz[to];		// 特别的, 如果边权->点权, 应记录w[to] = 边权.
		if (sz[to] > sz[son[x]]) son[x] = to; //更新重儿子
	}
}
int id[N], nw[N], top[N], ind;
//  新编号  新值    重链顶 当前用到的编号
void dfs2(int x = 1, int tp = 1) { // x = 树根节点, tp = 树根节点
	id[x] = ++ind, nw[ind] = w[x], top[x] = tp;

	if (!son[x]) return; //叶子结点
	dfs2(son[x], tp); //先遍历重儿子

	for (auto& to : edge[x]) {
		if (to == p[x] or to == son[x]) continue;
		dfs2(to, to);
	}
}


struct node {
	int l, r;
	int fmax, lmax, rmax, sum;
	int lazy;
}t[N << 2];
void pushdown(node& op, int lazy) {
	const int len = op.r - op.l + 1;
	if (lazy > 0) op.fmax = op.lmax = op.rmax = len * lazy;
	else op.fmax = op.lmax = op.rmax = lazy;
	op.sum = len * lazy;
	op.lazy = lazy;
}
void pushdown(int x) {
	if (t[x].lazy == INF) return;
	pushdown(t[x << 1], t[x].lazy), pushdown(t[x << 1 | 1], t[x].lazy);
	t[x].lazy = INF;
}

node merge(const node& l, const node& r) {
	node res;
	res.fmax = max({ l.fmax, r.fmax, l.rmax + r.lmax });
	res.lmax = max(l.lmax, l.sum + r.lmax);
	res.rmax = max(r.rmax, r.sum + l.rmax);
	res.sum = l.sum + r.sum;
	return res;
}
void pushup(int x) { t[x] = merge(t[x << 1], t[x << 1 | 1]); }

void build(int l, int r, int x = 1) {
	t[x] = { l, r, nw[l], nw[l], nw[l], nw[l], INF };
	if (l == r) return;
	int mid = l + r >> 1;
	build(l, mid, x << 1), build(mid + 1, r, x << 1 | 1);
	pushup(x);
}

void modify(int l, int r, int c, int x = 1) {
	if (l <= t[x].l and r >= t[x].r) {
		pushdown(t[x], c);
		return;
	}
	pushdown(x);
	int mid = t[x].l + t[x].r >> 1;
	if (l <= mid) modify(l, r, c, x << 1);
	if (r > mid) modify(l, r, c, x << 1 | 1);
	pushup(x);
}

node ask(int l, int r, int x = 1) {
	if (l <= t[x].l and r >= t[x].r) return t[x];
	pushdown(x);
	int mid = t[x].l + t[x].r >> 1;
	if (r <= mid) return ask(l, r, x << 1);
	if (l > mid) return ask(l, r, x << 1 | 1);
	node left = ask(l, r, x << 1), right = ask(l, r, x << 1 | 1);
	return merge(left, right);
}


void modify_route(int a, int b, int c) {
	while (top[a] != top[b]) {
		if (dep[top[a]] < dep[top[b]]) swap(a, b);
		modify(id[top[a]], id[a], c);
		a = p[top[a]];
	}
	if (id[a] > id[b]) swap(a, b);
	modify(id[a], id[b], c);
}

int ask_route(int a, int b) {
	node la = { 0, 0, 0, 0, 0, 0, 0 }, lb = { 0, 0, 0, 0, 0, 0, 0 };
	while (top[a] != top[b]) {
		if (dep[top[a]] < dep[top[b]]) swap(a, b), swap(la, lb);
		la = merge(ask(id[top[a]], id[a]), la);
		a = p[top[a]];
	}

	if (dep[a] > dep[b]) swap(a, b), swap(la, lb);
	lb = merge(ask(id[a], id[b]), lb);
	swap(la.lmax, la.rmax);
	return merge(la, lb).fmax;
}

int main()
{
	int n; cin >> n;
	rep(i, n) scanf("%d", &w[i]);
	rep(i, n - 1) {
		int a, b; scanf("%d %d", &a, &b);
		edge[a].push_back(b), edge[b].push_back(a);
	}

	dfs1(), dfs2();
	build(1, n);

	int m; cin >> m;
	rep(i, m) {
		int tp, a, b; scanf("%d %d %d", &tp, &a, &b);
		if (tp == 1) printf("%d\n", ask_route(a, b));
		else {
			int c; scanf("%d", &c);
			modify_route(a, b, c);
		}
	}

	return 0;
}

END

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逍遥Fau

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值