记录编号 607058 评测结果 AAAAAAAAAAAAAAA
题目名称 3863.[USACO23 Jan Silver] Following Directions 最终得分 100
用户昵称 Gravatar对立猫猫对立 是否通过 通过
代码语言 C++ 运行时间 1.110 s
提交时间 2025-10-05 14:23:15 内存使用 9.43 MiB
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
int n, q, l[1505], r[1505], a[1505][1505];
char c[1505][1505];
void init() {
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= n; j++) {
			a[i][j]++;
			if(c[i][j] == 'R') a[i][j + 1] += a[i][j];
			else if(c[i][j] == 'D') a[i + 1][j] += a[i][j];
		}
	}
}
void dfs(int x, int y, int cnt, bool ok) {
	if(ok) a[x][y] += cnt;
	if(y == n && c[x][y] == 'R') a[x][y + 1] += cnt;
	else if(x == n && c[x][y] == 'D') a[x + 1][y] += cnt;
	else if(c[x][y] == 'R') dfs(x, y + 1, cnt, 1);
	else if(c[x][y] == 'D') dfs(x + 1, y, cnt, 1);
}
int ans() {
	int sum = 0;
	for(int i = 1; i <= n; i++) {
		sum += a[i][n + 1] * l[i] + a[n + 1][i] * r[i];
	}
	return sum;
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> n;
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= n; j++) {
			cin >> c[i][j];
		}
		cin >> l[i];
	}
	for(int i = 1; i <= n; i++) cin >> r[i];
	init(); // 初始化
	cout << ans() << '\n'; // 计算最初的答案
	cin >> q;
	while(q--) {
		int x, y;
		cin >> x >> y;
		dfs(x, y, -a[x][y], 0); // 从(x,y)走向右下的奶牛先剔除
		if(c[x][y] == 'D') c[x][y] = 'R';
		else c[x][y] = 'D';
		dfs(x, y, a[x][y], 0); // 按新路线再走一遍
		cout << ans() << '\n'; // 输出!
	}
	return 0;
}