๐Ÿ”ฅ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ํŒ์™•, ์„ธ๊ทธ๋จผํŠธ ํŠธ๋ฆฌ๋กœ ๊ตฌ๊ฐ„ ๋ฌธ์ œ ์™„๋ฒฝ ์ •๋ณตํ•˜๊ธฐ! ๐Ÿ’ช

๊น€์ƒ์šฑยท2024๋…„ 9์›” 16์ผ
0
post-thumbnail

์„ธ๊ทธ๋จผํŠธ ํŠธ๋ฆฌ(Segment Tree) ๐ŸŒณ

์„ธ๊ทธ๋จผํŠธ ํŠธ๋ฆฌ(Segment Tree)๋Š” ์ฃผ๋กœ ๊ตฌ๊ฐ„ ์ฟผ๋ฆฌ(range query)์™€ ๊ตฌ๊ฐ„ ์—…๋ฐ์ดํŠธ(range update)๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ์ž๋ฃŒ ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. ์„ธ๊ทธ๋จผํŠธ ํŠธ๋ฆฌ๋Š” ์ด์ง„ ํŠธ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๋ฉฐ, ๊ฐ ๋…ธ๋“œ๋Š” ํŠน์ • ๊ตฌ๊ฐ„์˜ ๊ฐ’์„ ์ €์žฅํ•ด ๊ตฌ๊ฐ„ํ•ฉ, ์ตœ์†Ÿ๊ฐ’, ์ตœ๋Œ“๊ฐ’ ๋“ฑ์„ ๋น ๋ฅด๊ฒŒ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์–ด์š”. โšก


์„ธ๊ทธ๋จผํŠธ ํŠธ๋ฆฌ์˜ ๊ตฌ์„ฑ ๐Ÿ—๏ธ

์„ธ๊ทธ๋จผํŠธ ํŠธ๋ฆฌ๋Š” ๋ฐฐ์—ด์˜ ๊ฐ’์„ ํŠธ๋ฆฌ ํ˜•ํƒœ๋กœ ํ‘œํ˜„ํ•œ ์ž๋ฃŒ ๊ตฌ์กฐ๋กœ, ๊ฐ ๋…ธ๋“œ๋Š” ๋ฐฐ์—ด์˜ ๋ถ€๋ถ„ ๊ตฌ๊ฐ„์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

  • ๋ฆฌํ”„ ๋…ธ๋“œ ๐Ÿƒ: ๋ฐฐ์—ด์˜ ๊ฐ ์›์†Œ๊ฐ€ ๋ฆฌํ”„ ๋…ธ๋“œ์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.
  • ๋‚ด๋ถ€ ๋…ธ๋“œ ๐ŸŒฟ: ๋‚ด๋ถ€ ๋…ธ๋“œ๋Š” ์ž์‹ ์ด ๋‚˜ํƒ€๋‚ด๋Š” ๊ตฌ๊ฐ„์˜ ๊ฐ’์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๊ตฌ๊ฐ„ ํ•ฉ ์„ธ๊ทธ๋จผํŠธ ํŠธ๋ฆฌ์—์„œ๋Š” ํ•ด๋‹น ๊ตฌ๊ฐ„์˜ ํ•ฉ์„ ์ €์žฅํ•ด์š”.

