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
/
3.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))