목차

Breed Counting

ps
링크acmicpc.net/…
출처BOJ
문제 번호11969
문제명Breed Counting
레벨실버 3
분류

누적합

시간복잡도O(n+q)
인풋사이즈n<=100,000, q<=100,000
사용한 언어Python
제출기록37820KB / 520ms
최고기록424ms
해결날짜2022/05/29
태그

[라이] 구간합 배열

풀이

코드

"""Solution code for "BOJ 11969. Breed Counting".

- Problem link: https://www.acmicpc.net/problem/11969
- Solution link: http://www.teferi.net/ps/problems/boj/11969

Tags: [Prefix sum]
"""

import sys


def main():
    N, Q = [int(x) for x in sys.stdin.readline().split()]  # pylint: disable=unused-variable
    ids = [sys.stdin.readline().rstrip() for _ in range(N)]

    prefix_sums = [[v := 0] + [v := (v + 1 if x == '1' else v) for x in ids],
                   [v := 0] + [v := (v + 1 if x == '2' else v) for x in ids],
                   [v := 0] + [v := (v + 1 if x == '3' else v) for x in ids]]
    for _ in range(Q):
        a, b = [int(x) for x in sys.stdin.readline().split()]
        print(*(psum[b] - psum[a - 1] for psum in prefix_sums))


if __name__ == '__main__':
    main()