这题一看就是动态dp,先考虑暴力
s表示不选这个点,f表示选这个点
s [ i ] = max ( s [ t ] , f [ t ] ) s[i] = \max(s[t], f[t]) s[i]=max(s[t],f[t])
f [ i ] = max ( 0 , V x + ∑ f [ t ] ) f[i] = \max(0, V_x + \sum f[t]) f[i]=max(0,Vx+∑f[t])
一开始依照套路写个矩乘(雾
虽然上面的转移我硬是推出来了,但是我推了之后发现 s s s并不支持删除一个子树
还是将轻重儿子分开,g表示轻儿子f之和
重链上g的序列代表了什么呢
惊讶的发现 f i f_i fi就是以i为头的最大子段和, s i s_i si就是重链上序列g的最大子段和与轻儿子 s t s_t st 取max
这显然可以方便的使用线段树维护。
修改也变成了单点修改,g可以直接加减维护,单点s可以开一个可删堆维护
#include <queue>
#include <cstdio>
#include <algorithm>
using namespace std;
#define int long long
#define dd c=getchar()
int read() {
int s=0,w=1;char c;while (dd,c>'9' || c<'0') if (c=='-') w=-1;while (c>='0' && c<='9') s=s*10+c-'0',dd;return s*w;}
#undef dd
void write(int x) {
if (x<0) putchar('-'),x=