์„ธ๊ทธ๋จผํŠธ ํŠธ๋ฆฌ์˜ ํŠน์ง• ๐Ÿง

  1. ํŠธ๋ฆฌ์˜ ๋†’์ด โฌ†๏ธ: ์„ธ๊ทธ๋จผํŠธ ํŠธ๋ฆฌ๋Š” ์ด์ง„ ํŠธ๋ฆฌ์ด๋ฏ€๋กœ ๋†’์ด๋Š” ๋ฐฐ์—ด์˜ ํฌ๊ธฐ (n)์— ๋Œ€ํ•ด (log n)์— ๋น„๋ก€ํ•ฉ๋‹ˆ๋‹ค.
  2. ๊ณต๊ฐ„ ๋ณต์žก๋„ ๐Ÿ’พ: ๋ฐฐ์—ด ํฌ๊ธฐ๊ฐ€ (n)์ผ ๋•Œ, ์„ธ๊ทธ๋จผํŠธ ํŠธ๋ฆฌ๋Š” ์•ฝ (4n)๊ฐœ์˜ ๋…ธ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  3. ์‹œ๊ฐ„ ๋ณต์žก๋„ โฑ๏ธ:
    • ๊ตฌ๊ฐ„ ํ•ฉ ๋˜๋Š” ๊ตฌ๊ฐ„ ์ตœ์†Œ/์ตœ๋Œ€ ๋“ฑ์˜ ์ฟผ๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐ (O(log n)) ์‹œ๊ฐ„์ด ์†Œ์š”๋ฉ๋‹ˆ๋‹ค.
    • ์—…๋ฐ์ดํŠธ ์ž‘์—…๋„ (O(log n)) ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฝ๋‹ˆ๋‹ค.

์„ธ๊ทธ๋จผํŠธ ํŠธ๋ฆฌ์˜ ์‚ฌ์šฉ ๋ฐฉ๋ฒ• ๐Ÿ› ๏ธ

1. ์„ธ๊ทธ๋จผํŠธ ํŠธ๋ฆฌ ์ƒ์„ฑ ๐ŸŒฑ

์„ธ๊ทธ๋จผํŠธ ํŠธ๋ฆฌ๋Š” ๋ฐฐ์—ด์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ƒ์„ฑ๋˜๋ฉฐ, ํŠธ๋ฆฌ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐ (O(n)) ์‹œ๊ฐ„์ด ์†Œ์š”๋ฉ๋‹ˆ๋‹ค.

def build_segment_tree(arr, tree, node, start, end):
    if start == end:
        # ๋ฆฌํ”„ ๋…ธ๋“œ์— ๋ฐฐ์—ด์˜ ๊ฐ’์„ ์ €์žฅ
        tree[node] = arr[start]
    else:
        mid = (start + end) // 2
        # ์™ผ์ชฝ ์ž์‹ ๋…ธ๋“œ
        build_segment_tree(arr, tree, 2 * node, start, mid)
        # ์˜ค๋ฅธ์ชฝ ์ž์‹ ๋…ธ๋“œ
        build_segment_tree(arr, tree, 2 * node + 1, mid + 1, end)
        # ๋ถ€๋ชจ ๋…ธ๋“œ๋Š” ์ž์‹ ๋…ธ๋“œ๋“ค์˜ ๊ฐ’์„ ํ•ฉ์นจ
        tree[node] = tree[2 * node] + tree[2 * node + 1]

2. ๊ตฌ๊ฐ„ ์ฟผ๋ฆฌ ๐Ÿ”

์›ํ•˜๋Š” ๊ตฌ๊ฐ„ [L, R]์˜ ๊ฐ’์„ ์ฐพ๊ธฐ ์œ„ํ•ด ํŠธ๋ฆฌ์—์„œ ๊ตฌ๊ฐ„ ์ฟผ๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

def range_query(tree, node, start, end, L, R):
    if R < start or end < L:
        # ๊ตฌ๊ฐ„์ด ์™„์ „ํžˆ ๊ฒน์น˜์ง€ ์•Š์œผ๋ฉด ๋ฌด์‹œ
        return 0
    if L <= start and end <= R:
        # ๊ตฌ๊ฐ„์ด ์™„์ „ํžˆ ํฌํ•จ๋˜๋ฉด ํ˜„์žฌ ๋…ธ๋“œ ๊ฐ’์„ ๋ฐ˜ํ™˜
        return tree[node]
    
    # ๋ถ€๋ถ„์ ์œผ๋กœ ๊ฒน์น˜๋Š” ๊ฒฝ์šฐ ์ž์‹ ๋…ธ๋“œ๋กœ ์ฟผ๋ฆฌ ๋ถ„ํ• 
    mid = (start + end) // 2
    left_sum = range_query(tree, 2 * node, start, mid, L, R)
    right_sum = range_query(tree, 2 * node + 1, mid + 1, end, L, R)
    
    return left_sum + right_sum

