Submission details
Task:Forest density
Sender:aalto26dm_023
Submission time:2026-09-21 17:25:02 +0300
Language:C++ (C++20)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.58 sdetails
#3ACCEPTED0.56 sdetails

Code

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n, q;
    cin >> n >> q;
    // vector<vector<char>> grid(n);
    char grid[n][n];

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            char t;
            cin >> t;
            // grid[i].push_back(t);
            grid[i][j] = t;
        }
    }

    // vector<vector<int>> query(q);
    int query[q][4];
    for (int i = 0; i < q; i++) {
        for (int j = 0; j < 4; j++) {
            int t;
            cin >> t;
            // query[i].push_back(t - 1);
            query[i][j] = t - 1;
        }
    }

    
    int sum[n][n];
    for (int i = 0; i < n; i++) {
        int curr = 0;
        for (int j = 0; j < n; j++) {
            int last;
            if (i == 0) last = 0;
            else last = sum[i - 1][j];

            if (grid[i][j] == '*') curr++;
            sum[i][j] = curr + last;
        }
    }

    for (int i = 0; i < q; i++) {
        int ay = query[i][2], ax = query[i][3],
            by = query[i][2], bx = query[i][1] - 1,
            cy = query[i][0] - 1, cx = query[i][3],
            dy = query[i][0] - 1, dx = query[i][1] - 1;

            int a, b, c, d;
            a = sum[ay][ax];
            if (bx < 0) b = 0;
            else b = sum[by][bx];
            if (cy < 0) c = 0;
            else c = sum[cy][cx];
            if (dx < 0 || dy < 0) d = 0;
            else d = sum[dy][dx];

        int ans =  a + d - b - c;
        cout << ans << endl;
    }



}

Test details

Test 1

Verdict: ACCEPTED

input
10 100
**.*.*.**.
*.**.*..*.
.*****.**.
**....***.
...

correct output
10
14
5
7
8
...

user output
10
14
5
7
8
...

Test 2

Verdict: ACCEPTED

input
1000 200000
**.**.****..**.***..**.***.**....

correct output
41079
2824
15631
1548
8483
...

user output
41079
2824
15631
1548
8483
...

Test 3

Verdict: ACCEPTED

input
1000 200000
******************************...

correct output
1000000
1000000
1000000
1000000
1000000
...

user output
1000000
1000000
1000000
1000000
1000000
...