목차

배열 구간합 놀이

ps
링크acmicpc.net/…
출처BOJ
문제 번호24838
문제명배열 구간합 놀이
레벨골드 4
분류

구간합

시간복잡도O(m+nlogn)
인풋사이즈m<=200,000, n<=50,000
사용한 언어Python 3.11
제출기록46992KB / 2444ms
최고기록2444ms
해결날짜2023/08/01

풀이

코드

"""Solution code for "BOJ 24838. 배열 구간합 놀이".

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

import collections
import math
import sys

MOD = 10**9 + 7


def main():
    T = int(sys.stdin.readline())
    for _ in range(T):
        n, m = [int(x) for x in sys.stdin.readline().split()]
        count_delta = [0] * (n + 1)
        A = [int(x) for x in sys.stdin.readline().split()]
        for _ in range(m):
            x, y = [int(x) for x in sys.stdin.readline().split()]
            count_delta[x - 1] += 1
            count_delta[y] -= 1

        v = 0
        add_count = [v := v + x for x in count_delta]
        add_count.pop()
        max_sum = sum(x * y for x, y in zip(sorted(A), sorted(add_count)))
        counter = collections.Counter(add_count)
        arrangement_count = (
            math.prod(math.factorial(v) % MOD for v in counter.values()) % MOD
        )
        print(max_sum, arrangement_count)


if __name__ == '__main__':
    main()