【題解】CSES 1652 Forest Queries

【題目敘述】https://cses.fi/problemset/task/1652/

#include <iostream>
using namespace std;
 
int n, q, x1, y1, x2, y2, pre[1005][1005];
char c;
 
int main() {
    cin >> n >> q;
    for (int i = 1; i <= n; i++){
        for (int j = 1; j <= n; j++){
            cin >> c;
            if (c == '*') pre[i][j] = 1;
            pre[i][j] += pre[i][j-1];
            pre[i][j] += pre[i-1][j];
            pre[i][j] -= pre[i-1][j-1];
        }
    }
    for (int i = 0; i < q; i++){
        cin >> x1 >> y1 >> x2 >> y2;
        cout << pre[x2][y2]-pre[x1-1][y2]-pre[x2][y1-1]+pre[x1-1][y1-1] << "\n";
    }
}
分享本文 Share with friends