显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
const int N = 1505;
int n, q;
int d[N][N];
int v[N][N];
int c[N][N];
long long res;
void upd(int x, int y, int z) {
if (x > n || y > n) return;
if (d[x][y] == 1) {
c[x][y+1] += z;
upd(x, y+1, z);
} else {
c[x+1][y] += z;
upd(x+1, y, z);
}
}
void init(int x, int y) {
if (x > n || y > n) return;
c[x][y]++;
if (d[x][y] == 1) {
c[x][y+1] += c[x][y];
} else {
c[x+1][y] += c[x][y];
}
}
long long calc() {
res = 0;
for (int i = 1; i <= n; i++) {
res += 1LL * v[n+1][i] * c[n+1][i];
res += 1LL * v[i][n+1] * c[i][n+1];
}
return res;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
char ch;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> ch;
d[i][j] = (ch == 'R') ? 1 : 2;
}
cin >> v[i][n+1];
}
for (int i = 1; i <= n; i++) {
cin >> v[n+1][i];
}
memset(c, 0, sizeof(c));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
init(i, j);
}
}
cout << calc() << '\n';
cin >> q;
while (q--) {
int x, y;
cin >> x >> y;
if (d[x][y] == 1) {
c[x][y+1] -= c[x][y];
upd(x, y+1, -c[x][y]);
d[x][y] = 2;
} else {
c[x+1][y] -= c[x][y];
upd(x+1, y, -c[x][y]);
d[x][y] = 1;
}
upd(x, y, c[x][y]);
cout << calc() << '\n';
}
return 0;
}