https://www.acmicpc.net/problem/2042
💡 해결방법
# 2042 구간 합 구하기
import sys
input = sys.stdin.readline
N, M, K = map(int, input().split())
arr = [int(input()) for _ in range(N)]
tree = [0] * (N*4)
def init(start, end, idx):
if(start == end):
tree[idx] = arr[start]
return tree[idx]
mid = (start+end) // 2
tree[idx] = init(start, mid, idx*2) + init(mid+1, end, idx*2+1)
return tree[idx]
def interval(start, end, idx, left, right):
if (end < left or right < start):
return 0;
if (left <= start and end <= right):
return tree[idx]
mid = (start+end)//2
return interval(start,mid,idx*2,left,right) + interval(mid+1, end, idx*2+1,left,right)
def modify(start, end, idx, where, value):
if (where < start or end < where):
return
tree[idx] -= value
if (start == end):
return
mid = (start+end) // 2
modify(start, mid, idx*2, where, value)
modify(mid+1, end, idx*2 +1, where, value)
init(0, N-1, 1)
repeat = M+K
for _ in range(repeat):
a, b, c = list(map(int, input().split()))
if (a == 1):
modify(0, N-1, 1, b-1, arr[b-1] - c)
arr[b-1] = c
else:
print(interval(0, N-1, 1, b-1, c-1))
'문제 풀이 > 백준' 카테고리의 다른 글
백준 11505 - 구간 곱 구하기 (Python3) (0) | 2025.03.31 |
---|---|
[G5] 백준 2225 - 합분해 (python3) (0) | 2025.03.17 |
[G5] 백준 2230 - 수 고르기 (Python3) (0) | 2025.03.01 |
[G2] 백준 7453 - 합이 0인 네 정수 (Python3) (0) | 2025.02.25 |
[G5] 백준 3020 - 개똥벌레 (Python3) (0) | 2025.02.25 |
댓글