CSES - Aalto Competitive Programming 2024 - wk4 - Mon - Results
Submission details
Task:Forest density
Sender:fabiank
Submission time:2024-09-23 16:44:43 +0300
Language:C++ (C++20)
Status:READY
Result:
Test results
testverdicttime
#10.00 sdetails
#20.01 sdetails
#30.01 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++)
    {
        string line;
        getline(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:

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

correct output
10
14
5
7
8
...

user output
(empty)

Test 2

Verdict:

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

correct output
41079
2824
15631
1548
8483
...

user output
(empty)

Test 3

Verdict:

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

correct output
1000000
1000000
1000000
1000000
1000000
...

user output
(empty)