-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie.py
185 lines (172 loc) · 4.94 KB
/
trie.py
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
# 改造https://ikatakos.com/pot/programming_algorithm/data_structure/trie
class BinaryTrie:
def __init__(self, bit_depth):
self.root = [None, None, 0] # [0-child, 1-child, count]
self.bit_start = 1 << (bit_depth - 1)
def insert(self, x):
"""xを格納"""
b = self.bit_start
node = self.root
node[2] += 1
# print(self.root)
while b:
i = bool(x & b)
if node[i] is None:
node[i] = [None, None, 1]
else:
node[i][2] += 1
node = node[i]
b >>= 1
def pop_min(self, mask=0):
"""xor_mask適用後の最小値を取得し、木からは削除"""
b = self.bit_start
node = self.root
m = mask
node[2] -= 1
ret2 = 0
while b:
i = bool(m & b)
ret2 = ret2 << 1
if node[i] is None:
i ^= 1
ret2 += 1
if node[i][2] > 1:
node[i][2] -= 1
node = node[i]
else:
tmp = node[i]
node[i] = None
node = tmp
b >>= 1
return ret2
def get_min(self, mask=0):
"""xor_mask適用後の最小値を取得"""
b = self.bit_start
node = self.root
m = mask
ret2 = 0
while b:
i = bool(m & b)
ret2 = ret2 << 1
if node[i] is None:
i ^= 1
ret2 += 1
node = node[i]
b >>= 1
return ret2
def get_kth_min(self, k=1):
"""k番目に小さい値を取得"""
b = self.bit_start
node = self.root
ret2 = 0
while b:
# print(b)
ret2 = ret2 << 1
b >>= 1
if node[0] is None:
node = node[1]
ret2 += 1
continue
if node[1] is None:
node = node[0]
continue
if k <= node[0][2]:
node = node[0]
continue
else:
k -= node[0][2]
node = node[1]
ret2 += 1
continue
return ret2
def erase(self, x):
"""xを削除"""
b = self.bit_start
node = self.root
node[2] -= 1
# print(self.root)
while b:
i = bool(x & b)
if node[i][2] > 1:
node[i][2] -= 1
node = node[i]
else:
tmp = node[i]
node[i] = None
node = tmp
b >>= 1
def lower_bound(self, bound=0):
"""boundより大きい値での最小値を取得。存在しない場合はNoneを返す。"""
ans = self.get_kth_min(self.less_x(bound+1)+1)
if ans > bound:
return ans
def upper_bound(self, bound=0):
"""boundより小さい値での最大値を取得。存在しない場合はNoneを返す。"""
ans = self.get_kth_min(self.less_x(bound))
if ans < bound:
return ans
def merge(self, trie):
"""2つのbinatytrie木を合成"""
def merges(x, y):
if (not x):
return y
if (not y):
return x
return [merges(x[0], y[0]), merges(x[1], y[1]), x[2]+y[2]]
self.root = merges(self.root, trie.root)
def less_x(self, x):
"""xより小さい値の数を出力"""
if x < 0:
return 0
b = self.bit_start
node = self.root
ans = 0
# print(self.root)
while b:
i = bool(x & b)
if node[i] is None:
if i == 1:
ans += node[0][2]
return ans
if i == 1:
if node[0] is not None:
ans += node[0][2]
node = node[i]
b >>= 1
return ans
def less_x_mask(self, x, mask=0):
"""xormask適用後,xより小さい値の数を出力"""
if x < 0:
return 0
b = self.bit_start
node = self.root
ans = 0
m = mask
# print(self.root)
while b:
i = bool(x & b)
mm = bool(m & b)
imm = i ^ mm
if node[imm] is None:
if i == 1:
ans += node[imm ^ 1][2]
return ans
if i == 1:
if node[imm ^ 1] is not None:
ans += node[imm ^ 1][2]
node = node[imm]
b >>= 1
return ans
def is_exist(self, x):
"""xが存在するか判定"""
b = self.bit_start
node = self.root
node[2] -= 1
# print(self.root)
while b:
i = bool(x & b)
if node[i] is None:
return False
node = node[i]
b >>= 1
return True