๋ฌธ์
https://www.acmicpc.net/problem/2042
ํ์ด
# ๋ฐฑ์ค 2042๋ฒ ๋ฌธ์ - ๊ตฌ๊ฐ ํฉ ๊ตฌํ๊ธฐ
# 0. ์
๋ ฅ๋ฐ๊ธฐ
import sys
input = sys.stdin.readline
n, m, k = map(int,input().split())
arr = []
segment_tree = [0]*(n*4)
# 1. ํธ๋ฆฌ ๋ง๋ค๊ธฐ
def init(start, end, index):
# start์ end๊ฐ ๊ฐ๋ค๋ฉด ๋ฆฌํ๋
ธ๋์ด๋ค.
if start == end:
segment_tree[index] = arr[start-1]
return segment_tree[index]
# ํ์ฌ ๋
ธ๋๋ ์ผ์ชฝ ์๋ ๋
ธ๋์ ์ค๋ฅธ์ชฝ ์๋ ๋
ธ๋๋ฅผ ๋ํ ๊ฐ์ด๋ค.
mid = (start+end) // 2
segment_tree[index] = init(start, mid, index*2) + init(mid+1, end, index*2+1)
return segment_tree[index]
# 2. ํธ๋ฆฌ์์ ๊ฐ ์ฐพ๊ธฐ
def find(start, end, index, left, right):
# ์ฐพ์ผ๋ ค๋ ๋ฒ์๊ฐ start~end ๋ฒ์๋ณด๋ค ํด ๊ฒฝ์ฐ
if start > right or end < left:
return 0
# ์ฐพ์ผ๋ ค๋ ๋ฒ์๊ฐ segment tree ๋
ธ๋์์ ๊ตฌํ๋์ด ์์ ๊ฒฝ์ฐ
if start >= left and end <= right:
return segment_tree[index]
# ์ฝ๋๋ฅผ ๋์์ํค๊ธฐ ์ํ ๊ธฐ๋ณธ ์ฝ๋
# ํ์ฌ ๋
ธ๋๋ ์ผ์ชฝ์๋ + ์ค๋ฅธ์ชฝ์๋ ๋
ธ๋์ด๋ค.
mid = (start + end) // 2
sub_sum = find(start, mid, index*2, left, right) + find(mid+1, end, index*2+1, left, right)
return sub_sum
# 3. ํธ๋ฆฌ ๊ฐ ๋ฐ๊ฟ์ฃผ๊ธฐ
def update(start, end, index, update_idx, update_data):
# update ํ๋ ค๋ ๋ฒ์๊ฐ ์ด๊ณผ๋ ๊ฒฝ์ฐ
if start > update_idx or end < update_idx:
return
segment_tree[index] += update_data
# ๋ฆฌํ๋
ธ๋๊น์ง ๋ฐ๊ฟ์ฃผ์์ผ๋ฉด ์ฌ๊ทํจ์๋ฅผ ๋๋ธ๋ค.
if start == end:
return
# ๋ด๊ฐ ๊ด์ฌํ๊ณ ์๋ ๋
ธ๋๋ค์ ์ฐพ์์ ๋ฐ๊ฟ์ค๋ค -> ์ฌ๊ทํจ์๋ก ๊ตฌํ
mid = (start + end) // 2
update(start, mid, index*2, update_idx, update_data)
update(mid+1, end, index*2+1, update_idx, update_data)
for _ in range(n):
arr.append(int(input()))
init(1, n, 1)
for _ in range(m+k):
a, b, c = map(int,input().split())
if a == 1:
temp = c - arr[b-1]
arr[b-1] = c
update(1, n, 1, b, temp)
elif a == 2:
print(find(1, n, 1, b, c))
์ธ๊ทธ๋จผํธ ํธ๋ฆฌ ๋ฌธ์ ์ด๋ค. ์ฒ์ ์ ํ ์๊ณ ๋ฆฌ์ฆ์ด์ด์ ํด๊ฒฐํ๋๋ฐ ์ค๋ ๊ฑธ๋ ธ๋ค.
'๐๏ธ Algorithm > ๐ฉ ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐ฉ [๋ฐฑ์ค] [Python] [Gold5] 5582๋ฒ_๊ณตํต ๋ถ๋ถ ๋ฌธ์์ด (0) | 2023.04.26 |
---|---|
๐ฉ [๋ฐฑ์ค] [Python] [Silver1] 6588๋ฒ_๊ณจ๋๋ฐํ์ ์ถ์ธก (0) | 2023.04.25 |
๐ฉ [๋ฐฑ์ค] [Python] [Silver3] 11478๋ฒ_์๋ก ๋ค๋ฅธ ๋ถ๋ถ ๋ฌธ์์ด์ ๊ฐ์ (0) | 2023.04.23 |
๐ฉ [๋ฐฑ์ค] [Python] [Silver3] 2512๋ฒ_์์ฐ (0) | 2023.04.22 |
๐ฉ [๋ฐฑ์ค] [Python] [Gold5] 1107๋ฒ_๋ฆฌ๋ชจ์ปจ (1) | 2023.04.21 |