Submission details
Task:Forest density
Sender:aalto26dm_028
Submission time:2026-09-21 17:16:01 +0300
Language:Python3 (PyPy3)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.04 sdetails
#2ACCEPTED0.32 sdetails
#3ACCEPTED0.28 sdetails

Code

import sys


def main():
    data = sys.stdin.buffer.read().split()
    it = iter(data)
    n = int(next(it))
    q = int(next(it))
    forest = [
        list(map(lambda c: 0 if c == "." else 1, list((str(next(it))).split("'")[1])))
        for _ in range(n)
    ]

    premade = []
    for i in range(n):
        premade.append([])
        for j in range(n):
            val = forest[i][j]
            if i > 0:
                val += premade[i - 1][j]
            if j > 0:
                val += premade[i][j - 1]
            if i > 0 and j > 0:
                val -= premade[i - 1][j - 1]
            premade[i].append(val)

    # for i in range(n):
    #     print(forest[i])
    # print("-----")
    # for i in range(n):
    #     print(premade[i])

    for _ in range(q):
        x1 = int(next(it)) - 1
        y1 = int(next(it)) - 1
        x2 = int(next(it)) - 1
        y2 = int(next(it)) - 1

        val = premade[x2][y2]
        if x1 > 0:
            val -= premade[x1 - 1][y2]
        if y1 > 0:
            val -= premade[x2][y1 - 1]
        if x1 > 0 and y1 > 0:
            val += premade[x1 - 1][y1 - 1]
        print(val)


main()

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