Submission details
Task:Forest density
Sender:aalto26dm_007
Submission time:2026-09-21 16:55:00 +0300
Language:Python3 (PyPy3)
Status:READY
Result:
Test results
testverdicttime
#10.06 sdetails
#20.19 sdetails
#30.20 sdetails

Code

import sys

class PrefixSum2D:
    def __init__(self, grid):
        r = len(grid)
        c = len(grid[0]) if r else 0
        self.p = [[0] * (c + 1) for _ in range(r + 1)]
        for i in range(r):
            for j in range(c):
                self.p[i + 1][j + 1] = (
                    grid[i][j]
                    + self.p[i][j + 1]
                    + self.p[i + 1][j]
                    - self.p[i][j]
                )

    def sum_rect(self, r1, c1, r2, c2):
        return (
            self.p[r2 + 1][c2 + 1]
            - self.p[r1][c2 + 1]
            - self.p[r2 + 1][c1]
            + self.p[r1][c1]
        )

TOK = None

def feed(src=None):
    global TOK
    raw = src.encode() if src is not None else sys.stdin.buffer.read()
    TOK = iter(raw.split())

def ni():
    return int(next(TOK))

def ns():
    return next(TOK).decode()

def read_grid(n):
    return [ns() for _ in range(n)]


feed()
n, m = ni(), ni()
g = read_grid(n)

prefix2D = PrefixSum2D(g)

out = []

for _ in range(m):
    a1, a2, b1, b2 = ni(), ni(), ni(), ni()
    out.append(str(prefix2D.sum_rect(a1 - 1, a2 -1, b1 - 1, b2 - 1)))

sys.stdout.write("\n".join(out) + ("\n" if out else ""))

Test details

Test 1

Verdict:

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

correct output
10
14
5
7
8
...

user output
(empty)

Error:
Traceback (most recent call last):
  File "/box/input/code.py", line 46, in <module>
    prefix2D = PrefixSum2D(g)
  File "/box/input/code.py", line 11, in __init__
    grid[i][j]
TypeError: unsupported operand type(s) for +: 'str' and 'int'

Test 2

Verdict:

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

correct output
41079
2824
15631
1548
8483
...

user output
(empty)

Error:
Traceback (most recent call last):
  File "/box/input/code.py", line 46, in <module>
    prefix2D = PrefixSum2D(g)
  File "/box/input/code.py", line 11, in __init__
    grid[i][j]
TypeError: unsupported operand type(s) for +: 'str' and 'int'

Test 3

Verdict:

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

correct output
1000000
1000000
1000000
1000000
1000000
...

user output
(empty)

Error:
Traceback (most recent call last):
  File "/box/input/code.py", line 46, in <module>
    prefix2D = PrefixSum2D(g)
  File "/box/input/code.py", line 11, in __init__
    grid[i][j]
TypeError: unsupported operand type(s) for +: 'str' and 'int'