https://www.acmicpc.net/problem/10868
import sys
n, m = [int(x) for x in sys.stdin.readline().split()]
num = []
for _ in range(n):
num.append(int(sys.stdin.readline()))
seg_tree = [0 for _ in range(4*n)]
def build_tree(x, left, right):
if left == right:
seg_tree[x] = num[left]
return seg_tree[x]
mid = (left + right) // 2
left_v = build_tree(x * 2, left, mid)
right_v = build_tree(x * 2 + 1, mid + 1, right)
seg_tree[x] = min(left_v, right_v)
return seg_tree[x]
build_tree(1, 0, n-1)
def query(x, start, end, left, right):
if start > right or end < left:
return 1000000001
if left <= start and right >= end:
return seg_tree[x]
mid = (start + end) // 2
mid = (start + end) // 2
return min(query(x*2, start, mid, left, right), query(x*2+1, mid+1, end, left, right))
# print(seg_tree)
answer = []
for _ in range(m):
a, b = [int(x) for x in sys.stdin.readline().split()]
s = query(1, 0, n-1, a-1, b-1)
# answer.append(s)
print(s)
구간 합 구하기(https://junheelab.tistory.com/140)와 달리 이번 문제는 각 트리의 노드를 자식의 노드 중 최소값으로 설정하는 문제이다.