Submission details
Task:Forest density
Sender:aalto26dm_010
Submission time:2026-09-21 17:08:04 +0300
Language:Python3 (PyPy3)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.04 sdetails
#2ACCEPTED0.62 sdetails
#3ACCEPTED0.55 sdetails

Code

n, q = map(int, input().split())

forest = [[] for _ in range(n)]
for y in range(n):
    row = input().strip()
    row_trees = 0
    for x, c in enumerate(row):
        if c == "*":
            row_trees += 1
        temp = row_trees
        if y != 0:
            temp += forest[y-1][x]
        forest[y].append(temp)

def get_value(arr: list, y: int, x: int):
    if y < 0 or x < 0:
        return 0
    return arr[y][x]

for _ in range(q):
    y1, x1, y2, x2 = map(lambda x: int(x) - 1, input().split())
    A = get_value(forest, y2, x2)
    B = get_value(forest, y2, x1-1)
    C = get_value(forest, y1-1, x2)
    D = get_value(forest, y1-1, x1-1)
    count = A - B - C + D
    print(count)

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
...