Skip to content

Commit 6049ae0

Browse files
committed
[IndexedTree] baekjoon-5676
1 parent 1bc4f37 commit 6049ae0

File tree

4 files changed

+153
-3
lines changed

4 files changed

+153
-3
lines changed

β€ŽREADME.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,14 @@
8585
| 02 | | [Baekjoon-1927 μ΅œμ†Œ νž™](./src/Heap/P1927) | |
8686
| 03 | | [Baekjoon-11279 μ΅œλŒ€ νž™](./src/Heap/P11279) | |
8787

88-
### Indexed Tree
88+
### Indexed Tree (Segment Tree)
8989

9090
| # | β˜† | Problem | Note |
9191
| :-: | :-: | :------------------------------------------------------------- | :--- |
9292
| 01 | | [Indexed Tree Example](./src/IndexedTree/IndexedTreeTest.java) | |
93-
| 02 | ⭐️ | [Baekjoon-2042 ꡬ간 ν•© κ΅¬ν•˜κΈ°](./src/IndexedTree/P2042) | |
94-
| 03 | ⭐️ | [Baekjoon-2243 μ‚¬νƒ•μƒμž](./src/IndexedTree/P2243) | |
93+
| 02 | ⭐️ | [Baekjoon-2042 ꡬ간 ν•© κ΅¬ν•˜κΈ°](./src/IndexedTree/P2042) | |
94+
| 03 | ⭐️ | [Baekjoon-2243 μ‚¬νƒ•μƒμž](./src/IndexedTree/P2243) | |
95+
| 04 | | [Baekjoon-5676 음주 μ½”λ”©](./src/IndexedTree/P5676) | |
9596

9697
### Trie
9798

β€Žsrc/IndexedTree/P5676/Main.java

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package IndexedTree.P5676;
2+
3+
import java.io.*;
4+
import java.util.*;
5+
6+
public class Main {
7+
8+
static int N, K;
9+
static int[] nums;
10+
final static char CH_CMD = 'C', MUL_CMD = 'P';
11+
12+
public static void main(String[] args) throws Exception {
13+
System.setIn(new FileInputStream("src/IndexedTree/P5676/input.txt"));
14+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
15+
16+
String line;
17+
while ((line = br.readLine()) != null) {
18+
StringTokenizer st = new StringTokenizer(line);
19+
N = stoi(st.nextToken());
20+
K = stoi(st.nextToken());
21+
22+
st = new StringTokenizer(br.readLine());
23+
nums = new int[N];
24+
for (int i = 0; i < N; i++) nums[i] = stoi(st.nextToken());
25+
26+
SegmentTree tree = new SegmentTree(N, nums);
27+
28+
StringBuilder sb = new StringBuilder();
29+
while (K-- > 0) {
30+
st = new StringTokenizer(br.readLine());
31+
char command = st.nextToken().charAt(0);
32+
int from = stoi(st.nextToken()), to = stoi(st.nextToken());
33+
34+
if (command == CH_CMD) tree.changeValue(0, 0, N-1, from-1, to);
35+
else if (command == MUL_CMD) {
36+
int result = tree.multiple(0, 0, N-1, from-1, to-1);
37+
if (result == 0) sb.append(0);
38+
else sb.append(result > 0 ? '+' : '-');
39+
}
40+
}
41+
System.out.println(sb.toString());
42+
}
43+
44+
br.close();
45+
}
46+
47+
static int stoi(String s) { return Integer.parseInt(s); }
48+
}
49+
50+
class SegmentTree {
51+
int[] tree;
52+
int[] arr;
53+
54+
public SegmentTree(int N, int[] arr) {
55+
this.tree = new int[(int) Math.pow(2, (Math.ceil(Math.log(N) / Math.log(2)) + 1))];
56+
this.arr = arr;
57+
makeTree(0, 0, N-1);
58+
}
59+
60+
private int setValue(int value) {
61+
return Integer.compare(value, 0);
62+
}
63+
64+
void makeTree(int idx, int start, int end) {
65+
if (start == end) {
66+
tree[idx] = setValue(arr[start]);
67+
return;
68+
}
69+
makeTree(idx*2+1, start, (start+end)/2);
70+
makeTree(idx*2+2, (start+end)/2+1, end);
71+
tree[idx] = tree[idx*2+1] * tree[idx*2+2];
72+
}
73+
74+
void changeValue(int idx, int start, int end, int target, int value) {
75+
if (target < start || target > end) return;
76+
if (start == end) {
77+
tree[idx] = setValue(value);
78+
return;
79+
}
80+
changeValue(idx*2+1, start, (start+end)/2, target, value);
81+
changeValue(idx*2+2, (start+end)/2+1, end, target, value);
82+
tree[idx] = tree[idx*2+1] * tree[idx*2+2];
83+
}
84+
85+
int multiple(int idx, int start, int end, int left, int right) {
86+
if (end < left || start > right) return 1;
87+
if (left <= start && end <= right) return tree[idx];
88+
return multiple(idx*2+1, start, (start+end)/2, left, right)
89+
* multiple(idx*2+2, (start+end)/2+1, end, left, right);
90+
}
91+
}

