CSES - Aalto Competitive Programming 2024 - wk4 - Mon - Results
Submission details
Task:Forest density
Sender:fabiank
Submission time:2024-09-23 16:48:57 +0300
Language:C++ (C++17)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.53 sdetails
#3ACCEPTED0.51 sdetails

Code

#include <bits/stdc++.h>

using namespace std;

void print_vector(vector<int> &x);
void print_matrix(vector<vector<int>> &matrix);

int main()
{
    int n, q;
    cin >> n >> q;

    vector<vector<int>> forest(n, vector<int>(n, 0));

    for (int i = 0; i < n; i++)
    {
        char line[n + 1];
        cin >> line;
        for (int j = 0; j < n; j++)
        {
            if (line[j] == '*')
            {
                forest[i][j] = 1;
            }
        }
    }

    vector<vector<int>> prefix_sum(n, vector<int>(n, 0));

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            int upper_value = i > 0 ? prefix_sum[i - 1][j] : 0;
            int left_value = j > 0 ? prefix_sum[i][j - 1] : 0;
            int diagional_value = i > 0 && j > 0 ? prefix_sum[i - 1][j - 1] : 0;
            prefix_sum[i][j] = upper_value + left_value - diagional_value + (int)forest[i][j];
        }
    }

    // print_matrix(prefix_sum);

    for (int i = 0; i < q; i++)
    {
        int y1, x1, y2, x2;
        cin >> y1 >> x1 >> y2 >> x2;

        int sum_a = y1 > 1 && x1 > 1 ? prefix_sum[y1 - 2][x1 - 2] : 0;
        int sum_b = y1 > 1 ? prefix_sum[y1 - 2][x2 - 1] : 0;
        int sum_c = x1 > 1 ? prefix_sum[y2 - 1][x1 - 2] : 0;
        int sum_d = prefix_sum[y2 - 1][x2 - 1];
        // cout << "A: " << sum_a << " B: " << sum_b << " c: " << sum_c << " D: " << sum_d << "\n";
        cout << sum_d - sum_b - sum_c + sum_a << "\n";
    }
}

void print_vector(vector<int> &x)
{
    for (int v : x)
    {
        cout << v << " ";
    }
    cout << "\n";
}

void print_matrix(vector<vector<int>> &matrix)
{
    cout << "\n"
         << "----------------" << "\n";
    for (vector<int> row : matrix)
    {
        print_vector(row);
    }
    cout << "\n"
         << "----------------" << "\n";
}

Test details

Test 1

Verdict: ACCEPTED

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

correct output
10
14
5
7
8
...

user output
10
14
5
7
8
...
Truncated

Test 2

Verdict: ACCEPTED

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

correct output
41079
2824
15631
1548
8483
...

user output
41079
2824
15631
1548
8483
...
Truncated

Test 3

Verdict: ACCEPTED

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

correct output
1000000
1000000
1000000
1000000
1000000
...

user output
1000000
1000000
1000000
1000000
1000000
...
Truncated