• JZOJ 4043. 【雅礼集训2015Kzf】洪水


    题目

    题解

    很明显的 (dp)
    (f_u = min(a_u, sum_{(u,v) in E}f_v))
    然后套路的设 (g_u) 表示不管重儿子的 (f_u)
    (f_u = min(a_u, g_u+f_{ ext{son}_v}))
    然后推一波矩阵

    [egin{bmatrix} g_u&a_u\ infty&0 end{bmatrix}otimes egin{bmatrix} f_{ ext{son}_u} \ 0 end{bmatrix}= egin{bmatrix} f_u\ 0 end{bmatrix} ]

    (Code)

    #include<cstdio>
    #include<iostream>
    #define LL long long
    #define ls (p << 1)
    #define rs (ls | 1)
    using namespace std;
    
    const int N = 2e5 + 5;
    const LL INF = 0x3f3f3f3f3f3f3f3f;
    int n, m, a[N], h[N], tot;
    LL f[N], g[N];
    
    struct edge{int to, nxt;}e[N << 1];
    inline void add(int x, int y){e[++tot] = edge{y, h[x]}, h[x] = tot;}
    
    int siz[N], fa[N], son[N], top[N], dfn[N], bot[N], dfc;
    void dfs1(int x)
    {
    	siz[x] = 1;
    	for(register int i = h[x]; i; i = e[i].nxt)
    	{
    		int v = e[i].to;
    		if (v == fa[x]) continue;
    		fa[v] = x, dfs1(v), siz[x] += siz[v], f[x] += f[v];
    		if (siz[v] > siz[son[x]]) son[x] = v;
    	}
    	if (siz[x] == 1) f[x] = a[x];
    	else f[x] = min(f[x], 1LL * a[x]);
    }
    void dfs2(int x)
    {
    	dfn[x] = ++dfc;
    	if (son[x]) top[son[x]] = top[x], dfs2(son[x]);
    	else bot[top[x]] = dfn[x], g[x] = INF;
    	for(register int i = h[x]; i; i = e[i].nxt)
    	{
    		int v = e[i].to;
    		if (v == son[x] || v == fa[x]) continue;
    		top[v] = v, g[x] += f[v], dfs2(v);
    	}
    }
    
    struct Matrix{
    	LL a[2][2];
    	inline Matrix(){a[0][0] = a[0][1] = a[1][0] = a[1][1] = INF;}
    	inline Matrix operator*(const Matrix &b)
    	{
    		Matrix c;
    		for(register int i = 0; i < 2; i++)
    			for(register int j = 0; j < 2; j++)
    				for(register int k = 0; k < 2; k++)
    				c.a[i][j] = min(c.a[i][j], a[i][k] + b.a[k][j]);
    		return c;
    	}
    }seg[N << 2];
    struct Matrix Node(LL _g, LL _a)
    {
    	Matrix c;
    	c.a[0][0] = _g, c.a[0][1] = _a, c.a[1][0] = INF, c.a[1][1] = 0;
    	return c;
    }
    struct Matrix I()
    {
    	Matrix c;
    	c.a[0][0] = c.a[1][1] = 0, c.a[0][1] = c.a[1][0] = INF;
    	return c;
    }
    
    void update(int p, int l, int r, int x, Matrix v)
    {
    	if (l == r) return void(seg[p] = v);
    	int mid = (l + r) >> 1;
    	if (x <= mid) update(ls, l, mid, x, v);
    	else update(rs, mid + 1, r , x, v);
    	seg[p] = seg[ls] * seg[rs];
    }
    Matrix query(int p, int l, int r, int tl, int tr)
    {
    	if (tl <= l && r <= tr) return seg[p];
    	int mid = (l + r) >> 1;
    	Matrix ret = I();
    	if (tl <= mid) ret = query(ls, l, mid, tl, tr);
    	if (tr > mid) ret = ret * query(rs, mid + 1, r, tl, tr);
    	return ret;
    }
    
    inline void change(int x)
    {
    	while (x)
    	{
    		update(1, 1, n, dfn[x], Node(g[x], a[x])), x = top[x];
    		Matrix ret = query(1, 1, n, dfn[x], bot[x]);
    		g[fa[x]] -= f[x], f[x] = min(ret.a[0][0], ret.a[0][1]), g[fa[x]] += f[x];
    		x = fa[x];
    	}
    }
    
    int main()
    {
    	scanf("%d", &n);
    	for(register int i = 1; i <= n; i++) scanf("%d", a + i);
    	for(register int i = 1, u, v; i < n; i++) scanf("%d%d", &u, &v), add(u, v), add(v, u);
    	dfs1(1), top[1] = 1, dfs2(1);
    	for(register int i = 1; i <= n; i++) update(1, 1, n, dfn[i], Node(g[i], a[i]));
    	scanf("%d", &m);
    	char op[5];
    	for(int x, to; m; --m)
    	{
    		scanf("%s%d", op, &x);
    		if (op[0] == 'Q')
    		{
    			Matrix ret = query(1, 1, n, dfn[x], bot[top[x]]);
    			printf("%lld
    ", min(ret.a[0][0], ret.a[0][1]));
    		}
    		else scanf("%d", &to), a[x] += to, change(x);
    	}
    }
    
  • 相关阅读:
    【kd-tree】bzoj2648 SJY摆棋子
    【kd-tree】bzoj3053 The Closest M Points
    【堆】【kd-tree】bzoj2626 JZPFAR
    【kd-tree】bzoj1941 [Sdoi2010]Hide and Seek
    【kd-tree】bzoj2850 巧克力王国
    【kd-tree】bzoj3489 A simple rmq problem
    【kd-tree】bzoj4066 简单题
    【二维莫队】【二维分块】bzoj2639 矩形计算
    【kd-tree】bzoj1176 [Balkan2007]Mokia
    【kd-tree】bzoj3290 Theresa与数据结构
  • 原文地址:https://www.cnblogs.com/leiyuanze/p/14316756.html
Copyright © 2020-2023  润新知