Skip to content

Commit be129af

Browse files
beberichetony9402
andauthored
[ADD] baekjoon 18436 java solution (#84)
* [ADD] baekjoon 18436 java solution * solution description update * Refactoring --------- Co-authored-by: Minsang Kim <[email protected]>
1 parent 6e256b5 commit be129af

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

solutions/baekjoon/18436/Main.java

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
// Authored by: beberiche
2+
// Co-authored by: -
3+
// Link: http://boj.kr/a194b1386c8a4767aac779c171d0d2f1
4+
5+
import java.util.*;
6+
import java.io.*;
7+
8+
public class Main {
9+
public static void main(String[] args) {
10+
FastReader rd = new FastReader();
11+
int N = rd.nextInt();
12+
int[] a = new int[N + 1];
13+
for (int i = 1; i <= N; i++) {
14+
a[i] = rd.nextInt();
15+
}
16+
int h = (int) Math.ceil(Math.log(N) / Math.log(2));
17+
int size = 1 << (h + 1);
18+
int[] tree_odd = new int[size];
19+
int[] tree_even = new int[size];
20+
21+
init(1, 1, N, a, tree_odd, tree_even);
22+
23+
int M = rd.nextInt();
24+
25+
StringBuffer sb = new StringBuffer();
26+
for (int i = 0; i < M; i++) {
27+
int cmd = rd.nextInt();
28+
int n1 = rd.nextInt();
29+
int n2 = rd.nextInt();
30+
31+
if (cmd == 1) {
32+
update(1, 1, N, n1, n2, tree_odd, tree_even);
33+
} else if (cmd == 2) {
34+
sb.append(query(1, 1, N, n1, n2, tree_odd, tree_even, true)).append("\n");
35+
} else {
36+
sb.append(query(1, 1, N, n1, n2, tree_odd, tree_even, false)).append("\n");
37+
}
38+
}
39+
System.out.print(sb.toString());
40+
}
41+
42+
43+
private static void init(int curr, int st, int ed, int[] a, int[] tree_odd, int[] tree_even) {
44+
if (st == ed) {
45+
if (a[st] % 2 == 1) {
46+
tree_odd[curr] = 1;
47+
} else {
48+
tree_even[curr] = 1;
49+
}
50+
return;
51+
}
52+
53+
int mid = (st + ed) / 2;
54+
init(curr * 2, st, mid, a, tree_odd, tree_even);
55+
init(curr * 2 + 1, mid + 1, ed, a, tree_odd, tree_even);
56+
tree_odd[curr] = tree_odd[curr * 2] + tree_odd[curr * 2 + 1];
57+
tree_even[curr] = tree_even[curr * 2] + tree_even[curr * 2 + 1];
58+
}
59+
60+
private static void update(int curr, int st, int ed, int idx, int newVal, int[] tree_odd, int[] tree_even) {
61+
if (idx < st || ed < idx) {
62+
return;
63+
}
64+
65+
if (st == ed) {
66+
tree_even[curr] = newVal % 2 == 0 ? 1 : 0;
67+
tree_odd[curr] = newVal % 2 == 1 ? 1 : 0;
68+
return;
69+
}
70+
71+
int mid = (st + ed) / 2;
72+
update(curr * 2, st, mid, idx, newVal, tree_odd, tree_even);
73+
update(curr * 2 + 1, mid + 1, ed, idx, newVal, tree_odd, tree_even);
74+
tree_odd[curr] = tree_odd[curr * 2] + tree_odd[curr * 2 + 1];
75+
tree_even[curr] = tree_even[curr * 2] + tree_even[curr * 2 + 1];
76+
}
77+
78+
private static int query(int curr, int st, int ed, int l, int r, int[] tree_odd, int[] tree_even, boolean check) {
79+
if (r < st || ed < l) {
80+
return 0;
81+
}
82+
83+
84+
if (l <= st && ed <= r) {
85+
if (check) return tree_even[curr];
86+
else return tree_odd[curr];
87+
}
88+
89+
90+
int mid = (st + ed) / 2;
91+
int ret1 = query(curr * 2, st, mid, l, r, tree_odd, tree_even, check);
92+
int ret2 = query(curr * 2 + 1, mid + 1, ed, l, r, tree_odd, tree_even, check);
93+
return ret1 + ret2;
94+
}
95+
96+
static class FastReader {
97+
BufferedReader br;
98+
StringTokenizer st;
99+
100+
public FastReader() {
101+
br = new BufferedReader(new InputStreamReader(System.in));
102+
}
103+
104+
String next() {
105+
while (st == null || !st.hasMoreElements()) {
106+
try {
107+
st = new StringTokenizer(br.readLine());
108+
} catch (IOException e) {
109+
e.printStackTrace();
110+
}
111+
}
112+
return st.nextToken();
113+
}
114+
115+
int nextInt() {
116+
return Integer.parseInt(next());
117+
}
118+
119+
long nextLong() {
120+
return Long.parseLong(next());
121+
}
122+
123+
double nextDouble() {
124+
return Double.parseDouble(next());
125+
}
126+
127+
String nextLine() {
128+
String str = "";
129+
try {
130+
str = br.readLine();
131+
} catch (IOException e) {
132+
e.printStackTrace();
133+
}
134+
return str;
135+
}
136+
}
137+
}
138+
139+
/* Solution Description
140+
141+
1. 세그먼트 트리 기본 문제. `tree[]` 를 2개 만들어,
142+
임의의 범위에 대한 짝수, 홀수인 경우의 갯수를 따로 구한다.
143+
144+
*/

0 commit comments

Comments
 (0)