Submission details
Task:Forest density
Sender:luukwin
Submission time:2025-09-22 16:54:44 +0300
Language:Python3 (PyPy3)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.04 sdetails
#2ACCEPTED0.39 sdetails
#3ACCEPTED0.32 sdetails

Code

n, q = [int(x) for x in input().split()]

array = [[0 for _ in range(n)] for _ in range(n)]

for i in range(0, n):
    line = input()
    for j in range(0, n):
        if i != 0 and j != 0: array[i][j] = array[i-1][j] + array[i][j-1] - array[i-1][j-1]
        elif i != 0: array[i][j] = array[i-1][j]
        elif j != 0: array[i][j] = array[i][j-1]
        if line[j] == "*": array[i][j] += 1

# for line in array: print(line)

output = []
for i in range(q):
    coords = [int(x) - 1 for x in input().split()]
    answer = array[coords[2]][coords[3]]
    # print(answer)
    if coords[0] != 0: 
        # print(" 0 coords ", array[coords[0] - 1][coords[3]])
        answer -= array[coords[0] - 1][coords[3]]
    if coords[1] != 0: 
        # print("1 coord", array[coords[2]][coords[1] - 1])
        answer -= array[coords[2]][coords[1] - 1]
    if coords[1] != 0 and coords[0] != 0:
        answer += array[coords[0] - 1][coords[1]-1]
    output.append(str(answer))

print("\n".join(output))

Test details

Test 1

Verdict: ACCEPTED

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

correct output
10
14
5
7
8
...

user output
10
14
5
7
8
...
Truncated

Test 2

Verdict: ACCEPTED

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

correct output
41079
2824
15631
1548
8483
...

user output
41079
2824
15631
1548
8483
...
Truncated

Test 3

Verdict: ACCEPTED

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

correct output
1000000
1000000
1000000
1000000
1000000
...

user output
1000000
1000000
1000000
1000000
1000000
...
Truncated