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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#[derive(Debug, Default, Clone, PartialEq, Eq)]
struct Node {
children: Vec<Option<Node>>,
count: u32,
}
impl Node {
fn new() -> Self {
Self {
children: vec![None; 2],
count: 0,
}
}
}
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct BinaryTrie {
nodes: Option<Node>,
}
impl BinaryTrie {
pub const fn new() -> Self {
Self { nodes: None }
}
#[inline]
pub fn insert(&mut self, x: u32) -> Option<()> {
if self.nodes.is_none() {
self.nodes = Some(Node::new());
}
let mut node = self.nodes.as_mut()?;
for i in (0..32).rev() {
node.count += 1;
let bit = (x >> i & 1) as usize;
if unsafe { node.children.get_unchecked(bit) }.is_none() {
*unsafe { node.children.get_unchecked_mut(bit) } = Some(Node::new());
}
node = unsafe { node.children.get_unchecked_mut(bit) }.as_mut()?;
}
node.count += 1;
Some(())
}
#[inline]
pub fn count(&self, x: u32) -> Option<u32> {
let mut node = &self.nodes;
for i in (0..32).rev() {
node = unsafe { node.as_ref()?.children.get_unchecked((x >> i & 1) as usize) };
}
Some(node.as_ref()?.count)
}
#[inline]
pub fn erase(&mut self, x: u32) -> Option<()> {
self.count(x)?;
self.erase_inner(x, 1)
}
#[inline]
pub fn erase_all(&mut self, x: u32) -> Option<()> {
let erase_count = self.count(x)?;
self.erase_inner(x, erase_count)
}
fn erase_inner(&mut self, x: u32, erase_count: u32) -> Option<()> {
let mut node = &mut self.nodes;
for i in (0..32).rev() {
if node.as_ref()?.count == erase_count {
*node = None;
return Some(());
} else {
node.as_mut()?.count -= erase_count;
}
node = unsafe {
node.as_mut()?
.children
.get_unchecked_mut((x >> i & 1) as usize)
};
}
if node.as_ref()?.count == erase_count {
*node = None;
} else {
node.as_mut()?.count -= erase_count;
}
Some(())
}
#[inline]
pub fn xor_min(&self, x: u32) -> Option<u32> {
let mut ans = 0;
let mut node = self.nodes.as_ref()?;
for i in (0..32).rev() {
let bit = {
let mut buff = (x >> i & 1) as usize;
if unsafe { node.children.get_unchecked(buff) }.is_none() {
buff ^= 1;
}
buff
};
ans ^= (bit as u32) << i;
node = unsafe { node.children.get_unchecked(bit) }.as_ref()?;
}
Some(ans ^ x)
}
#[inline]
pub fn min(&self) -> Option<u32> {
let mut ans = 0;
let mut node = self.nodes.as_ref()?;
for i in (0..32).rev() {
let bit = if node.children[0].is_none() { 1 } else { 0 };
ans ^= (bit as u32) << i;
node = unsafe { node.children.get_unchecked(bit) }.as_ref()?;
}
Some(ans)
}
#[inline]
pub fn max(&self) -> Option<u32> {
let mut ans = 0;
let mut node = self.nodes.as_ref()?;
for i in (0..32).rev() {
let bit = if node.children[1].is_none() { 0 } else { 1 };
ans ^= (bit as u32) << i;
node = unsafe { node.children.get_unchecked(bit) }.as_ref()?;
}
Some(ans)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn bt() {
let mut b = BinaryTrie::new();
b.insert(6);
let a = b.clone();
b.insert(7);
b.insert(7);
b.erase(7);
b.erase(7);
b.erase(10);
assert_eq!(a.nodes, b.nodes);
}
#[test]
fn btt() {
let mut b = BinaryTrie::new();
let n = 2u32.pow(30);
for i in 0..100 {
b.insert(n + i);
}
for i in 0..99 {
b.erase(n + i);
assert_eq!(b.min().unwrap(), n + i + 1);
}
}
#[test]
fn library_checker() {
let mut b = BinaryTrie::new();
let query = vec![(0, 6), (0, 7), (2, 5), (1, 7), (1, 10), (2, 7)];
let mut ans = vec![];
query.iter().for_each(|&(p, x)| match p {
0 => {
b.insert(x);
}
1 => {
b.erase_all(x);
}
_ => ans.push(b.xor_min(x).unwrap_or_else(|| panic!("{}", x.to_string()))),
});
assert_eq!(vec![2, 1], ans);
}
}