Skip to content

Commit d35f673

Browse files
committed
세그먼트트리 / 인덱스를 출력하는거라 어려웠음
1 parent 7d1d329 commit d35f673

2 files changed

Lines changed: 138 additions & 0 deletions

File tree

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
2+
/* ************************************************************************** */
3+
/* */
4+
/* ::: ::: ::: */
5+
/* Problem Number: 14428 :+: :+: :+: */
6+
/* +:+ +:+ +:+ */
7+
/* By: thxogh1 <boj.kr/u/thxogh1> +#+ +#+ +#+ */
8+
/* +#+ +#+ +#+ */
9+
/* https://boj.kr/14428 #+# #+# #+# */
10+
/* Solved: 2025/05/06 15:35:34 by thxogh1 ### ### ##.kr */
11+
/* */
12+
/* ************************************************************************** */
13+
import java.io.BufferedReader;
14+
import java.io.InputStreamReader;
15+
import java.util.StringTokenizer;
16+
17+
public class Main {
18+
19+
static void init(int[] arr, int[] tree, int node, int s, int e) {
20+
if (s == e) {
21+
tree[node] = s;
22+
} else {
23+
int m = (s + e) / 2;
24+
init(arr, tree, node * 2, s, m);
25+
init(arr, tree, node * 2 + 1, m + 1, e);
26+
if (arr[tree[node * 2]] <= arr[tree[node * 2 + 1]]) {
27+
tree[node] = tree[node * 2];
28+
} else {
29+
tree[node] = tree[node * 2 + 1];
30+
}
31+
}
32+
}
33+
34+
static void update(int[] arr, int[] tree, int node, int s, int e, int idx, int val) {
35+
if (e < idx || idx < s) {
36+
return;
37+
}
38+
if (s == e && idx == s) {
39+
arr[tree[node]] = val;
40+
return;
41+
}
42+
int m = (s + e) / 2;
43+
update(arr, tree, node * 2, s, m, idx, val);
44+
update(arr, tree, node * 2 + 1, m + 1, e, idx, val);
45+
if (arr[tree[node * 2]] <= arr[tree[node * 2 + 1]]) {
46+
tree[node] = tree[node * 2];
47+
} else {
48+
tree[node] = tree[node * 2 + 1];
49+
}
50+
}
51+
52+
static int query(int[] arr, int[] tree, int node, int s, int e, int l, int r) {
53+
if (e < l || r < s) {
54+
return -1;
55+
}
56+
if (l <= s && e <= r) {
57+
return tree[node];
58+
}
59+
int m = (s + e) / 2;
60+
int lmin = query(arr, tree, node * 2, s, m, l, r);
61+
int rmin = query(arr, tree, node * 2 + 1, m + 1, e, l, r);
62+
if (lmin == -1) {
63+
return rmin;
64+
} else if (rmin == -1) {
65+
return lmin;
66+
} else if (arr[lmin] <= arr[rmin]) {
67+
return lmin;
68+
} else {
69+
return rmin;
70+
}
71+
}
72+
73+
public static void main(String[] args) throws Exception {
74+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
75+
StringTokenizer st;
76+
77+
int n = Integer.parseInt(br.readLine());
78+
int[] arr = new int[n];
79+
st = new StringTokenizer(br.readLine());
80+
for (int i = 0; i < n; i++) {
81+
arr[i] = Integer.parseInt(st.nextToken());
82+
}
83+
int m = Integer.parseInt(br.readLine());
84+
int[] tree = new int[4 * n];
85+
init(arr, tree, 1, 0, n - 1);
86+
for (int i = 0; i < m; i++) {
87+
st = new StringTokenizer(br.readLine());
88+
int type = Integer.parseInt(st.nextToken());
89+
if (type == 1) {
90+
int idx = Integer.parseInt(st.nextToken());
91+
int val = Integer.parseInt(st.nextToken());
92+
update(arr, tree, 1, 0, n - 1, idx - 1, val);
93+
} else {
94+
int s = Integer.parseInt(st.nextToken());
95+
int e = Integer.parseInt(st.nextToken());
96+
System.out.println(query(arr, tree, 1, 0, n - 1, s - 1, e - 1) + 1);
97+
}
98+
}
99+
}
100+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# 14428번: 수열과 쿼리 16 - <img src="https://static.solved.ac/tier_small/15.svg" style="height:20px" /> Gold I
2+
3+
<!-- performance -->
4+
5+
<!-- 문제 제출 후 깃허브에 푸시를 했을 때 제출한 코드의 성능이 입력될 공간입니다.-->
6+
7+
<!-- end -->
8+
9+
## 문제
10+
11+
[문제 링크](https://boj.kr/14428)
12+
13+
<p>길이가 N인 수열 A<sub>1</sub>, A<sub>2</sub>, ..., A<sub>N</sub>이 주어진다. 이때, 다음 쿼리를 수행하는 프로그램을 작성하시오.</p>
14+
15+
<ul>
16+
<li><code>1 i v</code> : A<sub>i</sub>를 v로 바꾼다. (1 ≤ i ≤ N, 1 ≤ v ≤ 10<sup>9</sup>)</li>
17+
<li><code>2 i j</code> : A<sub>i</sub>, A<sub>i+1</sub>, ..., A<sub>j</sub>에서 크기가 가장 작은 값의 인덱스를 출력한다. 그러한 값이 여러개인 경우에는 인덱스가 작은 것을 출력한다. (1 ≤ i ≤ j <span style="display: none;"> </span>≤ N, 1 ≤ v ≤ 10<sup>9</sup>)</li>
18+
</ul>
19+
20+
<p>수열의 인덱스는 1부터 시작한다.<span style="display: none;"> </span></p>
21+
22+
## 입력
23+
24+
<p>첫째 줄에 수열의 크기 N이 주어진다. (1 ≤ N ≤ 100,000)</p>
25+
26+
<p>둘째 줄에는 A<sub>1</sub>, A<sub>2</sub>, ..., A<sub>N</sub>이 주어진다. (1 ≤ A<sub>i</sub> ≤ 10<sup>9</sup>)</p>
27+
28+
<p>셋째 줄에는 쿼리의 개수 M이 주어진다. (1 ≤ M ≤ 100,000)</p>
29+
30+
<p>넷째 줄부터 M개의 줄에는 쿼리가 주어진다.</p>
31+
32+
## 출력
33+
34+
<p>2번 쿼리에 대해서 정답을 한 줄에 하나씩 순서대로 출력한다.</p>
35+
36+
## 소스코드
37+
38+
[소스코드 보기](Main.java)

0 commit comments

Comments
 (0)