ps | |
---|---|
링크 | acmicpc.net/… |
출처 | BOJ |
문제 번호 | 16507 |
문제명 | 어두운 건 무서워 |
레벨 | 실버 1 |
분류 |
누적합 |
시간복잡도 | O(r*c + q) |
인풋사이즈 | r<=1000, c<=1000, q<=10000 |
사용한 언어 | Python |
제출기록 | 108860KB / 472ms |
최고기록 | 472ms |
해결날짜 | 2022/05/30 |
태그 |
"""Solution code for "BOJ 16507. 어두운 건 무서워".
- Problem link: https://www.acmicpc.net/problem/16507
- Solution link: http://www.teferi.net/ps/problems/boj/16507
Tags: [Prefix sum]
"""
import sys
def create_2d_prefix_sum(nums):
prefix_sum = [[0] * (len(nums[0]) + 1)]
for row in nums:
prefix_sum.append([ps := 0] +
[(ps := ps + num) + p
for num, p in zip(row, prefix_sum[-1][1:])])
return prefix_sum
def main():
# pylint: disable=unused-variable
R, C, Q = [int(x) for x in sys.stdin.readline().split()]
nums = [[int(x) for x in sys.stdin.readline().split()] for _ in range(R)]
prefix_sum = create_2d_prefix_sum(nums)
for _ in range(Q):
r1, c1, r2, c2 = [int(x) for x in sys.stdin.readline().split()]
pixel_sum = (prefix_sum[r2][c2] - prefix_sum[r1 - 1][c2] -
prefix_sum[r2][c1 - 1] + prefix_sum[r1 - 1][c1 - 1])
size = (r2 - r1 + 1) * (c2 - c1 + 1)
print(pixel_sum // size)
if __name__ == '__main__':
main()