- Notifications
You must be signed in to change notification settings - Fork 846
/
Copy path3.py
43 lines (36 loc) · 1.15 KB
/
3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
importsys
input=sys.stdin.readline
# 데이터의 개수(n), 변경 횟수(m), 구간 합 계산 횟수(k)
n, m, k=map(int, input().split())
# 전체 데이터의 개수는 최대 1,000,000개
arr= [0] * (n+1)
tree= [0] * (n+1)
# i번째 수까지의 누적 합을 계산하는 함수
defprefix_sum(i):
result=0
whilei>0:
result+=tree[i]
# 0이 아닌 마지막 비트만큼 빼가면서 이동
i-= (i&-i)
returnresult
# i번째 수를 dif만큼 더하는 함수
defupdate(i, dif):
whilei<=n:
tree[i] +=dif
i+= (i&-i)
# start부터 end까지의 구간 합을 계산하는 함수
definterval_sum(start, end):
returnprefix_sum(end) -prefix_sum(start-1)
foriinrange(1, n+1):
x=int(input())
arr[i] =x
update(i, x)
foriinrange(m+k):
a, b, c=map(int, input().split())
# 업데이트(update) 연산인 경우
ifa==1:
update(b, c-arr[b]) # 바뀐 크기(dif)만큼 적용
arr[b] =c
# 구간 합(interval sum) 연산인 경우
else:
print(interval_sum(b, c))