Submission details
Task:Forest density
Sender:aalto26dm_037
Submission time:2026-09-21 16:59:07 +0300
Language:Python3 (PyPy3)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.04 sdetails
#2ACCEPTED0.67 sdetails
#3ACCEPTED0.58 sdetails

Code

n, q = list(map(int, input().split()))
tab = []
for j in range(n):
    f = lambda x : 1 if x=="*" else 0
    row = list(map(f, input()))
    # print(row)
    tab.append(row)
# print("tab:", tab)
dp = [[0]*n]
# dp[0] = tab[0].copy()
sum_line=0
for j in range(n):
    sum_line+= tab[0][j]
    dp[0][j]=sum_line
# print(dp)
for i in range(1,n):
    new_row = [0]*n
    sum_line = 0
    for j in range(n):
        sum_line += tab[i][j]
        # print(sum_line)
        new_row[j] = dp[i-1][j]+sum_line
    dp.append(new_row)

# print(dp)
for query in range(q):
    i1, j1, i2, j2 = list(map(int, input().split()))
    rec = dp[i2-1][j2-1]
    # print(rec)
    if i1>=2 : rec -= dp[i1-2][j2-1]
    if j1>=2 : rec -= dp[i2-1][j1-2]
    if i1 >= 2 and j1>=2:
        rec += dp[i1-2][j1-2]
    print(rec)

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