Skip to content

Commit 360790e

Browse files
authored
[20250608] BOJ / P2 / mex / 권혁준
1 parent d90961f commit 360790e

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed

khj20006/202506/08 BOJ P2 mex.md

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
class IOController {
6+
BufferedReader br;
7+
BufferedWriter bw;
8+
StringTokenizer st;
9+
10+
public IOController(){
11+
br = new BufferedReader(new InputStreamReader(System.in));
12+
bw = new BufferedWriter(new OutputStreamWriter(System.out));
13+
st = new StringTokenizer("");
14+
}
15+
16+
void nextLine() throws Exception {
17+
st = new StringTokenizer(br.readLine());
18+
}
19+
20+
String nextToken() throws Exception {
21+
while(!st.hasMoreTokens()) nextLine();
22+
return st.nextToken();
23+
}
24+
25+
int nextInt() throws Exception { return Integer.parseInt(nextToken()); }
26+
long nextLong() throws Exception { return Long.parseLong(nextToken()); }
27+
double nextDouble() throws Exception { return Double.parseDouble(nextToken()); }
28+
29+
void close() throws Exception {
30+
bw.flush();
31+
bw.close();
32+
}
33+
34+
void write(String content) throws Exception {
35+
bw.write(content);
36+
}
37+
38+
}
39+
40+
class Node {
41+
Node left, right;
42+
Node() {
43+
left = null;
44+
right = null;
45+
}
46+
}
47+
48+
class Trie {
49+
Node root;
50+
Trie(){
51+
root = new Node();
52+
}
53+
54+
// 정수 x 삽입
55+
void insert(int x) {
56+
int k = 1<<18;
57+
Node now = root;
58+
while(k > 0) {
59+
int bit = x & k;
60+
if(bit == 0) {
61+
if(now.left == null) now.left = new Node();
62+
now = now.left;
63+
}
64+
else {
65+
if(now.right == null) now.right = new Node();
66+
now = now.right;
67+
}
68+
k >>= 1;
69+
}
70+
}
71+
72+
// x와 XOR 했을 때의 최솟값 찾기
73+
int find(int x) {
74+
int k = 1<<18, res = 0;
75+
Node now = root;
76+
while(k > 0) {
77+
int bit = x & k;
78+
if(bit == 0) {
79+
if(now.left == null){
80+
res |= k;
81+
now = now.right;
82+
}
83+
else{
84+
now = now.left;
85+
}
86+
}
87+
else {
88+
if(now.right == null) {
89+
res |= k;
90+
now = now.left;
91+
}
92+
else{
93+
now = now.right;
94+
}
95+
}
96+
k >>= 1;
97+
}
98+
return res;
99+
}
100+
}
101+
102+
public class Main {
103+
104+
static IOController io;
105+
106+
//
107+
108+
static int N, M;
109+
static boolean[] exist;
110+
static Trie trie;
111+
112+
public static void main(String[] args) throws Exception {
113+
114+
io = new IOController();
115+
116+
init();
117+
solve();
118+
119+
io.close();
120+
121+
}
122+
123+
public static void init() throws Exception {
124+
125+
N = io.nextInt();
126+
M = io.nextInt();
127+
exist = new boolean[524288];
128+
for(int i=1;i<=N;i++) exist[io.nextInt()] = true;
129+
130+
trie = new Trie();
131+
for(int i=0;i<524288;i++) if(!exist[i]) trie.insert(i);
132+
133+
}
134+
135+
static void solve() throws Exception {
136+
137+
int X = 0;
138+
for(int i=0;i<M;i++) {
139+
X ^= io.nextInt();
140+
io.write(trie.find(X) + "\n");
141+
}
142+
143+
}
144+
145+
146+
}
147+
```

0 commit comments

Comments
 (0)