3. ๊ฐ’ ์—…๋ฐ์ดํŠธ ๐Ÿ“Š

๋ฐฐ์—ด์˜ ํŠน์ • ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ๋•Œ, ํ•ด๋‹น ๊ฐ’์ด ํฌํ•จ๋œ ๊ตฌ๊ฐ„์˜ ๋…ธ๋“œ๋“ค๋„ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค.

def update(tree, node, start, end, idx, val):
    if start == end:
        # ๋ฆฌํ”„ ๋…ธ๋“œ ์—…๋ฐ์ดํŠธ
        tree[node] = val
    else:
        mid = (start + end) // 2
        if start <= idx <= mid:
            # ์™ผ์ชฝ ์ž์‹ ๋…ธ๋“œ ์—…๋ฐ์ดํŠธ
            update(tree, 2 * node, start, mid, idx, val)
        else:
            # ์˜ค๋ฅธ์ชฝ ์ž์‹ ๋…ธ๋“œ ์—…๋ฐ์ดํŠธ
            update(tree, 2 * node + 1, mid + 1, end, idx, val)
    
        # ๋ถ€๋ชจ ๋…ธ๋“œ๋Š” ์ž์‹ ๋…ธ๋“œ๋“ค์˜ ๊ฐ’์œผ๋กœ ๋‹ค์‹œ ๊ณ„์‚ฐ
        tree[node] = tree[2 * node] + tree[2 * node + 1]

์„ธ๊ทธ๋จผํŠธ ํŠธ๋ฆฌ์˜ ์‘์šฉ ๐ŸŽฏ

์„ธ๊ทธ๋จผํŠธ ํŠธ๋ฆฌ๋Š” ๋‹ค์–‘ํ•œ ๋ฌธ์ œ์— ํ™œ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด:

  • ๊ตฌ๊ฐ„ ํ•ฉ ๋ฌธ์ œ โž•: ๋ฐฐ์—ด์˜ ํŠน์ • ๊ตฌ๊ฐ„์˜ ํ•ฉ์„ ๋น ๋ฅด๊ฒŒ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๊ตฌ๊ฐ„ ์ตœ์†Œ/์ตœ๋Œ€ ๋ฌธ์ œ ๐Ÿ”ฝ๐Ÿ”ผ: ๋ฐฐ์—ด์˜ ํŠน์ • ๊ตฌ๊ฐ„์—์„œ ์ตœ์†Œ๊ฐ’ ๋˜๋Š” ์ตœ๋Œ€๊ฐ’์„ ๋น ๋ฅด๊ฒŒ ์ฐพ์„ ์ˆ˜ ์žˆ์–ด์š”.
  • ๊ตฌ๊ฐ„ ์—…๋ฐ์ดํŠธ ๋ฌธ์ œ ๐Ÿ› ๏ธ: ๋ฐฐ์—ด์˜ ํŠน์ • ๊ตฌ๊ฐ„์˜ ๊ฐ’์„ ์ผ๊ด„์ ์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฌธ์ œ์—๋„ ์‘์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์„ธ๊ทธ๋จผํŠธ ํŠธ๋ฆฌ๋Š” ์ด๋ ‡๊ฒŒ ์‹œ๊ฐ„ ๋ณต์žก๋„ (O(log n))๋กœ ํšจ์œจ์ ์œผ๋กœ ๋™์ž‘ํ•ด, ํฐ ๋ฐ์ดํ„ฐ์…‹์—์„œ๋„ ์„ฑ๋Šฅ์„ ๋ณด์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๐Ÿ’ก

0๊ฐœ์˜ ๋Œ“๊ธ€