From 30ee1143fb1db0e8a54a8d4ac735bb0812d169c2 Mon Sep 17 00:00:00 2001 From: sm4640 Date: Fri, 27 Mar 2026 12:27:47 +0900 Subject: [PATCH] =?UTF-8?q?11659-s3=20=EC=84=B1=EA=B3=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- workbook_7319/silver/11659-s3.py | 35 ++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 workbook_7319/silver/11659-s3.py diff --git a/workbook_7319/silver/11659-s3.py b/workbook_7319/silver/11659-s3.py new file mode 100644 index 0000000..2b54e34 --- /dev/null +++ b/workbook_7319/silver/11659-s3.py @@ -0,0 +1,35 @@ +# 구간 합 구하기4 + +import sys + +input = sys.stdin.readline +def input_ints(): + return map(int, input().rstrip().split()) + +def solution(): + n, m = input_ints() + + lst = [0]+list(input_ints()) + + prefix_sum = lst[:] + for i in range(1, n+1): + prefix_sum[i] = prefix_sum[i-1]+lst[i] + + for _ in range(m): + i, j = input_ints() + print(prefix_sum[j]-prefix_sum[i-1]) + + + return + + +solution() + +""" +걸린 시간: 9분 + +시간 복잡도: 누적합 리스트를 채우는데 O(n)이 걸린다. + +해설: 구간 합을 m번 계산해야하는데, m이 100,000이하이므로 썼던 것을 재활용하지 않으면 시간초과가 날 것 같았다. +구간합은 누적합 - 누적합으로 계산할 수 있기 때문에 누적합을 처음에 계산해놓고 진행했다. +""" \ No newline at end of file