forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path3.py
More file actions
43 lines (36 loc) Β· 1.15 KB
/
Copy path3.py
File metadata and controls
43 lines (36 loc) Β· 1.15 KB
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
import sys
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λ²μ§Έ μκΉμ§μ λμ ν©μ κ³μ°νλ ν¨μ
def prefix_sum(i):
result = 0
while i > 0:
result += tree[i]
# 0μ΄ μλ λ§μ§λ§ λΉνΈλ§νΌ λΉΌκ°λ©΄μ μ΄λ
i -= (i & -i)
return result
# iλ²μ§Έ μλ₯Ό difλ§νΌ λνλ ν¨μ
def update(i, dif):
while i <= n:
tree[i] += dif
i += (i & -i)
# startλΆν° endκΉμ§μ κ΅¬κ° ν©μ κ³μ°νλ ν¨μ
def interval_sum(start, end):
return prefix_sum(end) - prefix_sum(start - 1)
for i in range(1, n + 1):
x = int(input())
arr[i] = x
update(i, x)
for i in range(m + k):
a, b, c = map(int, input().split())
# μ
λ°μ΄νΈ(update) μ°μ°μΈ κ²½μ°
if a == 1:
update(b, c - arr[b]) # λ°λ ν¬κΈ°(dif)λ§νΌ μ μ©
arr[b] = c
# κ΅¬κ° ν©(interval sum) μ°μ°μΈ κ²½μ°
else:
print(interval_sum(b, c))