반응형
https://www.acmicpc.net/problem/2357
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
nums = []
for _ in range(n):
nums.append(int(input()))
s_tree = [[0 for _ in range(2)] for _ in range(4*n)]
def merge(left, right):
min_val = min(left[0], right[0])
max_val = max(left[1], right[1])
return [min_val, max_val]
def build(node, left, right):
if left == right: # leaf node
s_tree[node] = [nums[left], nums[left]]
return s_tree[node]
mid = left + (right - left)//2
a = build(2*node, left, mid)
b = build(2*node+1, mid+1, right)
s_tree[node] = merge(a, b)
return s_tree[node]
def query(start, end, node, left, right):
if end < left or right < start:
return [9876543210, -9876543210]
if start <= left and right <= end:
return s_tree[node]
mid = left + (right - left)//2
a = query(start, end, 2*node, left, mid)
b = query(start, end, 2*node+1, mid+1, right)
return merge(a, b)
build(1, 0, n-1)
for _ in range(m):
start, end = map(int, input().split())
result = query(start-1, end-1, 1, 0, n-1)
print(*result)
세그먼트 트리로 그냥 풀리는 문제!
댓글