| Task: | Forest density |
| Sender: | yoshifumi_k |
| Submission time: | 2025-09-22 17:05:55 +0300 |
| Language: | Python3 (PyPy3) |
| Status: | READY |
| Result: | ACCEPTED |
| test | verdict | time | |
|---|---|---|---|
| #1 | ACCEPTED | 0.04 s | details |
| #2 | ACCEPTED | 0.71 s | details |
| #3 | ACCEPTED | 0.65 s | details |
Code
def forest_density():
n, q = map(int, input().split())
trees_sum = []
for i in range(n):
arr = list(input())
trees_sum.append([0] * n)
count = 0
for j in range (n):
if arr[j] == "*":
count += 1
if i == 0:
trees_sum[i][j] = count
else:
trees_sum[i][j] = count + trees_sum[i - 1][j]
# print(trees_sum)
for _ in range(q):
y1, x1, y2, x2 = map(int, input().split())
y1 -= 1
x1 -= 1
y2 -= 1
x2 -= 1
if y1 == 0 and x1 == 0:
print(trees_sum[y2][x2])
elif y1 == 0:
print(trees_sum[y2][x2] - trees_sum[y2][x1 - 1])
elif x1 == 0:
print(trees_sum[y2][x2] - trees_sum[y1 - 1][x2])
else:
print(trees_sum[y2][x2] - trees_sum[y2][x1 - 1] - trees_sum[y1 - 1][x2] + trees_sum[y1 - 1][x1 - 1])
if __name__ == "__main__":
forest_density()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 |
