728x90
반응형
https://www.acmicpc.net/problem/11659
11659번: 구간 합 구하기 4
첫째 줄에 수의 개수 N과 합을 구해야 하는 횟수 M이 주어진다. 둘째 줄에는 N개의 수가 주어진다. 수는 1,000보다 작거나 같은 자연수이다. 셋째 줄부터 M개의 줄에는 합을 구해야 하는 구간 i와 j
www.acmicpc.net
최종 코드
GitHub - hwayeon351/BEAKJOON-Algorithms: 백준 알고리즘 소스 코드 모음
백준 알고리즘 소스 코드 모음. Contribute to hwayeon351/BEAKJOON-Algorithms development by creating an account on GitHub.
github.com
import sys
N, M = map(int, sys.stdin.readline().split())
nums = list(map(int, sys.stdin.readline().split()))
nums = [0] + nums
dp = [0]*(N+1)
for i in range(1, N+1):
dp[i] = dp[i-1] + nums[i]
for _ in range(M):
i, j = map(int, sys.stdin.readline().split())
print(dp[j]-dp[i-1])
GitHub - hwayeon351/BEAKJOON-Algorithms: 백준 알고리즘 소스 코드 모음
백준 알고리즘 소스 코드 모음. Contribute to hwayeon351/BEAKJOON-Algorithms development by creating an account on GitHub.
github.com
import sys
sys.setrecursionlimit(200000)
N, M = map(int, sys.stdin.readline().split())
nums = list(map(int, sys.stdin.readline().split()))
nums = [0] + nums
def cal_dp(i):
if i == 0: return 0
if dp[i] != 0:
return dp[i]
dp[i] = cal_dp(i-1) + nums[i]
return dp[i]
dp = [0]*(N+1)
for _ in range(M):
i, j = map(int, sys.stdin.readline().split())
print(cal_dp(j)-cal_dp(i-1))
728x90
반응형
'코테 노트 > 백준' 카테고리의 다른 글
백준 17142 연구소 3 C++ (0) | 2021.08.18 |
---|---|
백준 1325 효율적인 해킹 C++/PyPy (0) | 2021.08.18 |
백준 11725 트리의 부모 찾기 Python3 (0) | 2021.08.18 |
백준 17140 이차원 배열과 연산 C++ (0) | 2021.08.17 |
백준 11501 주식 Python3 (0) | 2021.08.16 |