Skip to content

Commit ff7045a

Browse files
authored
[20250822] BOJ / P3 / 화려한 마을 / 이종환
1 parent 8d8050f commit ff7045a

File tree

1 file changed

+166
-0
lines changed

1 file changed

+166
-0
lines changed
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
```java
2+
import java.io.BufferedReader;
3+
import java.io.IOException;
4+
import java.io.InputStreamReader;
5+
import java.util.Arrays;
6+
import java.util.HashMap;
7+
import java.util.HashSet;
8+
import java.util.StringTokenizer;
9+
10+
11+
public class Main {
12+
13+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
14+
static StringBuilder sb = new StringBuilder();
15+
static StringTokenizer st;
16+
17+
static int houseCnt, nodeCnt, colorCnt, taskCnt;
18+
19+
// 각각의 노드가 지금 구간에서 어떤 색을 몇 개 가지고 있는 지 인지해야함.
20+
// 100,000 * 4 * 30 = 12,000,000 개의 Integer -> 이게 맞나?
21+
22+
static class SegmentTree{
23+
24+
int[] lazy;
25+
int check;
26+
int[] nodes; // 어느 색상을 가지는지 비트마스킹으로 표현. 이래서 색상이 최대가 30이었구나
27+
28+
public SegmentTree() {
29+
nodeCnt = houseCnt*4;
30+
nodes = new int[nodeCnt];
31+
lazy = new int[nodeCnt];
32+
Arrays.fill(nodes, (1 << 1));
33+
}
34+
35+
36+
37+
private void updateLazy(int nodeIdx, int from, int to) {
38+
if (lazy[nodeIdx] ==0) return;
39+
40+
nodes[nodeIdx] = ( 1 << lazy[nodeIdx]);
41+
42+
if ( from != to) {
43+
lazy[nodeIdx*2] = lazy[nodeIdx];
44+
lazy[nodeIdx*2+1] = lazy[nodeIdx];
45+
}
46+
lazy[nodeIdx] = 0;
47+
48+
return;
49+
}
50+
51+
public void updateRnage(int from, int to , int target) {
52+
updateRange(1,1,houseCnt, from,to,target);
53+
}
54+
55+
private void updateRange (int idx, int start, int end, int from ,int to, int target) {
56+
updateLazy(idx, start, end);
57+
58+
if ( start > to || end < from) return; // 아예 안걸림
59+
60+
61+
// 완전히 걸림
62+
if ( start >= from && end <= to) {
63+
64+
nodes[idx] = (1 << target);
65+
66+
if (start!=end) {
67+
lazy[idx*2] = target;
68+
lazy[idx*2+1] = target;
69+
}
70+
return;
71+
}
72+
73+
//걸치는 경우
74+
75+
int mid = (start+end)/2;
76+
77+
// 하위 노드를 업데이트 후
78+
updateRange(idx*2,start,mid,from,to,target);
79+
updateRange(idx*2+1,mid+1,end,from,to,target);
80+
81+
82+
nodes[idx] =( nodes[idx*2] | nodes[idx*2+1]);
83+
}
84+
85+
public int query (int from, int to) {
86+
check = 0 ;
87+
query(1, 1, houseCnt, from,to);
88+
89+
int ans = 0;
90+
91+
for (int i = 0; i < 30; i++) {
92+
if ( (check & (1 << i)) != 0) ans++;
93+
}
94+
return ans;
95+
}
96+
97+
private void query(int idx, int start, int end, int from ,int to) {
98+
updateLazy(idx, start, end);
99+
100+
if (start > to || end < from) return;
101+
102+
103+
if ( start >= from && end <= to) {
104+
check |= nodes[idx];
105+
return;
106+
}
107+
108+
// 적당히 겹치는 경우
109+
int mid = (start+end)/2;
110+
query(idx*2, start,mid,from,to);
111+
query(idx*2+1, mid+1,end,from,to);
112+
}
113+
114+
}
115+
116+
117+
118+
119+
public static void main(String[] args) throws IOException {
120+
init();
121+
process();
122+
print();
123+
}
124+
125+
public static void init() throws IOException {
126+
st = new StringTokenizer(br.readLine());
127+
128+
houseCnt = Integer.parseInt(st.nextToken());
129+
colorCnt = Integer.parseInt(st.nextToken());
130+
taskCnt = Integer.parseInt(st.nextToken());
131+
132+
}
133+
134+
public static void process() throws IOException {
135+
SegmentTree tree = new SegmentTree();
136+
137+
for (int i = 0; i < taskCnt; i++) {
138+
StringTokenizer st = new StringTokenizer(br.readLine());
139+
String order = st.nextToken();
140+
int from = Integer.parseInt(st.nextToken());
141+
int to = Integer.parseInt(st.nextToken());
142+
143+
if (from > to) {
144+
int temp = from;
145+
from = to;
146+
to = temp;
147+
}
148+
149+
if (order.equals("C")){
150+
int target = Integer.parseInt(st.nextToken());
151+
tree.updateRnage(from, to, target);
152+
} else {
153+
sb.append(tree.query(from, to)).append("\n");
154+
}
155+
}
156+
157+
}
158+
159+
160+
161+
public static void print() {
162+
System.out.print(sb.toString());
163+
}
164+
}
165+
166+
```

0 commit comments

Comments
 (0)