[백준] 2243번 - 사탕상자 [Java][C++]
[백준] 2243번 - 사탕상자 [Java][C++]
1. 문제 풀이
사탕은 $1$ 부터 $1,000,000$ 까지 맛의 점수가 있고 이런 사탕을 사탕상자에 담는 쿼리와 사탕상자에서 꺼내는 쿼리가 주어졌을 때 쿼리들을 해결하는 문제다. $1$ 부터 $1,000,000$ 까지의 수들에 대한 등장 횟수를 세는 세그먼트 트리를 활용하면 해결할 수 있는데 수의 등장 횟수를 저장하는 세그먼트 트리에서 이분 탐색을 활용해서 $k$ 번째로 등장한 수를 찾으면 해결할 수 있다. 등장 횟수를 저장하는 것은 횟수에 대한 구간 합이므로 펜윅 트리를 활용해도 해결할 수 있다.
$k$ 번째 수를 구하는 전형적인 세그먼트 트리 유형의 문제다.
2. 코드
1. 세그먼트 트리 [Java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.io.*;
import java.util.*;
public class Main {
static final int MAX = 1_000_000;
static class SegmentTree {
int n;
int[] tree;
public SegmentTree(int n) {
this.n = n;
this.tree = new int[4 * n];
}
void update(int idx, int delta) {
update(1, 1, n, idx, delta);
}
void update(int node, int start, int end, int idx, int delta) {
if (start == end) {
tree[node] += delta;
return;
}
int mid = (start + end) / 2;
if (idx <= mid) {
update(node * 2, start, mid, idx, delta);
} else {
update(node * 2 + 1, mid + 1, end, idx, delta);
}
tree[node] = tree[node * 2] + tree[node * 2 + 1];
}
int queryKth(int k) {
return queryKth(1, 1, n, k);
}
int queryKth(int node, int start, int end, int k) {
if (start == end) return start;
int mid = (start + end) / 2;
if (k <= tree[node * 2]) {
return queryKth(node * 2, start, mid, k);
} else {
return queryKth(node * 2 + 1, mid + 1, end, k - tree[node * 2]);
}
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
StringTokenizer st;
SegmentTree tree = new SegmentTree(MAX);
int N = Integer.parseInt(br.readLine());
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
int A = Integer.parseInt(st.nextToken());
int B = Integer.parseInt(st.nextToken());
if (A == 1) {
int k = tree.queryKth(B);
sb.append(k).append("\n");
tree.update(k, -1);
} else {
int C = Integer.parseInt(st.nextToken());
tree.update(B, C);
}
}
System.out.println(sb);
}
}
2. 펜윅 트리 [Java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.io.*;
import java.util.*;
public class Main {
static final int MAX = 1_000_000;
static class FenwickTree {
int n;
int[] tree;
public FenwickTree(int n) {
this.n = n;
tree = new int[1 + n];
}
void update(int idx, int delta) {
while (idx <= n) {
tree[idx] += delta;
idx += idx & -idx;
}
}
int kth(int k) {
int pos = 0;
int bit = 1 << 20;
while (bit > 0) {
int next = pos + bit;
if (next <= n && tree[next] < k) {
k -= tree[next];
pos = next;
}
bit >>= 1;
}
return pos + 1;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
StringTokenizer st;
FenwickTree tree = new FenwickTree(MAX);
int N = Integer.parseInt(br.readLine());
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
int A = Integer.parseInt(st.nextToken());
int B = Integer.parseInt(st.nextToken());
if (A == 1) {
int k = tree.kth(B);
sb.append(k).append("\n");
tree.update(k, -1);
} else {
int C = Integer.parseInt(st.nextToken());
tree.update(B, C);
}
}
System.out.println(sb);
}
}
3. 세그먼트 트리 [C++]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <bits/stdc++.h>
using namespace std;
constexpr int MAX = 1000000;
struct SegTree {
int n;
vector<int> tree;
SegTree(int n) : n(n), tree(4 * n) {
}
void update(int node, int start, int end, int idx, int delta) {
if (start == end) {
tree[node] += delta;
return;
}
int mid = (start + end) / 2;
if (idx <= mid) {
update(node * 2, start, mid, idx, delta);
} else {
update(node * 2 + 1, mid + 1, end, idx, delta);
}
tree[node] = tree[node * 2] + tree[node * 2 + 1];
}
int queryKth(int node, int start, int end, int k) {
if (start == end) return start;
int mid = (start + end) / 2;
if (k <= tree[node * 2]) {
return queryKth(node * 2, start, mid, k);
} else {
return queryKth(node * 2 + 1, mid + 1, end, k - tree[node * 2]);
}
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
SegTree tree(MAX);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int a, b;
cin >> a >> b;
if (a == 1) {
int k = tree.queryKth(1, 1, MAX, b);
tree.update(1, 1, MAX, k, -1);
cout << k << '\n';
} else {
int c;
cin >> c;
tree.update(1, 1, MAX, b, c);
}
}
}
4. 펜윅 트리 [C++]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <bits/stdc++.h>
using namespace std;
constexpr int MAX = 1000000;
struct Fenwick {
int n;
vector<int> tree;
Fenwick(int n) : n(n), tree(1 + n) {
}
void add(int idx, int delta) {
while (idx <= n) {
tree[idx] += delta;
idx += idx & -idx;
}
}
int kth(int k) {
int pos = 0;
int bit = 1 << 20;
while (bit > 0) {
int next = pos + bit;
if (next <= n && tree[next] < k) {
k -= tree[next];
pos = next;
}
bit >>= 1;
}
return pos + 1;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
Fenwick tree(MAX);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int a, b;
cin >> a >> b;
if (a == 1) {
int k = tree.kth(b);
tree.add(k, -1);
cout << k << '\n';
} else {
int c;
cin >> c;
tree.add(b, c);
}
}
}
3. 풀이 정보
1. 세그먼트 트리 [Java]
| 언어 | 시간 | 메모리 | 코드 길이 |
|---|---|---|---|
| Java 11 | 488 ms | 57008 KB | 2160 B |
2. 펜윅 트리 [Java]
| 언어 | 시간 | 메모리 | 코드 길이 |
|---|---|---|---|
| Java 11 | 448 ms | 45120 KB | 1659 B |
3. 세그먼트 트리 [C++]
| 언어 | 시간 | 메모리 | 코드 길이 |
|---|---|---|---|
| C++ 17 | 56 ms | 17652 KB | 1402 B |
4. 펜윅 트리 [C++]
| 언어 | 시간 | 메모리 | 코드 길이 |
|---|---|---|---|
| C++ 17 | 44 ms | 5928 KB | 1052 B |
This post is licensed under CC BY 4.0 by the author.