목차

어두운 건 무서워

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()