# bzoj4712: 洪水 动态Dp 树链剖分+线段树 或 LCT维护矩阵乘法

### bzoj4712: 洪水

#### Input

n<=200000，保证任意to都为非负数

4
4 3 2 1
1 2
1 3
4 2
4
Q 1
Q 2
C 4 10
Q 1

3
1
4

### 分析

$f[u]=min \{v[u], \sum f[son_u]\}$

UPD2019.1.28: 用了一个LCT维护矩阵乘法的做法，可以适用于修改不递增的情况，而且复杂度为$O((n+m)logn)$

$f_u=\min \{f_{u-1}+g[u],val[u]\}$

$\left[ \begin{matrix} f_{u-1} & 0 \end{matrix} \right] \cdot \left[ \begin{matrix} g_u & 0 \\ val[u] & 0 \end{matrix} \right] \to \left[ \begin{matrix} f_u & 0 \end{matrix} \right]$

### 代码

#include<cstdio>
#include<algorithm>
#define ls p << 1
#define rs p << 1 | 1
typedef long long LL;
int ri() {
char c = getchar(); int x = 0; for(;c < '0' || c > '9'; c = getchar()) ;
for(;c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) - '0' + c; return x;
}
const int N = 2e5 + 10;
int f[N], de[N], ds[N], p[N], in[N], d[N], s[N], pr[N], nx[N << 1], to[N << 1], tp, tot, n;
long long h[N], g[N], v[N], tg[N << 2], t[N << 2];
void add(int u, int v) {to[++tp] = v; nx[tp] = pr[u]; pr[u] = tp;}
void Dfs1(int u, int f) {
::f[u] = f; de[u] = de[f] + 1; s[u] = 1;
for(int i = pr[u]; i; i = nx[i])
if(to[i] != f) {
Dfs1(to[i], u); s[u] += s[to[i]];
if(s[to[i]] > s[ds[u]]) ds[u] = to[i];
h[u] += g[to[i]];
}
if(s[u] == 1) h[u] = 1e18;
g[u] = std::min(v[u], h[u]);
}
void Dfs2(int u, int c) {
d[u] = c; p[in[u] = ++tot] = u; if(!ds[u]) return; Dfs2(ds[u], c);
for(int i = pr[u]; i; i = nx[i])
if(to[i] != f[u] && to[i] != ds[u])
Dfs2(to[i], to[i]);
}
void T(int p, int v) {tg[p] += v; t[p] -= v;}
void Push(int p) {if(tg[p]) T(ls, tg[p]), T(rs, tg[p]), tg[p] = 0;}
void Up(int p) {t[p] = std::min(t[ls], t[rs]);}
void Build(int p, int L, int R) {
if(L == R) {tg[p] = h[::p[L]]; t[p] = v[::p[L]] - h[::p[L]]; return ;}
int m = L + R >> 1; Build(ls, L, m); Build(rs, m + 1, R); Up(p);
}
LL Que(int p, int L, int R, int v) {
if(L == R) return tg[p];
int m = L + R >> 1; Push(p);
return v <= m ? Que(ls, L, m, v) : Que(rs, m + 1, R, v);
}
void Modv(int p, int L, int R, int v) {
if(L == R) return void(t[p] = ::v[::p[p]] - h[::p[p]]);
int m = L + R >> 1; Push(p);
v <= p ? Modv(ls, L, m, v) : Modv(rs, m + 1, R, v);
Up(p);
}
void Modh(int p, int L, int R, int st, int ed, LL w) {
if(L == st && ed == R) return T(p, w);
int m = L + R >> 1; Push(p);
if(ed > m) Modh(rs, m + 1, R, std::max(st, m + 1), ed, w);
if(st <= m) Modh(ls, L, m, st, std::min(ed, m), w);
Up(p);
}
LL Dp(int u) {return std::min(v[u], Que(1, 1, n, in[u]));}
void Chain(int u, int v, LL w) {
for(;d[u] != d[v]; u = f[d[u]]) Modh(1, 1, n, in[d[u]], in[u], w);
Modh(1, 1, n, in[v], in[u], w);
}
int Get(int p, int L, int R, int st, int ed, LL w) {
if(L == st && ed == R) {
if(t[p] >= w) return L;
if(L == R) return 0;
}
Push(p); int m = L + R >> 1, t = -1;
if(ed > m) t = Get(rs, m + 1, R, std::max(st, m + 1), ed, w);
if(~t && t != m + 1) return t;
if(st <= m) t = Get(ls, L, m, st, std::min(ed, m), w);
return t ? t : m + 1;
}
int Get(int u, LL w) {
int r = u; u = f[u];
for(;u; r = d[u], u = f[r]) {
int s = Get(1, 1, n, in[d[u]], in[u], w);
if(!s) break; if(s > in[d[u]]) return p[s];
}
return r;
}
void Mod(int u, LL w) {
LL t = Dp(u); v[u] += w; Modv(1, 1, n, in[u]);
LL d = Dp(u) - t; if(!d) return ;
for(;f[u];)  {
int v = Get(u, d);
if(u != v) Chain(f[u], v, d);
u = f[v]; if(!u) return ;
t = Dp(u); Modh(1, 1, n, in[u], in[u], d);
d = Dp(u) - t; if(!d) return;
}
}
int main() {
n = ri(); for(int i = 1; i <= n; ++i) v[i] = ri();
for(int i = 1;i < n; ++i) adds(ri(), ri());
Dfs1(1, 0); Dfs2(1, 1); Build(1, 1, n);
for(int m = ri();m--;) {
char op = getchar(); for(;op != 'C' && op != 'Q'; op = getchar()) ;
int x = ri();
if(op == 'Q') printf("%lld\n", Dp(x));
else Mod(x, ri());
}
return 0;
}
﻿


