From f0c9133599fe17a5146b488f2f1b8e52edcc6f90 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Wed, 6 Aug 2025 18:38:52 -0600 Subject: [PATCH 1/4] nits to binary trie --- library/data_structures/binary_trie.hpp | 40 +++++++++++++++++++ .../binary_trie/binary_trie.hpp | 30 -------------- library/data_structures/binary_trie/count.hpp | 10 ----- library/data_structures/binary_trie/walk.hpp | 12 ------ .../data_structures/binary_trie.test.cpp | 28 ++++++------- 5 files changed, 53 insertions(+), 67 deletions(-) create mode 100644 library/data_structures/binary_trie.hpp delete mode 100644 library/data_structures/binary_trie/binary_trie.hpp delete mode 100644 library/data_structures/binary_trie/count.hpp delete mode 100644 library/data_structures/binary_trie/walk.hpp diff --git a/library/data_structures/binary_trie.hpp b/library/data_structures/binary_trie.hpp new file mode 100644 index 00000000..89dc8377 --- /dev/null +++ b/library/data_structures/binary_trie.hpp @@ -0,0 +1,40 @@ +#pragma once +//! @code +//! binary_trie bt; +//! bt.update(num, 1); // insert +//! bt.update(num, -1); // erase +//! @endco +//! @time O(q * mx_bit) +//! @space O(q * mx_bit) +const ll mx_bit = 1LL << 60; +struct binary_trie { + struct node { + int siz = 0; + array next = {-1, -1}; + }; + deque t; + binary_trie(): t(1) {} + void update(ll num, int delta) { + int v = 0; + for (ll bit = mx_bit; bit; bit /= 2) { + bool b = num & bit; + if (t[v].next[b] == -1) { + t[v].next[b] = sz(t); + t.emplace_back(); + } + v = t[v].next[b]; + t[v].siz += delta; + } + } + ll walk(ll num) { + int v = 0; + ll res = 0; + for (ll bit = mx_bit; bit; bit /= 2) { + bool b = num & bit; + int u = t[v].next[b]; + if (u != -1 && t[u].siz > 0) v = u, res |= num & bit; + else v = t[v].next[!b], res |= (~num) & bit; + } + return res; + } +}; diff --git a/library/data_structures/binary_trie/binary_trie.hpp b/library/data_structures/binary_trie/binary_trie.hpp deleted file mode 100644 index 4fe16c13..00000000 --- a/library/data_structures/binary_trie/binary_trie.hpp +++ /dev/null @@ -1,30 +0,0 @@ -#pragma once -//! @code -//! binary_trie bt1; //mx_bit = 30 -//! binary_trie bt2; //mx_bit = 62 -//! @endcode -//! @time O(mx_bit) -//! @space O(1) -template struct binary_trie { - int mx_bit; - struct node { - int sub_sz = 0; - array next = {-1, -1}; - }; - vector t; - binary_trie(): mx_bit(8 * sizeof(T) - 2), t(1) {} - void update(T num, int delta) { - t[0].sub_sz += delta; - for (int v = 0, bit = mx_bit; bit >= 0; bit--) { - bool b = (num >> bit) & 1; - if (t[v].next[b] == -1) { - t[v].next[b] = sz(t); - t.emplace_back(); - } - v = t[v].next[b]; - t[v].sub_sz += delta; - } - } -#include "count.hpp" -#include "walk.hpp" -}; diff --git a/library/data_structures/binary_trie/count.hpp b/library/data_structures/binary_trie/count.hpp deleted file mode 100644 index e6355d15..00000000 --- a/library/data_structures/binary_trie/count.hpp +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once -int count(T num) { - int v = 0; - for (int bit = mx_bit; bit >= 0; bit--) { - bool b = (num >> bit) & 1; - if (t[v].next[b] == -1) return 0; - v = t[v].next[b]; - } - return t[v].sub_sz; -} diff --git a/library/data_structures/binary_trie/walk.hpp b/library/data_structures/binary_trie/walk.hpp deleted file mode 100644 index 129af5e9..00000000 --- a/library/data_structures/binary_trie/walk.hpp +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once -T walk(T num) { - T res = 0; - for (int v = 0, bit = mx_bit; bit >= 0; bit--) { - bool b = (num >> bit) & 1; - int u = t[v].next[b]; - if (u != -1 && t[u].sub_sz > 0) - v = u, res |= T(b) << bit; - else v = t[v].next[!b], res |= T(!b) << bit; - } - return res; -} diff --git a/tests/library_checker_aizu_tests/data_structures/binary_trie.test.cpp b/tests/library_checker_aizu_tests/data_structures/binary_trie.test.cpp index db920a62..de34b555 100644 --- a/tests/library_checker_aizu_tests/data_structures/binary_trie.test.cpp +++ b/tests/library_checker_aizu_tests/data_structures/binary_trie.test.cpp @@ -1,34 +1,32 @@ #define PROBLEM \ "https://judge.yosupo.jp/problem/set_xor_min" #include "../template.hpp" -#include "../../../library/data_structures/binary_trie/binary_trie.hpp" +#include "../../../library/data_structures/binary_trie.hpp" int main() { cin.tie(0)->sync_with_stdio(0); int q; cin >> q; { - binary_trie bt_ll; - assert(bt_ll.mx_bit == 62); - bt_ll.update(61238612412983LL, 5); - int cnt = bt_ll.count(61238612412983LL); - assert(cnt == 5); - cnt = bt_ll.count(54289162783746217LL); - assert(cnt == 0); - int64_t res = bt_ll.walk(54289162783746217LL); - assert(res == 61238612412983LL); + binary_trie bt; + bt.update(61238612412983LL, 5); + assert( + bt.walk(54289162783746217LL) == 61238612412983LL); } - binary_trie bt_int; - assert(bt_int.mx_bit == 30); + binary_trie bt; + bt.update(0, 0); while (q--) { int type, x; cin >> type >> x; if (type == 0) { - if (bt_int.count(x) == 0) bt_int.update(x, 1); + if ( + bt.t[bt.t[0].next[0]].siz == 0 || bt.walk(x) != x) + bt.update(x, 1); } else if (type == 1) { - if (bt_int.count(x) == 1) bt_int.update(x, -1); + if (bt.t[bt.t[0].next[0]].siz > 0 && bt.walk(x) == x) + bt.update(x, -1); } else { assert(type == 2); - int val = bt_int.walk(x); + int val = bt.walk(x); cout << (val ^ x) << '\n'; } } From 1ab85cca6c15819b4678e43c353b190f5b3ae91b Mon Sep 17 00:00:00 2001 From: GitHub Date: Thu, 7 Aug 2025 00:40:38 +0000 Subject: [PATCH 2/4] [auto-verifier] verify commit f0c9133599fe17a5146b488f2f1b8e52edcc6f90 --- .verify-helper/timestamps.remote.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.verify-helper/timestamps.remote.json b/.verify-helper/timestamps.remote.json index c73b821b..9449ae56 100644 --- a/.verify-helper/timestamps.remote.json +++ b/.verify-helper/timestamps.remote.json @@ -3,7 +3,7 @@ "tests/library_checker_aizu_tests/convolution/lcm_convolution.test.cpp": "2025-06-25 15:06:18 -0600", "tests/library_checker_aizu_tests/convolution/min_plus_convolution.test.cpp": "2024-11-17 14:04:03 -0600", "tests/library_checker_aizu_tests/data_structures/binary_search_example.test.cpp": "2024-11-18 10:51:39 -0600", -"tests/library_checker_aizu_tests/data_structures/binary_trie.test.cpp": "2024-11-17 14:04:03 -0600", +"tests/library_checker_aizu_tests/data_structures/binary_trie.test.cpp": "2025-08-06 18:38:52 -0600", "tests/library_checker_aizu_tests/data_structures/bit.test.cpp": "2024-12-14 19:50:29 -0600", "tests/library_checker_aizu_tests/data_structures/bit_inc.test.cpp": "2024-11-18 09:44:22 -0600", "tests/library_checker_aizu_tests/data_structures/bit_ordered_set.test.cpp": "2024-12-15 14:34:10 -0600", From 2b0c99272b903b31f1a626abb51896fbbd86dfed Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Wed, 6 Aug 2025 18:44:46 -0600 Subject: [PATCH 3/4] fix --- library/data_structures/binary_trie.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/data_structures/binary_trie.hpp b/library/data_structures/binary_trie.hpp index 89dc8377..1ab72dbc 100644 --- a/library/data_structures/binary_trie.hpp +++ b/library/data_structures/binary_trie.hpp @@ -3,7 +3,7 @@ //! binary_trie bt; //! bt.update(num, 1); // insert //! bt.update(num, -1); // erase -//! @endco +//! @endcode //! @time O(q * mx_bit) //! @space O(q * mx_bit) const ll mx_bit = 1LL << 60; From f659a7a891d6695b93e868446ce04087a13ff02e Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Wed, 6 Aug 2025 18:45:26 -0600 Subject: [PATCH 4/4] fix --- tests/scripts/compile_commented_snippets.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/scripts/compile_commented_snippets.sh b/tests/scripts/compile_commented_snippets.sh index c451b3fa..caddfe5a 100755 --- a/tests/scripts/compile_commented_snippets.sh +++ b/tests/scripts/compile_commented_snippets.sh @@ -23,7 +23,7 @@ git submodule update echo "vector rhs;" echo "vector> mat;" echo "vector> grid;" - echo "int n,m,k,tl,tr,l,r,l1,r1,l2,r2,s_l,s_r,root_l,root_r,source,sink,total_flow,bccid,u,v,lsz,rsz,cols,cap;" + echo "int n,m,k,tl,tr,l,r,l1,r1,l2,r2,s_l,s_r,root_l,root_r,source,sink,total_flow,bccid,u,v,lsz,rsz,cols,cap,num;" } >entire_library_without_main {