记录编号 607098 评测结果 AAAAAAAAAAAAAAA
题目名称 3863.[USACO23 Jan Silver] Following Directions 最终得分 100
用户昵称 Gravatar淮淮清子 是否通过 通过
代码语言 C++ 运行时间 1.539 s
提交时间 2025-10-05 15:51:40 内存使用 20.61 MiB
显示代码纯文本
#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;
}