From f7a9f8f95c3a66cb5a15fc43c26427c637a41ee5 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Fri, 3 Apr 2026 11:21:19 -0600 Subject: [PATCH 01/10] add walk for [l,r] now --- library/data_structures_[l,r]/bit.hpp | 1 + .../bit_uncommon/walk_lambda.hpp | 6 +++ .../data_structures/bit_inc_walk.test.cpp | 51 +++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 library/data_structures_[l,r]/bit_uncommon/walk_lambda.hpp create mode 100644 tests/library_checker_aizu_tests/data_structures/bit_inc_walk.test.cpp diff --git a/library/data_structures_[l,r]/bit.hpp b/library/data_structures_[l,r]/bit.hpp index 72a77a7c..c88805db 100644 --- a/library/data_structures_[l,r]/bit.hpp +++ b/library/data_structures_[l,r]/bit.hpp @@ -17,5 +17,6 @@ struct BIT { ll query(int l, int r) { return query(r) - query(l - 1); } +#include "bit_uncommon/walk_lambda.hpp" #include "../data_structures_[l,r)/bit_uncommon/walk.hpp" }; diff --git a/library/data_structures_[l,r]/bit_uncommon/walk_lambda.hpp b/library/data_structures_[l,r]/bit_uncommon/walk_lambda.hpp new file mode 100644 index 00000000..a87ee4af --- /dev/null +++ b/library/data_structures_[l,r]/bit_uncommon/walk_lambda.hpp @@ -0,0 +1,6 @@ +void walk(const auto& f) { + ll sum = 0; + for (int i = bit_floor(size(s)), r = 0; i; i /= 2) + if (r + i <= sz(s) && f(r + i - 1, sum + s[r + i - 1])) + sum += s[(r += i) - 1]; +} diff --git a/tests/library_checker_aizu_tests/data_structures/bit_inc_walk.test.cpp b/tests/library_checker_aizu_tests/data_structures/bit_inc_walk.test.cpp new file mode 100644 index 00000000..f9d5cf87 --- /dev/null +++ b/tests/library_checker_aizu_tests/data_structures/bit_inc_walk.test.cpp @@ -0,0 +1,51 @@ +#define PROBLEM \ + "https://judge.yosupo.jp/problem/predecessor_problem" +#include "../template.hpp" +#include "../../../library/data_structures_[l,r]/bit.hpp" +int main() { + cin.tie(0)->sync_with_stdio(0); + int n, q; + string s; + cin >> n >> q >> s; + vector init(n); + for (int i = 0; i < n; i++) init[i] = s[i] - '0'; + BIT bit(init); + while (q--) { + int type, k; + cin >> type >> k; + if (type == 0) { + if (bit.query(k, k) == 0) bit.update(k, 1); + } else if (type == 1) { + if (bit.query(k, k) == 1) bit.update(k, -1); + } else if (type == 2) { + cout << bit.query(k, k) << '\n'; + } else if (type == 3) { + if (bit.query(k, n - 1) == 0) cout << -1 << '\n'; + else { + ll order = bit.query(k - 1); + int res = -1; + bit.walk([&](int r, ll sum) { + if (sum <= order) return 1; + res = r; + return 0; + }); + cout << res << '\n'; + } + } else { + if (bit.query(k) == 0) cout << -1 << '\n'; + else { + ll order = bit.query(k); + int res = -1; + bit.walk([&](int r, ll sum) { + if (sum >= order) { + res = r; + return 0; + } + return 1; + }); + cout << res << '\n'; + } + } + } + return 0; +} From 891327ade2cfaa5accb807dfa885a1f8d007573b Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Fri, 3 Apr 2026 11:24:56 -0600 Subject: [PATCH 02/10] fix --- .../library_checker_aizu_tests/data_structures/bit.test.cpp | 2 +- .../data_structures/bit_ordered_set.test.cpp | 6 +++--- .../handmade_tests/seg_tree_find.test.cpp | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/library_checker_aizu_tests/data_structures/bit.test.cpp b/tests/library_checker_aizu_tests/data_structures/bit.test.cpp index f0353e12..3f80ba46 100644 --- a/tests/library_checker_aizu_tests/data_structures/bit.test.cpp +++ b/tests/library_checker_aizu_tests/data_structures/bit.test.cpp @@ -58,7 +58,7 @@ int main() { } return 1; }; - assert(bit.walk(sum) == st.find_first(0, n, f)); + assert(bit.walk2(sum) == st.find_first(0, n, f)); } return 0; } diff --git a/tests/library_checker_aizu_tests/data_structures/bit_ordered_set.test.cpp b/tests/library_checker_aizu_tests/data_structures/bit_ordered_set.test.cpp index 3349d015..aeef3a5c 100644 --- a/tests/library_checker_aizu_tests/data_structures/bit_ordered_set.test.cpp +++ b/tests/library_checker_aizu_tests/data_structures/bit_ordered_set.test.cpp @@ -40,7 +40,7 @@ int main() { x = get_compressed_idx(x); if (bit.query(x, x) == 1) bit.update(x, -1); } else if (type == 2) { - int res = bit.walk(x); + int res = bit.walk2(x); if (res == -1 || res == ssize(compress)) cout << -1 << '\n'; else cout << compress[res] << '\n'; @@ -49,12 +49,12 @@ int main() { cout << bit.query(x) << '\n'; } else if (type == 4) { x = get_compressed_idx(x); - int res = bit.walk(bit.query(x)); + int res = bit.walk2(bit.query(x)); if (res == -1) cout << -1 << '\n'; else cout << compress[res] << '\n'; } else { x = get_compressed_idx(x); - int res = bit.walk(bit.query(x - 1) + 1); + int res = bit.walk2(bit.query(x - 1) + 1); if (res == ssize(bit.s)) cout << -1 << '\n'; else cout << compress[res] << '\n'; } diff --git a/tests/library_checker_aizu_tests/handmade_tests/seg_tree_find.test.cpp b/tests/library_checker_aizu_tests/handmade_tests/seg_tree_find.test.cpp index 42248b64..9efea020 100644 --- a/tests/library_checker_aizu_tests/handmade_tests/seg_tree_find.test.cpp +++ b/tests/library_checker_aizu_tests/handmade_tests/seg_tree_find.test.cpp @@ -38,7 +38,7 @@ int main() { rngs.push_back({tl, tr, 1}); return 1; }; - int pos = min(bit.walk(bit.query(l) + sum), r); + int pos = min(bit.walk2(bit.query(l) + sum), r); reset(); assert(pos == seg.find_first(l, r, f)); assert(!empty(rngs)); From b2715bf73aa7aa1ee342e66f22aca64a7701fb12 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Fri, 3 Apr 2026 11:29:57 -0600 Subject: [PATCH 03/10] update docs now --- library/data_structures_[l,r)/bit_uncommon/walk_lambda.hpp | 5 +++++ library/data_structures_[l,r]/bit_uncommon/walk_lambda.hpp | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/library/data_structures_[l,r)/bit_uncommon/walk_lambda.hpp b/library/data_structures_[l,r)/bit_uncommon/walk_lambda.hpp index a652e94d..9b46525a 100644 --- a/library/data_structures_[l,r)/bit_uncommon/walk_lambda.hpp +++ b/library/data_structures_[l,r)/bit_uncommon/walk_lambda.hpp @@ -1,3 +1,8 @@ +//! @code +//! bit.walk([&](int r, ll sum) -> bool { +//! // sum = a[0] + a[1] + ... + a[r - 1] +//! }); +//! @endcode void walk(const auto& f) { ll sum = 0; for (int i = bit_floor(size(s)), r = 0; i; i /= 2) diff --git a/library/data_structures_[l,r]/bit_uncommon/walk_lambda.hpp b/library/data_structures_[l,r]/bit_uncommon/walk_lambda.hpp index a87ee4af..8466a224 100644 --- a/library/data_structures_[l,r]/bit_uncommon/walk_lambda.hpp +++ b/library/data_structures_[l,r]/bit_uncommon/walk_lambda.hpp @@ -1,3 +1,8 @@ +//! @code +//! bit.walk([&](int r, ll sum) -> bool { +//! // sum = a[0] + a[1] + ... + a[r] +//! }); +//! @endcode void walk(const auto& f) { ll sum = 0; for (int i = bit_floor(size(s)), r = 0; i; i /= 2) From f42d6cbf08aa587489aa319a8893c7a0dab416c3 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Fri, 3 Apr 2026 11:30:44 -0600 Subject: [PATCH 04/10] fix build --- tests/.config/.code_snippet_excluded_file_list | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/.config/.code_snippet_excluded_file_list b/tests/.config/.code_snippet_excluded_file_list index f6dd52d1..6680d0b6 100644 --- a/tests/.config/.code_snippet_excluded_file_list +++ b/tests/.config/.code_snippet_excluded_file_list @@ -18,6 +18,7 @@ find_substrings_concatenated.hpp pascals_identity.hpp binary_search.hpp walk.hpp +walk_lambda.hpp prev.hpp next.hpp wavelet_count_less.hpp From 9d93798d3596475383b43247674cba44bbb92229 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Fri, 3 Apr 2026 11:33:42 -0600 Subject: [PATCH 05/10] update docs again --- library/data_structures_[l,r)/bit.hpp | 8 ++++++++ library/data_structures_[l,r)/bit_uncommon/walk.hpp | 3 --- .../data_structures_[l,r)/bit_uncommon/walk_lambda.hpp | 5 ----- library/data_structures_[l,r]/bit.hpp | 8 ++++++++ .../data_structures_[l,r]/bit_uncommon/walk_lambda.hpp | 5 ----- 5 files changed, 16 insertions(+), 13 deletions(-) diff --git a/library/data_structures_[l,r)/bit.hpp b/library/data_structures_[l,r)/bit.hpp index 4b6cc0be..2a8404df 100644 --- a/library/data_structures_[l,r)/bit.hpp +++ b/library/data_structures_[l,r)/bit.hpp @@ -1,4 +1,12 @@ #pragma once +//! @code +//! bit.walk([&](int r, ll sum) -> bool { +//! // sum = a[0] + a[1] + ... + a[r - 1] +//! }); +//! int r = bit.walk2(sum); +//! // Returns min r s.t. sum of [0,r] >= sum +//! // Returns n if sum of [0,n-1] < sum +//! @endcode //! @time O(n + q log n) //! @space O(n) // NOLINTNEXTLINE(readability-identifier-naming) diff --git a/library/data_structures_[l,r)/bit_uncommon/walk.hpp b/library/data_structures_[l,r)/bit_uncommon/walk.hpp index 85d984d1..20c3a732 100644 --- a/library/data_structures_[l,r)/bit_uncommon/walk.hpp +++ b/library/data_structures_[l,r)/bit_uncommon/walk.hpp @@ -1,6 +1,3 @@ -//! Requires sum of [i,i] >= 0 -//! Returns min r s.t. sum of [0,r] >= sum -//! Returns n if sum of [0,n-1] < sum int walk2(ll sum) { if (sum <= 0) return -1; int r = 0; diff --git a/library/data_structures_[l,r)/bit_uncommon/walk_lambda.hpp b/library/data_structures_[l,r)/bit_uncommon/walk_lambda.hpp index 9b46525a..a652e94d 100644 --- a/library/data_structures_[l,r)/bit_uncommon/walk_lambda.hpp +++ b/library/data_structures_[l,r)/bit_uncommon/walk_lambda.hpp @@ -1,8 +1,3 @@ -//! @code -//! bit.walk([&](int r, ll sum) -> bool { -//! // sum = a[0] + a[1] + ... + a[r - 1] -//! }); -//! @endcode void walk(const auto& f) { ll sum = 0; for (int i = bit_floor(size(s)), r = 0; i; i /= 2) diff --git a/library/data_structures_[l,r]/bit.hpp b/library/data_structures_[l,r]/bit.hpp index c88805db..6be41606 100644 --- a/library/data_structures_[l,r]/bit.hpp +++ b/library/data_structures_[l,r]/bit.hpp @@ -1,4 +1,12 @@ #pragma once +//! @code +//! bit.walk([&](int r, ll sum) -> bool { +//! // sum = a[0] + a[1] + ... + a[r] +//! }); +//! int r = bit.walk2(sum); +//! // Returns min r s.t. sum of [0,r] >= sum +//! // Returns n if sum of [0,n-1] < sum +//! @endcode //! @time O(n + q log n) //! @space O(n) // NOLINTNEXTLINE(readability-identifier-naming) diff --git a/library/data_structures_[l,r]/bit_uncommon/walk_lambda.hpp b/library/data_structures_[l,r]/bit_uncommon/walk_lambda.hpp index 8466a224..a87ee4af 100644 --- a/library/data_structures_[l,r]/bit_uncommon/walk_lambda.hpp +++ b/library/data_structures_[l,r]/bit_uncommon/walk_lambda.hpp @@ -1,8 +1,3 @@ -//! @code -//! bit.walk([&](int r, ll sum) -> bool { -//! // sum = a[0] + a[1] + ... + a[r] -//! }); -//! @endcode void walk(const auto& f) { ll sum = 0; for (int i = bit_floor(size(s)), r = 0; i; i /= 2) From 8f0513c971a6677f530d777cd4800981539de81a Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Fri, 3 Apr 2026 11:34:34 -0600 Subject: [PATCH 06/10] fix build --- library/data_structures_[l,r)/bit.hpp | 1 + library/data_structures_[l,r]/bit.hpp | 1 + 2 files changed, 2 insertions(+) diff --git a/library/data_structures_[l,r)/bit.hpp b/library/data_structures_[l,r)/bit.hpp index 2a8404df..0ee5f36c 100644 --- a/library/data_structures_[l,r)/bit.hpp +++ b/library/data_structures_[l,r)/bit.hpp @@ -1,5 +1,6 @@ #pragma once //! @code +//! BIT bit(n); //! bit.walk([&](int r, ll sum) -> bool { //! // sum = a[0] + a[1] + ... + a[r - 1] //! }); diff --git a/library/data_structures_[l,r]/bit.hpp b/library/data_structures_[l,r]/bit.hpp index 6be41606..ba96de89 100644 --- a/library/data_structures_[l,r]/bit.hpp +++ b/library/data_structures_[l,r]/bit.hpp @@ -1,5 +1,6 @@ #pragma once //! @code +//! BIT bit(n); //! bit.walk([&](int r, ll sum) -> bool { //! // sum = a[0] + a[1] + ... + a[r] //! }); From c784643546afb822b1d2409b6d59e3296dff44a9 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Fri, 3 Apr 2026 11:36:28 -0600 Subject: [PATCH 07/10] fix --- tests/.config/.cppcheck_suppression_list | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/.config/.cppcheck_suppression_list b/tests/.config/.cppcheck_suppression_list index aa381399..3640e873 100644 --- a/tests/.config/.cppcheck_suppression_list +++ b/tests/.config/.cppcheck_suppression_list @@ -61,5 +61,6 @@ unusedFunction:../kactl/content/data-structures/UnionFind.h:14 unusedFunction:../kactl/content/number-theory/ModPow.h:13 unusedFunction:../kactl/stress-tests/utilities/genTree.h:49 containerOutOfBounds:../library/data_structures_[l,r)/uncommon/permutation_tree.hpp:85 -ctuOneDefinitionRuleViolation:../library/data_structures_[l,r)/bit.hpp:5 +ctuOneDefinitionRuleViolation:../library/data_structures_[l,r)/bit.hpp:13 +ctuOneDefinitionRuleViolation:../library/data_structures_[l,r]/bit.hpp:13 ctuOneDefinitionRuleViolation:../library/data_structures_[l,r)/lazy_seg_tree.hpp:4 From d1c5d5f0b34325001646ae588faec081c8aef873 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Fri, 3 Apr 2026 11:37:24 -0600 Subject: [PATCH 08/10] yet another fix lol --- 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 4d73abfc..785ace98 100755 --- a/tests/scripts/compile_commented_snippets.sh +++ b/tests/scripts/compile_commented_snippets.sh @@ -25,7 +25,7 @@ git submodule update echo "vi 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,w,lsz,rsz,cols,cap,num,x,y,i,j,i1,i2,j1,j2,len;" + 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,w,lsz,rsz,cols,cap,num,x,y,i,j,i1,i2,j1,j2,len,sum;" } >entire_library_without_main { From abd141bf3c674c9c4da3dd82d20dff4325e723b9 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Fri, 3 Apr 2026 11:38:09 -0600 Subject: [PATCH 09/10] fix --- tests/.config/.cppcheck_suppression_list | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/.config/.cppcheck_suppression_list b/tests/.config/.cppcheck_suppression_list index 3640e873..0efd6e3d 100644 --- a/tests/.config/.cppcheck_suppression_list +++ b/tests/.config/.cppcheck_suppression_list @@ -61,6 +61,6 @@ unusedFunction:../kactl/content/data-structures/UnionFind.h:14 unusedFunction:../kactl/content/number-theory/ModPow.h:13 unusedFunction:../kactl/stress-tests/utilities/genTree.h:49 containerOutOfBounds:../library/data_structures_[l,r)/uncommon/permutation_tree.hpp:85 -ctuOneDefinitionRuleViolation:../library/data_structures_[l,r)/bit.hpp:13 -ctuOneDefinitionRuleViolation:../library/data_structures_[l,r]/bit.hpp:13 +ctuOneDefinitionRuleViolation:../library/data_structures_[l,r)/bit.hpp:14 +ctuOneDefinitionRuleViolation:../library/data_structures_[l,r]/bit.hpp:14 ctuOneDefinitionRuleViolation:../library/data_structures_[l,r)/lazy_seg_tree.hpp:4 From 60aaf508dbd93e08a953ab3043e92d9762f15543 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Fri, 3 Apr 2026 11:39:30 -0600 Subject: [PATCH 10/10] sdaoivnsdfoiasndvoiasndvoinaes FIX --- tests/.config/.cppcheck_suppression_list | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/.config/.cppcheck_suppression_list b/tests/.config/.cppcheck_suppression_list index 0efd6e3d..79794aaa 100644 --- a/tests/.config/.cppcheck_suppression_list +++ b/tests/.config/.cppcheck_suppression_list @@ -62,5 +62,4 @@ unusedFunction:../kactl/content/number-theory/ModPow.h:13 unusedFunction:../kactl/stress-tests/utilities/genTree.h:49 containerOutOfBounds:../library/data_structures_[l,r)/uncommon/permutation_tree.hpp:85 ctuOneDefinitionRuleViolation:../library/data_structures_[l,r)/bit.hpp:14 -ctuOneDefinitionRuleViolation:../library/data_structures_[l,r]/bit.hpp:14 ctuOneDefinitionRuleViolation:../library/data_structures_[l,r)/lazy_seg_tree.hpp:4