[Gold I] Title: 최솟값, Time: 672 ms, Memory: 38556 KB -BaekjoonHub

This commit is contained in:
SSUM
2025-03-04 14:30:33 +09:00
parent 6b7404f2b2
commit c50a7f63e1
2 changed files with 74 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
# [Gold I] 최솟값 - 10868
[문제 링크](https://www.acmicpc.net/problem/10868)
### 성능 요약
메모리: 38556 KB, 시간: 672 ms
### 분류
세그먼트 트리, 희소 배열, 자료 구조
### 제출 일자
2025년 3월 4일 14:30:19
### 문제 설명
<p>N(1 ≤ N ≤ 100,000)개의 정수들이 있을 때, a번째 정수부터 b번째 정수까지 중에서 제일 작은 정수를 찾는 것은 어려운 일이 아니다. 하지만 이와 같은 a, b의 쌍이 M(1 ≤ M ≤ 100,000)개 주어졌을 때는 어려운 문제가 된다. 이 문제를 해결해 보자.</p>
<p>여기서 a번째라는 것은 입력되는 순서로 a번째라는 이야기이다. 예를 들어 a=1, b=3이라면 입력된 순서대로 1번, 2번, 3번 정수 중에서 최솟값을 찾아야 한다. 각각의 정수들은 1이상 1,000,000,000이하의 값을 갖는다.</p>
### 입력
<p>첫째 줄에 N, M이 주어진다. 다음 N개의 줄에는 N개의 정수가 주어진다. 다음 M개의 줄에는 a, b의 쌍이 주어진다.</p>
### 출력
<p>M개의 줄에 입력받은 순서대로 각 a, b에 대한 답을 출력한다.</p>

View File

@@ -0,0 +1,44 @@
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
height = 0
length = n
while length != 0:
length //= 2
height += 1
tree_size = pow(2, height+1)
tree_start_index = pow(2, height) - 1
tree = [sys.maxsize] * (tree_size+1)
for i in range(n):
tree[tree_start_index+i+1] = int(input())
temp = n + tree_start_index
while temp != 1:
tree[temp//2] = min(tree[temp//2], tree[temp])
temp-=1
def find_tree(a,b):
result = sys.maxsize
while a<=b:
if (a%2==1):
result = min(result, tree[a])
a+=1
if (b%2==0):
result = min(result, tree[b])
b-=1
a //= 2
b //= 2
return result
for _ in range(m):
s_i, e_i = map(int, input().split())
s_i += tree_start_index
e_i += tree_start_index
print(find_tree(s_i, e_i))