BZOJ 3939: [Usaco2015 Feb]Cow Hopscotch

\(dp[i][j]\) 表示走到 \((i,j)\) 的方案数
\(dp[i][j] = \sum\limits_{x<i\wedge y<j \wedge c[x][y]\neq c[i][j]}dp[x][y]=\sum\limits_{x<i\wedge y<j}dp[x][y]-\sum\limits_{x<i\wedge y<j \wedge c[x][y] = c[i][j]}dp[x][y]\)
解法一:第一部分二维前缀和,动态开点线段树维护每种颜色每一列的方案数,\(i\) 循环行,\(j\) 循环列,那么就只需要查询 \(1\) ~ \(j-1\) 的和,先处理出每一行的dp值,再一起更新线段树及二维前缀和
解法二:CDQ分治,按行分治,考虑前半部分对后半部分的贡献
两种写法都挺好写的,复杂度都是 \(O(nm\log n)\),但是CDQ空间更优,常数小

/*** solution1 ***/
#include <bits/stdc++.h>
#define mid ((l + r) >> 1)
#define lp tree[p].l
#define rp tree[p].r

const int MOD = 1000000007;
const int N = 776;

void M(int &x) {
	if (x >= MOD) x -= MOD;
	if (x < 0) x += MOD;
}

struct Node {
	int l, r, sum;
} tree[10 * N * N];
int tol, R, C, K;
int sum[N][N], dp[N][N], color[N][N];
void update(int &p, int l, int r, int pos, int v) {
	if (!p) p = ++tol;
	if (l == r) {
		M(tree[p].sum += v);
		return;
	}
	if (pos <= mid) update(lp, l, mid, pos, v);
	else update(rp, mid + 1, r, pos, v);
	M(tree[p].sum = tree[lp].sum + tree[rp].sum);
}
int query(int p, int l, int r, int x, int y) {
	if (x > y) return 0;
	if (!p) return 0;
	if (x <= l && y >= r) return tree[p].sum;
	int ans = 0;
	if (x <= mid) M(ans += query(lp, l, mid, x, y));
	if (y > mid) M(ans += query(rp, mid + 1, r, x, y));
	return ans;
}
int root[N * N];

int main() {
	scanf("%d%d%d", &R, &C, &K);
	for (int i = 1; i <= R; i++)
		for (int j = 1; j <= C; j++)
			scanf("%d", color[i] + j);
	dp[1][1] = sum[1][1] = 1;
	for (int i = 2; i <= R; i++)
		sum[i][1] = 1;
	for (int j = 2; j <= C; j++)
		sum[1][j] = 1;
	update(root[color[1][1]], 1, C, 1, 1);
	for (int i = 2; i <= R; i++) {
		for (int j = 2; j <= C; j++) {
			M(dp[i][j] = sum[i - 1][j - 1] - query(root[color[i][j]], 1, C, 1, j - 1));
		}
		for (int j = 2; j <= C; j++) {
			update(root[color[i][j]], 1, C, j, dp[i][j]);
			M(sum[i][j] = (1LL * dp[i][j] + sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1]) % MOD);
		}
	}
	printf("%d\n", dp[R][C]);
	return 0;
}
/*** solution2 ***/
#include <bits/stdc++.h>

namespace IO {
	char buf[1 << 21], *p1 = buf, *p2 = buf;
	inline void read() {}
	inline int getc() {
	    return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++;
	}
	template <typename T, typename... T2>
	inline void read(T &x, T2 &... oth) {
	    T f = 1; x = 0;
	    char ch = getc();
	    while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getc(); }
	    while (isdigit(ch)) { x = x * 10 + ch - 48; ch = getc(); }
	    x *= f;
	    read(oth...);
	}
}

const int MOD = 1000000007;
const int N = 776;

inline void M(int &x) {
	if (x >= MOD) x -= MOD;
	if (x < 0) x += MOD;
}

int n, m, k;
int dp[N][N], sum[N * N], color[N][N];
int st[N * N], top;
bool vis[N * N];

void solve(int l, int r) {
	if (l >= r) return;
	int mid = l + r >> 1;
	solve(l, mid);
	int all = 0;
	for (int j = 1; j <= m; j++) {
		for (int i = mid + 1; i <= r; i++)
			M(dp[i][j] += all - sum[color[i][j]]);
		for (int i = l; i <= mid; i++) {
			M(all += dp[i][j]), M(sum[color[i][j]] += dp[i][j]);
			if (!vis[color[i][j]]) vis[color[i][j]] = 1, st[++top] = color[i][j];
		}
	}
	while (top) {
		int c = st[top--];
		sum[c] = 0;
		vis[c] = 0;
	}
	solve(mid + 1, r);
}

int main() {
	IO::read(n, m, k);
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			IO::read(color[i][j]);
	dp[1][1] = 1;
	solve(1, n);
	printf("%d\n", dp[n][m]);
	return 0;
}
posted @ 2020-02-14 17:21  Mrzdtz220  阅读(104)  评论(0编辑  收藏  举报