β€Žsrc/IndexedTree/P5676/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
## [baekjoon-5676] 음주 μ½”λ”©
2+
3+
![image](https://user-images.githubusercontent.com/22045163/104617025-4ffa7d80-56ce-11eb-8ce1-4c44bd1354bf.png)
4+
![image](https://user-images.githubusercontent.com/22045163/104617067-5ab51280-56ce-11eb-93bb-62c7c117acc0.png)
5+
6+
### μ˜€λ²„ν”Œλ‘œμš° 이슈
7+
8+
μ²˜μŒμ—λŠ” Segment Tree에 λͺ¨λ“  값듀을 μ €μž₯ν•˜κ³  κ³±ν•΄μ„œ ν’€μ—ˆλ‹€.
9+
κ·Έλž¬μ„ λ•Œ `ν‹€λ ΈμŠ΅λ‹ˆλ‹€!`λ₯Ό λ³Ό 수 μžˆμ—ˆκ³ ,
10+
문제의 λ²”μœ„λ₯Ό λ³΄λ‹ˆ μ΅œλŒ€μ˜ μ ˆλŒ“κ°’ 100, μˆ˜μ—΄μ˜ ν¬κΈ°λŠ” 10^5둜
11+
μ΅œλŒ“κ°’μœΌλ‘œλŠ” 100을 10^5번 κ³±ν•  수 μžˆλ‹€λŠ” 것인데, 거의 상상 μ΄ˆμ›”λ‘œ 큰 μˆ˜κ°€ λ‚˜μ˜¬ 수 μžˆλ‹€.
12+
λ‹€μŒμ˜ μ˜ˆμ‹œλ§Œ μ‚΄νŽ΄λ΄λ„ κ·Έλ ‡λ‹€.
13+
14+
```
15+
// Input
16+
10 1
17+
100 100 100 100 100 100 100 100 100 10
18+
P 1 10
19+
// Output (Debugging)
20+
[-1981284352, 1410065408, 1000000000, 1000000, 10000, 1000000, 1000, 10000, 100, 100, 100, 10000, 100, 100, 10, 100, 100, 0, 0, 0, 0, 0, 0, 100, 100, 0, 0, 0, 0, 0, 0, 0]
21+
-1981284352
22+
-
23+
```
24+
25+
λ°°μ—΄μ—λŠ” μ–‘μˆ˜λ°–μ— λ‹΄μ§€ μ•Šμ•˜λŠ”λ°, 음수의 κ²°κ³Όκ°€ λ‚˜μ˜€λŠ” 것을 λ³Ό 수 μžˆλ‹€.
26+
27+
λ”°λΌμ„œ 이 λ¬Έμ œλŠ” **값이 μ•„λ‹Œ λΆ€ν˜Έ**둜 ν’€μ–΄μ•Ό ν•œλ‹€. 좜λ ₯κ°’μœΌλ‘œ λΆ€ν˜Έλ§Œμ„ μš”κ΅¬ν•˜λŠ” μ΄μœ κ°€ λ‹€ μžˆλ‹€. γ…Žγ…Ž
28+
λ‚˜λŠ” 1, -1, 0 으둜 값을 μ €μž₯ν•˜μ—¬ 문제λ₯Ό ν’€μ—ˆλ‹€.
29+
30+
![image](https://user-images.githubusercontent.com/22045163/104617857-4faeb200-56cf-11eb-8292-46db1e42d653.png)
31+
32+
μž¬λ°Œλ‹€. λ„ˆλ¬΄ μž¬λ°ŒλŠ” λ¬Έμ œλ‹€.

β€Žsrc/IndexedTree/P5676/input.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
3 2
2+
0 0 0
3+
C 3 1
4+
P 3 3
5+
10 1
6+
100 100 100 100 100 100 100 100 100 10
7+
P 1 10
8+
4 6
9+
-2 6 0 -1
10+
C 1 10
11+
P 1 4
12+
C 3 7
13+
P 2 2
14+
C 4 -5
15+
P 1 4
16+
5 9
17+
1 5 -2 4 3
18+
P 1 2
19+
P 1 5
20+
C 4 -5
21+
P 1 5
22+
P 4 5
23+
C 3 0
24+
P 1 5
25+
C 4 -5
26+
C 4 -52

0 commit comments

Comments
Β (0)