[NOIP2018 提高组] 保卫王国

题解

两只 \(\log\) 的动态 \(dp\)
相比标算倍增
动态 \(dp\) 既实用又好理解

\(Code\)

#include<cstdio>
#include<iostream>
#define ls (p << 1)
#define rs (ls | 1)
using namespace std;
typedef long long LL;

const int N = 1e5 + 5;
const LL INF = 0x3f3f3f3f3f3f3f3f;
int n, m, p[N];
char type[10];
LL f[N][2], g[N][2];

int h[N];
struct edge{int to, nxt;}e[N << 1];
inline void add(int x, int y)
{
	static int tot = 0;
	e[++tot] = edge{y, h[x]}, h[x] = tot;
}

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];
inline Matrix I()
{
	Matrix c;
	c.a[0][0] = c.a[1][1] = 0, c.a[0][1] = c.a[1][0] = INF;
	return c;
}
inline Matrix Node(LL g0, LL g1)
{
	Matrix c;
	c.a[0][0] = INF, c.a[0][1] = g0, c.a[1][0] = c.a[1][1] = g1;
	return c;
}

int fa[N], son[N], siz[N], top[N], bot[N], dfn[N];
void dfs1(int x)
{
	siz[x] = 1, f[x][1] = p[x];
	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];
		if (siz[v] > siz[son[x]]) son[x] = v;
		f[x][0] += f[v][1], f[x][1] += min(f[v][0], f[v][1]);
	}
}
void dfs2(int x)
{
	static int dfc = 0;
	dfn[x] = ++dfc, g[x][1] = p[x];
	if (son[x]) top[son[x]] = top[x], dfs2(son[x]);
	else bot[top[x]] = dfn[x];
	for(register int i = h[x]; i; i = e[i].nxt)
	{
		int v = e[i].to;
		if (v == fa[x] || v == son[x]) continue;
		top[v] = v, g[x][0] += f[v][1], g[x][1] += min(f[v][0], f[v][1]);
		dfs2(v);
	}
}

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][0], g[x][1])), x = top[x];
		Matrix ret = query(1, 1, n, dfn[x], bot[x]);
		g[fa[x]][0] -= f[x][1], g[fa[x]][1] -= min(f[x][0], f[x][1]);
		f[x][0] = ret.a[0][1], f[x][1] = ret.a[1][1];
		g[fa[x]][0] += f[x][1], g[fa[x]][1] += min(f[x][0], f[x][1]);
		x = fa[x];
	}
}

int main()
{
	freopen("defense.in", "r", stdin);
	freopen("defense.out", "w", stdout);
	scanf("%d%d%s", &n, &m, type);
	for(register int i = 1; i <= n; i++) scanf("%d", p + 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][0], g[i][1]));
	for(register int i = 1; i <= m; i++)
	{
		int a, b, x, y;
		scanf("%d%d%d%d", &a, &x, &b, &y), x ^= 1, y ^= 1;
		LL u = g[a][x], v = g[b][y];
		g[a][x] = INF, change(a), g[b][y] = INF, change(b);
		printf("%lld\n", min(f[1][0], f[1][1]) < INF ? min(f[1][0], f[1][1]) : -1LL);
		g[a][x] = u, change(a), g[b][y] = v, change(b);
	}
}

但如果两只 \(\log\) 被卡了呢?
一种比较优秀的卡常方法是对于每条重链开一颗线段树维护
然后跳的时候询问可直接获取

\(Code\)

#include<cstdio>
#include<iostream>
using namespace std;
typedef long long LL;

const int N = 1e5 + 5;
const LL INF = 0x3f3f3f3f3f3f3f3f;
int n, m, p[N];
char type[10];
LL f[N][2], g[N][2];

int h[N];
struct edge{int to, nxt;}e[N << 1];
inline void add(int x, int y)
{
	static int tot = 0;
	e[++tot] = edge{y, h[x]}, h[x] = tot;
}

int fa[N], son[N], siz[N], top[N], bot[N], dfn[N];
void dfs1(int x)
{
	siz[x] = 1, f[x][1] = p[x];
	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];
		if (siz[v] > siz[son[x]]) son[x] = v;
		f[x][0] += f[v][1], f[x][1] += min(f[v][0], f[v][1]);
	}
}
void dfs2(int x)
{
	static int dfc = 0;
	dfn[x] = ++dfc, g[x][1] = p[x];
	if (son[x]) top[son[x]] = top[x], dfs2(son[x]);
	else bot[top[x]] = dfn[x];
	for(register int i = h[x]; i; i = e[i].nxt)
	{
		int v = e[i].to;
		if (v == fa[x] || v == son[x]) continue;
		top[v] = v, g[x][0] += f[v][1], g[x][1] += min(f[v][0], f[v][1]);
		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;
	}
};
inline Matrix I()
{
	Matrix c;
	c.a[0][0] = c.a[1][1] = 0, c.a[0][1] = c.a[1][0] = INF;
	return c;
}
inline Matrix Node(LL g0, LL g1)
{
	Matrix c;
	c.a[0][0] = INF, c.a[0][1] = g0, c.a[1][0] = c.a[1][1] = g1;
	return c;
}
struct node{Matrix val; int ls, rs;}seg[N << 2];
int rt[N];

void build(int &p, int l, int r)
{
    static int size = 0;
    if (!p) p = ++size;
    if(l == r) return;
    int mid = (l + r) >> 1;
    build(seg[p].ls, l, mid), build(seg[p].rs, mid + 1, r);
}
void update(int p, int l, int r, int x, Matrix v)
{
	if (l == r) return void(seg[p].val = v);
	int mid = (l + r) >> 1;
	if (x <= mid) update(seg[p].ls, l, mid, x, v);
	else update(seg[p].rs, mid + 1, r, x, v);
	seg[p].val = seg[seg[p].ls].val * seg[seg[p].rs].val;
}

inline void change(int x)
{
	while (x)
	{
		update(rt[top[x]], dfn[top[x]], bot[top[x]], dfn[x], Node(g[x][0], g[x][1])), x = top[x];
		Matrix ret = seg[rt[x]].val;
		g[fa[x]][0] -= f[x][1], g[fa[x]][1] -= min(f[x][0], f[x][1]);
		f[x][0] = ret.a[0][1], f[x][1] = ret.a[1][1];
		g[fa[x]][0] += f[x][1], g[fa[x]][1] += min(f[x][0], f[x][1]);
		x = fa[x];
	}
}

int main()
{
	freopen("defense.in", "r", stdin);
	freopen("defense.out", "w", stdout);
	scanf("%d%d%s", &n, &m, type);
	for(register int i = 1; i <= n; i++) scanf("%d", p + 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++) if (top[i] == i) build(rt[i], dfn[i], bot[i]);
	for(register int i = 1; i <= n; i++) update(rt[top[i]], dfn[top[i]], bot[top[i]], dfn[i], Node(g[i][0], g[i][1]));
	for(register int i = 1; i <= m; i++)
	{
		int a, b, x, y;
		scanf("%d%d%d%d", &a, &x, &b, &y), x ^= 1, y ^= 1;
		LL u = g[a][x], v = g[b][y];
		g[a][x] = INF, change(a), g[b][y] = INF, change(b);
		printf("%lld\n", min(f[1][0], f[1][1]) < INF ? min(f[1][0], f[1][1]) : -1LL);
		g[a][x] = u, change(a), g[b][y] = v, change(b);
	}
}
posted @ 2021-01-22 10:31  leiyuanze  阅读(121)  评论(0编辑  收藏  举报