LCT

#include<bits/stdc++.h>
#define ls ch[p][0]
#define rs ch[p][1]
const int N = 2e5 + 10;
typedef long long LL;
int ri() {
char c = getchar(); int x = 0, f = 1; for(;c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
for(;c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) - '0' + c; return x * f;
}
struct Maxtir {
LL m[2][2];
LL * operator [] (int x) {return m[x];}
void Init(LL g, LL v) {
m[0][0] = g; m[1][0] = v;
m[0][1] = m[1][1] = 0;
}
Maxtir operator * (Maxtir b) {
Maxtir c;
c[0][0] = std::min(m[0][0] + b[0][0], m[0][1] + b[1][0]);
c[0][1] = std::min(m[0][0] + b[0][1], m[0][1] + b[1][1]);
c[1][0] = std::min(m[1][0] + b[0][0], m[1][1] + b[1][0]);
c[1][1] = std::min(m[1][0] + b[0][1], m[1][1] + b[1][1]);
return c;
}
}f[N];
int ch[N][2], fa[N], pr[N], to[N << 1], nx[N << 1], tp, n, m;
LL g[N], val[N];
void add(int u, int v) {to[++tp] = v; nx[tp] = pr[u]; pr[u] = tp;}
bool wh(int p) {return ch[fa[p]][1] == p;}
bool Ir(int p) {return ch[fa[p]][0] != p && ch[fa[p]][1] != p;}
void Up(int p) {
f[p].Init(g[p], val[p]);
if(ls) f[p] = f[p] * f[ls];
if(rs) f[p] = f[rs] * f[p];
}
void Rotate(int p) {
int f = fa[p], g = fa[f], c = wh(p);
if(!Ir(f)) ch[g][wh(f)] = p; fa[p] = g;
ch[f][c] = ch[p][c ^ 1]; if(ch[f][c]) fa[ch[f][c]] = f;
ch[p][c ^ 1] = f; fa[f] = p; Up(f);
}
void Splay(int p) {
for(;!Ir(p); Rotate(p))
if(!Ir(fa[p]))
Rotate(wh(p) == wh(fa[p]) ? fa[p] : p);
Up(p);
}
LL Ans(int p) {return std::min(f[p][0][0], f[p][1][0]);}
void Access(int u) {
for(int p = u, pr = 0; p; pr = p, p = fa[p]) {
Splay(p);
if(rs)
g[p] += Ans(rs);
if(pr)
g[p] -= Ans(pr);
rs = pr;
Up(p);
}
Splay(u);
}
void Dp(int u, int fa) {
::fa[u] = fa; bool lf = true;
for(int i = pr[u]; i; i = nx[i])
if(to[i] != fa)
Dp(to[i], u),
g[u] += Ans(to[i]),
lf = false;
if(lf) g[u] = 1e18;
f[u].Init(g[u], val[u]);
}
int main() {
n = ri();
for(int i = 1;i <= n; ++i)
val[i] = ri();
for(int i = 1;i < n; ++i)