[백준] 10868번 최솟값

HL·2021년 7월 10일
0

백준

목록 보기
100/104
post-custom-banner

문제링크

https://www.acmicpc.net/problem/10868

코드

import sys, math

tree = []
a = []


def solution():
    global a, tree

    read = sys.stdin.readline
    write = sys.stdout.write
    n, m = map(int, read().split())
    a = [int(read()) for _ in range(n)]
    commands = [list(map(int, read().split())) for _ in range(m)]

    h = math.ceil(math.log(n, 2))
    tree_size = 2 ** (h+1)
    tree = [0] * tree_size
    init(0, n-1, 1)

    for b, c in commands:
        write(f'{get_min(b-1, c-1, 0, n-1, 1)}\n')


def init(start, end, node):

    # 리프노드일때
    if start == end:
        tree[node] = a[start]
        return tree[node]

    # 아닐때
    tree[node] = min(init(start, (start+end)//2, node*2), init((start+end)//2+1, end, node*2+1)) #% 1000000007
    return tree[node]


def get_min(left, right, start, end, node):

    # 포함하지 않을 때
    if left > end or right < start:
        return float('inf')

    # 포함할 때
    if left <= start and end <= right:
        return tree[node]

    return min(get_min(left, right, start, (start+end)//2, node*2), get_min(left, right, (start+end)//2+1, end, node*2+1))


solution()
profile
Frontend 개발자입니다.
post-custom-banner

0개의 댓글