Submission details
Task:Forest density
Sender:aalto26dm_025
Submission time:2026-09-21 17:16:25 +0300
Language:C++ (C++20)
Status:READY
Result:
Test results
testverdicttime
#10.00 sdetails
#2--details
#3--details

Code

#include <iostream>
#include <vector>
#include <cmath>
#include <bits/stdc++.h>

using namespace std;

int sum_in_range(int a, int b, vector<int> & tree)
{
    int n = tree.size() / 2;
    a += n; b += n;
    int s = 0;
    while (a <= b)
    {
        if (a%2 == 1) s += tree[a++];
        if (b%2 == 0) s += tree[b--];
        a /= 2; b /= 2;
    }
    return s;
}

int sum_in_area(int y1, int x1, int y2, int x2, vector<vector<int>> forest)
{
    int sum = 0;
    for(int i = x1; i < x2 + 1; ++i)
    {
        sum += sum_in_range(y1, y2, forest[i]);
    }
    return sum;
}

void add(int k, int x, vector<int> & tree)
{
    int n = tree.size() / 2;
    k += n;
    tree[k] = x;
    for (k /= 2; k >= 1; k /= 2)
    {
        tree[k] = tree[2*k] + tree[2*k+1];
    }
}


int main()
{
    int forest_size, n_queries;
    cin >> forest_size >> n_queries;

    int base_array_size = pow(2, ceil(log2(forest_size)));
    int full_array_size = 2 * base_array_size;
    vector<vector<int>> forest(forest_size, vector<int>(full_array_size, 0));

    for(int l = 0; l < forest_size; ++l)
    {
        for(int c = 0; c < forest_size; ++c)
        {
            char temp;
            cin >> temp;
            if(temp == '.') add(c, 0, forest[l]);
            if(temp == '*') add(c, 1, forest[l]);
        }
    }

    for(int l = 0; l < forest_size; ++l)
    {
        for(int c = base_array_size; c < base_array_size + forest_size; ++c)
        {
            cout << forest[l][c];
        }
        cout << endl;
    }


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

        cout << sum_in_area(x1 - 1, y1 - 1, x2 - 1, y2 - 1, forest) << endl;
    }
 


}

Test details

Test 1

Verdict:

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

correct output
10
14
5
7
8
...

user output
1101010110
1011010010
0111110110
1100001110
0111100011
...

Feedback: Output is longer than expected

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)