动态 dp 学习笔记
动态 dp 是一种解决树上的动态修改的动态规划问题。
不太好说,拿题讲一下吧。
P4719 【模板】动态 DP
求树上最大权独立集。有单点修改。
没有修改就是没有上司的舞会。直接设 表示 号点不选择或选择对应的子树下的最大权独立集。这个式子懒得写了。
考虑一旦修改,有且只有从修改点到根的一条链上的 dp 值会被修改。因此考虑高效跳链算法,即树链剖分。
那么这样,我们将轻重链分开进行转移。设 为只考虑轻儿子的 dp 值, 还是老样子。
设 的重儿子为 ,那么显然,。
考虑快速进行转移,联想到矩阵快速幂,于是思考如何将这东西转化为矩阵形式。
发现可以化为 。
这不就是个 矩阵吗。定义运算 ,对于 ,有 。
然后实际上你的转移就相当于 。
然后考虑转移应当是从下到上的,但是树剖线段树是从上到下的,于是需要转置一下再做一个“乘法交换律”。
就是 。然后线段树维护矩阵乘法即可。
而且每次修改只会改变轻边那里的转移矩阵。因为转移矩阵里全是 ,如果在重链上根本不算数。
分析下时间复杂度吧,考虑到一个点被修改只会改变一条链,链上有 个重链,每次修改一条重链是 的,因此这个做法是大常数的 的,很笨蛋,但是可以过本题。
#include<bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
struct edge {int to , nxt;}e[200010];
int n , m , a[100010] , tot , head[100010] , dep[100010] , sz[100010] , son[100010] , fa[100010] , tp[100010] , dfn[100010] , cnt , rk[100010] , f[100010][2] , nd[100010];
struct Matrix
{
int n , m , mp[3][3];
Matrix(int n_ = 2 , int m_ = 2) {n = n_ , m = m_; memset(mp , -0x3f , sizeof(mp));}
Matrix operator * (const Matrix &a) const
{
int m_ = a.m; Matrix ans(n , m_);
for(int i = 1 ; i <= n ; i ++) for(int j = 1 ; j <= m_ ; j ++) for(int k = 1 ; k <= m ; k ++) ans.mp[i][j] = max(ans.mp[i][j] , mp[i][k] + a.mp[k][j]); // max-+ 矩乘
return ans;
}
};
Matrix g[100010];
Matrix init(int n)
{
Matrix x(n , n);
x.n = x.m = n;
for(int i = 1 ; i <= n ; i ++) x.mp[i][i] = 0;
return x;
}
void add(int u , int v)
{
++ tot;
e[tot].to = v;
e[tot].nxt = head[u];
head[u] = tot;
}
void dfs1(int u , int pa)
{
dep[u] = dep[pa] + 1 , sz[u] = 1 , fa[u] = pa;
for(int i = head[u] ; i != 0 ; i = e[i].nxt)
{
int v = e[i].to;
if(v == pa) continue;
dfs1(v , u);
sz[u] += sz[v];
if(sz[son[u]] < sz[v]) son[u] = v;
}
}
void dfs2(int u , int tp_fa)
{
dfn[u] = ++ cnt , rk[cnt] = u , tp[u] = tp_fa;
if(son[u]) dfs2(son[u] , tp_fa);
else nd[tp_fa] = dfn[u]; // 注意这里不是 fa[u] 因为这里相当于从 fa->u,你需要加上这个的转移矩阵
for(int i = head[u] ; i != 0 ; i = e[i].nxt)
{
int v = e[i].to;
if(v == fa[u] || v == son[u]) continue;
dfs2(v , v);
}
}
// 这里是初始化,所以正常做转移
void dfs3(int u , int pa)
{
f[u][1] = a[u];
for(int i = head[u] ; i != 0 ; i = e[i].nxt)
{
int v = e[i].to;
if(v == pa) continue;
dfs3(v , u);
f[u][0] += max(f[v][0] , f[v][1]);
f[u][1] += f[v][0];
}
}
void dfs4(int u , int tp_fa)
{
g[u] = Matrix(2 , 2);
g[u].mp[1][1] = g[u].mp[1][2] = 0 , g[u].mp[2][1] = a[u] , g[u].mp[2][2] = -inf;
if(son[u]) dfs4(son[u] , tp_fa);
for(int i = head[u] ; i != 0 ; i = e[i].nxt)
{
int v = e[i].to;
if(v == fa[u] || v == son[u]) continue;
dfs4(v , v);
g[u].mp[1][1] += max(f[v][0] , f[v][1]) , g[u].mp[1][2] += max(f[v][0] , f[v][1]);
g[u].mp[2][1] += f[v][0];
}
}
namespace SegmentTree
{
#define lson(root) (root << 1)
#define rson(root) (root << 1 | 1)
Matrix tree[400010];
void pushup(int root) {tree[root] = tree[lson(root)] * tree[rson(root)];}
void build(int root , int l , int r)
{
if(l == r) return tree[root] = g[rk[l]] , void();
int mid = (l + r) >> 1;
build(lson(root) , l , mid);
build(rson(root) , mid + 1 , r);
pushup(root);
}
void update(int root , int l , int r , int x , Matrix mp)
{
if(l == r) return tree[root] = mp , void();
int mid = (l + r) >> 1;
if(x <= mid) update(lson(root) , l , mid , x , mp);
else update(rson(root) , mid + 1 , r , x , mp);
pushup(root);
}
Matrix query(int root , int l , int r , int L , int R)
{
if(L <= l && R >= r) return tree[root];
int mid = (l + r) >> 1;
Matrix res1 = init(2) , res2 = init(2) , res = init(2);
if(L <= mid) res1 = query(lson(root) , l , mid , L , R);
if(R > mid) res2 = query(rson(root) , mid + 1 , r , L , R);
res = res1 * res2;
return res;
// if(L <= mid && R > mid) return query(lson(root) , l , mid , L , R) * query(rson(root) , mid + 1 , r , L , R);
// if(L <= mid) return query(lson(root) , l , mid , L , R);
// if(R > mid) return query(rson(root) , mid + 1 , r , L , R);
// __builtin_unreachable(); assert(0);
}
#undef lson
#undef rson
}
void uv_update(int u , int k)
{
// 这个函数专门计算 delta
g[u].mp[2][1] += k - a[u]; a[u] = k;
while(u)
{
int t = tp[u]; // 链头
Matrix pre = SegmentTree::query(1 , 1 , n , dfn[t] , nd[t]);
SegmentTree::update(1 , 1 , n , dfn[u] , g[u]);
Matrix now = SegmentTree::query(1 , 1 , n , dfn[t] , nd[t]);
u = fa[t];
if(u == 0) break;
g[u].mp[1][1] += max(now.mp[1][1] , now.mp[2][1]) - max(pre.mp[1][1] , pre.mp[2][1]) , g[u].mp[1][2] += max(now.mp[1][1] , now.mp[2][1]) - max(pre.mp[1][1] , pre.mp[2][1]);
g[u].mp[2][1] += now.mp[1][1] - pre.mp[1][1];
}
}
int main()
{
scanf("%d%d" , &n , &m);
for(int i = 1 ; i <= n ; i ++) scanf("%d" , &a[i]);
for(int i = 1 , u , v ; i < n ; i ++) scanf("%d%d" , &u , &v) , add(u , v) , add(v , u);
dfs1(1 , 0) , dfs2(1 , 1) , dfs3(1 , 0) , dfs4(1 , 1) , SegmentTree::build(1 , 1 , n);
for(int i = 1 , x , y ; i <= m ; i ++)
{
scanf("%d%d" , &x , &y);
// a[x] = y;
uv_update(x , y);
Matrix ans = SegmentTree::query(1 , 1 , n , dfn[1] , nd[1]);
printf("%d\n" , max({ans.mp[1][1] , ans.mp[1][2] , ans.mp[2][1] , ans.mp[2][2]}));
}
return 0;
}卡一下常数能过掉加强版。卡常代码如下:
#include<bits/stdc++.h>
using namespace std;
namespace FastIO {
#define USE_FastIO
#if ( defined(LOCAL) || defined(_WIN32) ) && !defined(DISABLE_MMAP)
#define DISABLE_MMAP
#endif
#ifdef LOCAL
inline void _chk_i() {}
inline char _gc_nochk() {
return getchar();
}
inline char _gc() {
return getchar();
}
inline void _chk_o() {}
inline void _pc_nochk(char c) {
putchar(c);
}
inline void _pc(char c) {
putchar(c);
}
template < int n > inline void _pnc_nochk(const char *c) {
for ( int i = 0 ; i < n ; i++ ) putchar(c[i]);
}
#else
#ifdef DISABLE_MMAP
inline constexpr int _READ_SIZE = 1 << 18;
inline static char _read_buffer[_READ_SIZE + 40], *_read_ptr = nullptr, *_read_ptr_end = nullptr;
static inline bool _eof = false;
inline void _chk_i() {
if ( __builtin_expect(!_eof, true) && __builtin_expect(_read_ptr_end - _read_ptr < 40, false) ) {
int sz = _read_ptr_end - _read_ptr;
if ( sz ) memcpy(_read_buffer, _read_ptr, sz);
char *beg = _read_buffer + sz;
_read_ptr = _read_buffer, _read_ptr_end = beg + fread(beg, 1, _READ_SIZE, stdin);
if ( __builtin_expect(_read_ptr_end != beg + _READ_SIZE, false) ) _eof = true, *_read_ptr_end = EOF;
}
}
inline char _gc_nochk() {
return __builtin_expect(_eof && _read_ptr == _read_ptr_end, false) ? EOF : *_read_ptr++;
}
inline char _gc() {
_chk_i();
return _gc_nochk();
}
#else
#include<sys/mman.h>
#include<sys/stat.h>
inline static char *_read_ptr = (char *)mmap(nullptr, [] { struct stat s; return fstat(0, &s), s.st_size; } (), 1, 2, 0, 0);
inline void _chk_i() {}
inline char _gc_nochk() {
return *_read_ptr++;
}
inline char _gc() {
return *_read_ptr++;
}
#endif
inline constexpr int _WRITE_SIZE = 1 << 18;
inline static char _write_buffer[_WRITE_SIZE + 40], *_write_ptr = _write_buffer;
inline void _chk_o() {
if ( __builtin_expect(_write_ptr - _write_buffer > _WRITE_SIZE, false) ) fwrite(_write_buffer, 1, _write_ptr - _write_buffer, stdout), _write_ptr = _write_buffer;
}
inline void _pc_nochk(char c) {
*_write_ptr++ = c;
}
inline void _pc(char c) {
*_write_ptr++ = c, _chk_o();
}
template < int n > inline void _pnc_nochk(const char *c) {
memcpy(_write_ptr, c, n), _write_ptr += n;
}
inline struct _auto_flush {
inline ~_auto_flush() {
fwrite(_write_buffer, 1, _write_ptr - _write_buffer, stdout);
}
} _auto_flush;
#endif
#define println println_ // don't use C++23 std::println
template < class T > inline constexpr bool _is_signed = numeric_limits < T >::is_signed;
template < class T > inline constexpr bool _is_unsigned = numeric_limits < T >::is_integer && !_is_signed < T >;
#if __SIZEOF_LONG__ == 64
template <> inline constexpr bool _is_signed < __int128 > = true;
template <> inline constexpr bool _is_unsigned < __uint128_t > = true;
#endif
inline bool _isgraph(char c) {
return c >= 33;
}
inline bool _isdigit(char c) {
return 48 <= c && c <= 57; // or faster, remove c <= 57
}
constexpr struct _table {
#ifndef LOCAL
int i[65536];
#endif
char o[40000];
constexpr _table() :
#ifndef LOCAL
i {},
#endif
o {} {
#ifndef LOCAL
for ( int x = 0 ; x < 65536 ; x++ ) i[x] = -1;
for ( int x = 0 ; x <= 9 ; x++ ) for ( int y = 0 ; y <= 9 ; y++ ) i[x + y * 256 + 12336] = x * 10 + y;
#endif
for ( int x = 0 ; x < 10000 ; x++ ) for ( int y = 3, z = x ; ~y ; y-- ) o[x * 4 + y] = z % 10 + 48, z /= 10;
}
} _table;
template < class T, int digit > inline constexpr T _pw10 = 10 * _pw10 < T, digit - 1 >;
template < class T > inline constexpr T _pw10 < T, 0 > = 1;
inline void read(char &c) {
do c = _gc();
while ( !_isgraph(c) );
}
inline void read_cstr(char *s) {
char c = _gc();
while ( !_isgraph(c) ) c = _gc();
while ( _isgraph(c) ) *s++ = c, c = _gc();
*s = 0;
}
inline void read(string &s) {
char c = _gc();
s.clear();
while ( !_isgraph(c) ) c = _gc();
while ( _isgraph(c) ) s.push_back(c), c = _gc();
}
template < class T, bool neg >
#ifndef LOCAL
__attribute__((no_sanitize("undefined")))
#endif
inline void _read_int_suf(T &x) {
_chk_i();
char c;
while
#ifndef LOCAL
( ~_table.i[*reinterpret_cast < unsigned short *& >(_read_ptr)] ) if constexpr ( neg ) x = x * 100 - _table.i[*reinterpret_cast < unsigned short *& >(_read_ptr)++];
else x = x * 100 + _table.i[*reinterpret_cast < unsigned short *& >(_read_ptr)++];
if
#endif
( _isdigit(c = _gc_nochk()) ) if constexpr ( neg ) x = x * 10 - ( c & 15 );
else x = x * 10 + ( c & 15 );
}
template < class T, enable_if_t < _is_signed < T >, int > = 0 > inline void read(T &x) {
char c;
while ( !_isdigit(c = _gc()) ) if ( c == 45 ) {
_read_int_suf < T, true >(x = -( _gc_nochk() & 15 ));
return;
}
_read_int_suf < T, false >(x = c & 15);
}
template < class T, enable_if_t < _is_unsigned < T >, int > = 0 > inline void read(T &x) {
char c;
while ( !_isdigit(c = _gc()) );
_read_int_suf < T, false >(x = c & 15);
}
inline void write(bool x) {
_pc(x | 48);
}
inline void write(char c) {
_pc(c);
}
inline void write_cstr(const char *s) {
while ( *s ) _pc(*s++);
}
inline void write(const string &s) {
for ( char c : s ) _pc(c);
}
template < class T, bool neg, int digit > inline void _write_int_suf(T x) {
if constexpr ( digit == 4 ) _pnc_nochk < 4 >(_table.o + ( neg ? -x : x ) * 4);
else _write_int_suf < T, neg, digit / 2 >(x / _pw10 < T, digit / 2 >), _write_int_suf < T, neg, digit / 2 >(x % _pw10 < T, digit / 2 >);
}
template < class T, bool neg, int digit > inline void _write_int_pre(T x) {
if constexpr ( digit <= 4 ) if ( digit >= 3 && ( neg ? x <= -100 : x >= 100 ) ) if ( digit >= 4 && ( neg ? x <= -1000 : x >= 1000 ) ) _pnc_nochk < 4 >(_table.o + ( neg ? -x : x ) * 4);
else _pnc_nochk < 3 >(_table.o + ( neg ? -x : x ) * 4 + 1);
else if ( digit >= 2 && ( neg ? x <= -10 : x >= 10 ) ) _pnc_nochk < 2 >(_table.o + ( neg ? -x : x ) * 4 + 2);
else _pc_nochk(( neg ? -x : x ) | 48);
else {
constexpr int cur = 1 << __lg(digit - 1);
if ( neg ? x <= -_pw10 < T, cur > : x >= _pw10 < T, cur > ) _write_int_pre < T, neg, digit - cur >(x / _pw10 < T, cur >), _write_int_suf < T, neg, cur >(x % _pw10 < T, cur >);
else _write_int_pre < T, neg, cur >(x);
}
}
template < class T, enable_if_t < _is_signed < T >, int > = 0 > inline void write(T x) {
if ( x >= 0 ) _write_int_pre < T, false, numeric_limits < T >::digits10 + 1 >(x);
else _pc_nochk(45), _write_int_pre < T, true, numeric_limits < T >::digits10 + 1 >(x);
_chk_o();
}
template < class T, enable_if_t < _is_unsigned < T >, int > = 0 > inline void write(T x) {
_write_int_pre < T, false, numeric_limits < T >::digits10 + 1 >(x), _chk_o();
}
template < size_t N, class ...T > inline void _read_tuple(tuple < T... > &x) {
read(get < N >(x));
if constexpr ( N + 1 != sizeof...(T) ) _read_tuple < N + 1, T... >(x);
}
template < size_t N, class ...T > inline void _write_tuple(const tuple < T... > &x) {
write(get < N >(x));
if constexpr ( N + 1 != sizeof...(T) ) _pc(32), _write_tuple < N + 1, T... >(x);
}
template < class ...T > inline void read(tuple < T... > &x) {
_read_tuple < 0, T... >(x);
}
template < class ...T > inline void write(const tuple < T... > &x) {
_write_tuple < 0, T... >(x);
}
template < class T1, class T2 > inline void read(pair < T1, T2 > &x) {
read(x.first), read(x.second);
}
template < class T1, class T2 > inline void write(const pair < T1, T2 > &x) {
write(x.first), _pc(32), write(x.second);
}
template < class T > inline auto read(T &x) -> decltype(x.read(), void()) {
x.read();
}
template < class T > inline auto write(const T &x) -> decltype(x.write(), void()) {
x.write();
}
template < class T1, class ...T2 > inline void read(T1 &x, T2 &...y) {
read(x), read(y...);
}
template < class ...T > inline void read_cstr(char *x, T *...y) {
read_cstr(x), read_cstr(y...);
}
template < class T1, class ...T2 > inline void write(const T1 &x, const T2 &...y) {
write(x), write(y...);
}
template < class ...T > inline void write_cstr(const char *x, const T *...y) {
write_cstr(x), write_cstr(y...);
}
template < class T > inline void print(const T &x) {
write(x);
}
inline void print_cstr(const char *x) {
write_cstr(x);
}
template < class T1, class ...T2 > inline void print(const T1 &x, const T2 &...y) {
write(x), _pc(32), print(y...);
}
template < class ...T > inline void print_cstr(const char *x, const T *...y) {
write_cstr(x), _pc(32), print_cstr(y...);
}
inline void println() {
_pc(10);
}
inline void println_cstr() {
_pc(10);
}
template < class ...T > inline void println(const T &...x) {
print(x...), _pc(10);
}
template < class ...T > inline void println_cstr(const T *...x) {
print_cstr(x...), _pc(10);
}
}
using FastIO::read, FastIO::read_cstr, FastIO::write, FastIO::write_cstr, FastIO::println, FastIO::println_cstr;
const int inf = 0x3f3f3f3f;
struct edge {int to , nxt;}e[200010];
int n , m , cnt , tot , head[100010] , a[100010] , sz[100010] , son[100010] , fa[100010] , tp[100010] , dfn[100010] , rk[100000] , f[100010][2] , nd[100010] , ed[100010] , rt[100010];
struct Matrix
{
int mp[2][2];
Matrix() {memset(mp , -0x3f , sizeof(mp));}
inline Matrix operator * (const Matrix &a) const
{
Matrix ans;
ans.mp[0][0] = max(mp[0][0] + a.mp[0][0] , mp[0][1] + a.mp[1][0]);
ans.mp[0][1] = max(mp[0][0] + a.mp[0][1] , mp[0][1] + a.mp[1][1]);
ans.mp[1][0] = max(mp[1][0] + a.mp[0][0] , mp[1][1] + a.mp[1][0]);
ans.mp[1][1] = max(mp[1][0] + a.mp[0][1] , mp[1][1] + a.mp[1][1]);
return ans;
}
};
Matrix g[100010];
inline Matrix init()
{
Matrix x;
for(int i = 0 ; i < 2 ; i ++) x.mp[i][i] = 0;
return x;
}
inline void add(int u , int v)
{
++ tot;
e[tot].to = v;
e[tot].nxt = head[u];
head[u] = tot;
}
void dfs1(int u , int pa)
{
sz[u] = 1 , fa[u] = pa;
f[u][1] = a[u];
for(int i = head[u] ; i != 0 ; i = e[i].nxt)
{
int v = e[i].to;
if(v == pa) continue;
dfs1(v , u);
sz[u] += sz[v];
f[u][0] += max(f[v][0] , f[v][1]);
f[u][1] += f[v][0];
if(sz[son[u]] < sz[v]) son[u] = v;
}
}
void dfs2(int u , int tp_fa)
{
dfn[u] = ++ cnt , rk[cnt] = u , tp[u] = tp_fa;
ed[tp_fa] = cnt;
g[u].mp[0][0] = g[u].mp[0][1] = 0 , g[u].mp[1][0] = a[u] , g[u].mp[1][1] = -inf;
if(son[u]) dfs2(son[u] , tp_fa);
else nd[tp_fa] = dfn[u];
for(int i = head[u] ; i != 0 ; i = e[i].nxt)
{
int v = e[i].to;
if(v == fa[u] || v == son[u]) continue;
dfs2(v , v);
g[u].mp[0][0] += max(f[v][0] , f[v][1]) , g[u].mp[0][1] += max(f[v][0] , f[v][1]);
g[u].mp[1][0] += f[v][0];
}
}
namespace SegmentTree
{
struct Tree{
Matrix a;
int ls,rs;
} tr[2097152];
int cnt;
inline void pushup(int root) {tr[root].a = tr[tr[root].ls].a * tr[tr[root].rs].a;}
void build(int &root , int l , int r)
{
root = ++ cnt;
if(l == r) return tr[root].a = g[rk[l]] , void();
int mid = (l + r) >> 1;
build(tr[root].ls , l , mid);
build(tr[root].rs , mid + 1 , r);
pushup(root);
}
void update(int root , int l , int r , int x ,const Matrix &mp)
{
if(l == r) return tr[root].a = mp , void();
int mid = (l + r) >> 1;
if(x <= mid) update(tr[root].ls , l , mid , x , mp);
else update(tr[root].rs , mid + 1 , r , x , mp);
pushup(root);
}
Matrix query(int root , int l , int r , int L , int R)
{
if(L <= l && R >= r) return tr[root].a;
int mid = (l + r) >> 1;
Matrix res = init();
if(L <= mid) res = query(tr[root].ls , l , mid , L , R);
if(R > mid) res = res * query(tr[root].rs , mid + 1 , r , L , R);
return res;
}
}
inline void uv_update(int u , int k)
{
g[u].mp[1][0] += k - a[u]; a[u] = k;
while(u)
{
int t = tp[u];
Matrix pre = SegmentTree::tr[rt[t]].a;
SegmentTree::update(rt[t] , dfn[t] , ed[t] , dfn[u] , g[u]);
Matrix now = SegmentTree::tr[rt[t]].a;
u = fa[t];
if(u == 0) break;
g[u].mp[0][0] += max(now.mp[0][0] , now.mp[1][0]) - max(pre.mp[0][0] , pre.mp[1][0]) , g[u].mp[0][1] += max(now.mp[0][0] , now.mp[1][0]) - max(pre.mp[0][0] , pre.mp[1][0]);
g[u].mp[1][0] += now.mp[0][0] - pre.mp[0][0];
}
}
int main()
{
read(n , m);
for(int i = 1 ; i <= n ; i ++) read(a[i]);
for(int i = 1 , u , v ; i < n ; i ++) read(u , v) , add(u , v) , add(v , u);
int r = 114514 % n + 1;
dfs1(r , 0) , dfs2(r , r);
for(int i = 1 ; i <= n ; i ++) if(tp[i] == i) SegmentTree::build(rt[i] , dfn[i] , ed[i]);
// int lstans = 0;
for(int i = 1 , x , y ; i <= m ; i ++)
{
read(x , y);
// x ^= lstans;
uv_update(x , y);
Matrix ans = SegmentTree::tr[rt[r]].a;
// lstans = max({ans.mp[0][0] , ans.mp[0][1] , ans.mp[1][0] , ans.mp[1][1]});
write(max({ans.mp[0][0] , ans.mp[0][1] , ans.mp[1][0] , ans.mp[1][1]}));
println();
}
return 0;
}