From fcb8967deaf6d2679ec8ab3011d3f960e3038397 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Mon, 19 Jan 2026 01:16:34 -0700 Subject: [PATCH 01/11] Optimize wavelet_matrix constructor logic --- .../seg_tree_uncommon/wavelet_matrix.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/data_structures_[l,r)/seg_tree_uncommon/wavelet_matrix.hpp b/library/data_structures_[l,r)/seg_tree_uncommon/wavelet_matrix.hpp index 681e90ae..f3a29938 100644 --- a/library/data_structures_[l,r)/seg_tree_uncommon/wavelet_matrix.hpp +++ b/library/data_structures_[l,r)/seg_tree_uncommon/wavelet_matrix.hpp @@ -14,10 +14,10 @@ struct wavelet_matrix { wavelet_matrix(vector a, ull max_val): n(sz(a)), bv(bit_width(max_val), {{}}) { for (int h = sz(bv); h--;) { - int i = 0; vector b(n); + rep(i, 0, n) b[i] = (~a[i] >> h) & 1; ranges::stable_partition(a, - [&](ull x) { return b[i++] = (~x >> h) & 1; }); + [&](ull x) { return (~x >> h) & 1; }); bv[h] = b; } } From d8f6769204e2c93153eb5e9cdfad7645bbf5e0e0 Mon Sep 17 00:00:00 2001 From: GitHub Date: Mon, 19 Jan 2026 08:18:37 +0000 Subject: [PATCH 02/11] [auto-verifier] verify commit fcb8967deaf6d2679ec8ab3011d3f960e3038397 --- .verify-helper/timestamps.remote.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.verify-helper/timestamps.remote.json b/.verify-helper/timestamps.remote.json index e125288f..992f00a1 100644 --- a/.verify-helper/timestamps.remote.json +++ b/.verify-helper/timestamps.remote.json @@ -25,7 +25,7 @@ "tests/library_checker_aizu_tests/data_structures/implicit_seg_tree.test.cpp": "2026-01-18 11:15:41 +0000", "tests/library_checker_aizu_tests/data_structures/kruskal_tree_aizu.test.cpp": "2026-01-18 02:20:40 +0000", "tests/library_checker_aizu_tests/data_structures/kth_smallest_pst.test.cpp": "2026-01-18 11:15:41 +0000", -"tests/library_checker_aizu_tests/data_structures/kth_smallest_wavelet_matrix.test.cpp": "2026-01-18 02:20:40 +0000", +"tests/library_checker_aizu_tests/data_structures/kth_smallest_wavelet_matrix.test.cpp": "2026-01-19 01:16:34 -0700", "tests/library_checker_aizu_tests/data_structures/lazy_segment_tree.test.cpp": "2026-01-18 04:18:37 -0700", "tests/library_checker_aizu_tests/data_structures/lazy_segment_tree_constructor.test.cpp": "2026-01-18 04:18:37 -0700", "tests/library_checker_aizu_tests/data_structures/lazy_segment_tree_inc.test.cpp": "2026-01-18 11:04:58 +0000", @@ -75,7 +75,7 @@ "tests/library_checker_aizu_tests/handmade_tests/functional_graph.test.cpp": "2025-08-06 16:18:37 -0600", "tests/library_checker_aizu_tests/handmade_tests/hilbert_mos.test.cpp": "2026-01-18 02:20:40 +0000", "tests/library_checker_aizu_tests/handmade_tests/manacher.test.cpp": "2026-01-18 11:15:41 +0000", -"tests/library_checker_aizu_tests/handmade_tests/merge_st_and_wavelet.test.cpp": "2026-01-18 11:04:58 +0000", +"tests/library_checker_aizu_tests/handmade_tests/merge_st_and_wavelet.test.cpp": "2026-01-19 01:16:34 -0700", "tests/library_checker_aizu_tests/handmade_tests/mobius.test.cpp": "2025-02-10 14:50:36 -0700", "tests/library_checker_aizu_tests/handmade_tests/mod_division.test.cpp": "2025-09-07 16:12:35 -0600", "tests/library_checker_aizu_tests/handmade_tests/n_choose_k.test.cpp": "2025-08-28 13:19:16 -0600", From cd5257c9398555440090cc59f1af448fa47b0217 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Mon, 19 Jan 2026 01:23:38 -0700 Subject: [PATCH 03/11] Update wavelet_matrix.hpp --- .../seg_tree_uncommon/wavelet_matrix.hpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/library/data_structures_[l,r)/seg_tree_uncommon/wavelet_matrix.hpp b/library/data_structures_[l,r)/seg_tree_uncommon/wavelet_matrix.hpp index f3a29938..1b59a86e 100644 --- a/library/data_structures_[l,r)/seg_tree_uncommon/wavelet_matrix.hpp +++ b/library/data_structures_[l,r)/seg_tree_uncommon/wavelet_matrix.hpp @@ -11,13 +11,15 @@ struct wavelet_matrix { int n; vector bv; - wavelet_matrix(vector a, ull max_val): + wavelet_matrix(const vector& a, ull max_val): n(sz(a)), bv(bit_width(max_val), {{}}) { + vi idx(n); + iota(all(idx), 0); for (int h = sz(bv); h--;) { vector b(n); - rep(i, 0, n) b[i] = (~a[i] >> h) & 1; - ranges::stable_partition(a, - [&](ull x) { return (~x >> h) & 1; }); + rep(i, 0, n) b[i] = (~a[idx[i]] >> h) & 1; + ranges::stable_partition(idx, + [&](int i) { return b[i]; }); bv[h] = b; } } From d33c8d6ad7b529d498b855449a3b9d57f04e402c Mon Sep 17 00:00:00 2001 From: GitHub Date: Mon, 19 Jan 2026 08:26:16 +0000 Subject: [PATCH 04/11] [auto-verifier] verify commit cd5257c9398555440090cc59f1af448fa47b0217 --- .verify-helper/timestamps.remote.json | 2 -- 1 file changed, 2 deletions(-) diff --git a/.verify-helper/timestamps.remote.json b/.verify-helper/timestamps.remote.json index 992f00a1..869238d2 100644 --- a/.verify-helper/timestamps.remote.json +++ b/.verify-helper/timestamps.remote.json @@ -25,7 +25,6 @@ "tests/library_checker_aizu_tests/data_structures/implicit_seg_tree.test.cpp": "2026-01-18 11:15:41 +0000", "tests/library_checker_aizu_tests/data_structures/kruskal_tree_aizu.test.cpp": "2026-01-18 02:20:40 +0000", "tests/library_checker_aizu_tests/data_structures/kth_smallest_pst.test.cpp": "2026-01-18 11:15:41 +0000", -"tests/library_checker_aizu_tests/data_structures/kth_smallest_wavelet_matrix.test.cpp": "2026-01-19 01:16:34 -0700", "tests/library_checker_aizu_tests/data_structures/lazy_segment_tree.test.cpp": "2026-01-18 04:18:37 -0700", "tests/library_checker_aizu_tests/data_structures/lazy_segment_tree_constructor.test.cpp": "2026-01-18 04:18:37 -0700", "tests/library_checker_aizu_tests/data_structures/lazy_segment_tree_inc.test.cpp": "2026-01-18 11:04:58 +0000", @@ -75,7 +74,6 @@ "tests/library_checker_aizu_tests/handmade_tests/functional_graph.test.cpp": "2025-08-06 16:18:37 -0600", "tests/library_checker_aizu_tests/handmade_tests/hilbert_mos.test.cpp": "2026-01-18 02:20:40 +0000", "tests/library_checker_aizu_tests/handmade_tests/manacher.test.cpp": "2026-01-18 11:15:41 +0000", -"tests/library_checker_aizu_tests/handmade_tests/merge_st_and_wavelet.test.cpp": "2026-01-19 01:16:34 -0700", "tests/library_checker_aizu_tests/handmade_tests/mobius.test.cpp": "2025-02-10 14:50:36 -0700", "tests/library_checker_aizu_tests/handmade_tests/mod_division.test.cpp": "2025-09-07 16:12:35 -0600", "tests/library_checker_aizu_tests/handmade_tests/n_choose_k.test.cpp": "2025-08-28 13:19:16 -0600", From 813033ac006b76884f3ddd2a50579748b5eceb4b Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Mon, 19 Jan 2026 02:39:17 -0700 Subject: [PATCH 05/11] Revert "Update wavelet_matrix.hpp" This reverts commit cd5257c9398555440090cc59f1af448fa47b0217. --- .../seg_tree_uncommon/wavelet_count_less.hpp | 2 +- .../seg_tree_uncommon/wavelet_matrix.hpp | 20 +++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/library/data_structures_[l,r)/seg_tree_uncommon/wavelet_count_less.hpp b/library/data_structures_[l,r)/seg_tree_uncommon/wavelet_count_less.hpp index 2416ad02..6a41a0c1 100644 --- a/library/data_structures_[l,r)/seg_tree_uncommon/wavelet_count_less.hpp +++ b/library/data_structures_[l,r)/seg_tree_uncommon/wavelet_count_less.hpp @@ -2,7 +2,7 @@ //! count of i in [l..r) such that a[i] < ub //! @time O(log(max_val)) //! @space O(1) -int count(int l, int r, ull ub) { +int count(int l, int r, ll ub) { int res = 0; for (int h = sz(bv); h--;) { int l0 = bv[h].cnt(l), r0 = bv[h].cnt(r); diff --git a/library/data_structures_[l,r)/seg_tree_uncommon/wavelet_matrix.hpp b/library/data_structures_[l,r)/seg_tree_uncommon/wavelet_matrix.hpp index 681e90ae..01180104 100644 --- a/library/data_structures_[l,r)/seg_tree_uncommon/wavelet_matrix.hpp +++ b/library/data_structures_[l,r)/seg_tree_uncommon/wavelet_matrix.hpp @@ -1,27 +1,27 @@ #pragma once #include "wavelet_bit_vec.hpp" //! @code -//! vector a(n); -//! wavelet_matrix wm(a, 1e9); // requires a[i] <= 1e9 +//! vector a(n); +//! wavelet_matrix wm(a, 30); // 0 <= a[i] < (1<<30) //! wm.kth(l, r, k); //(k+1)th smallest number in [l,r) //! wm.kth(l, r, 0); //min in [l,r) //! @endcode -//! @time O(n * log(max_val) + q * log(max_val)) -//! @space O(n * log(max_val) / 64) +//! @time O(n * lg + q * lg) +//! @space O(n * lg / 64) struct wavelet_matrix { int n; vector bv; - wavelet_matrix(vector a, ull max_val): - n(sz(a)), bv(bit_width(max_val), {{}}) { + wavelet_matrix(vector a, int lg): + n(sz(a)), bv(lg, {{}}) { for (int h = sz(bv); h--;) { - int i = 0; vector b(n); - ranges::stable_partition(a, - [&](ull x) { return b[i++] = (~x >> h) & 1; }); + rep(i, 0, n) b[i] = (~a[i] >> h) & 1; bv[h] = b; + ranges::stable_partition(a, + [&](ll x) { return (~x >> h) & 1; }); } } - ull kth(int l, int r, int k) { + ll kth(int l, int r, int k) { ll res = 0; for (int h = sz(bv); h--;) { int l0 = bv[h].cnt(l), r0 = bv[h].cnt(r); From 078f50932835cddc2b1a3bdf0fdcd5b0165df6c9 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Mon, 19 Jan 2026 02:44:46 -0700 Subject: [PATCH 06/11] fix --- .../seg_tree_uncommon/wavelet_count_less.hpp | 2 +- .../seg_tree_uncommon/wavelet_matrix.hpp | 15 +-------------- 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/library/data_structures_[l,r)/seg_tree_uncommon/wavelet_count_less.hpp b/library/data_structures_[l,r)/seg_tree_uncommon/wavelet_count_less.hpp index 6a41a0c1..45062317 100644 --- a/library/data_structures_[l,r)/seg_tree_uncommon/wavelet_count_less.hpp +++ b/library/data_structures_[l,r)/seg_tree_uncommon/wavelet_count_less.hpp @@ -1,6 +1,6 @@ #pragma once //! count of i in [l..r) such that a[i] < ub -//! @time O(log(max_val)) +//! @time O(lg) //! @space O(1) int count(int l, int r, ll ub) { int res = 0; diff --git a/library/data_structures_[l,r)/seg_tree_uncommon/wavelet_matrix.hpp b/library/data_structures_[l,r)/seg_tree_uncommon/wavelet_matrix.hpp index be6fcfce..f66a237f 100644 --- a/library/data_structures_[l,r)/seg_tree_uncommon/wavelet_matrix.hpp +++ b/library/data_structures_[l,r)/seg_tree_uncommon/wavelet_matrix.hpp @@ -11,23 +11,10 @@ struct wavelet_matrix { int n; vector bv; -<<<<<<< HEAD - wavelet_matrix(vector a, int lg): - n(sz(a)), bv(lg, {{}}) { + wavelet_matrix(vector a, int lg): n(sz(a)), bv(lg, {{}}) { for (int h = sz(bv); h--;) { vector b(n); rep(i, 0, n) b[i] = (~a[i] >> h) & 1; -======= - wavelet_matrix(const vector& a, ull max_val): - n(sz(a)), bv(bit_width(max_val), {{}}) { - vi idx(n); - iota(all(idx), 0); - for (int h = sz(bv); h--;) { - vector b(n); - rep(i, 0, n) b[i] = (~a[idx[i]] >> h) & 1; - ranges::stable_partition(idx, - [&](int i) { return b[i]; }); ->>>>>>> d33c8d6ad7b529d498b855449a3b9d57f04e402c bv[h] = b; ranges::stable_partition(a, [&](ll x) { return (~x >> h) & 1; }); From 52e8a1fda1ca8f6935a4246b4102d5546cd4ce0c Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Mon, 19 Jan 2026 02:45:24 -0700 Subject: [PATCH 07/11] fix --- .../data_structures_[l,r)/seg_tree_uncommon/wavelet_matrix.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/data_structures_[l,r)/seg_tree_uncommon/wavelet_matrix.hpp b/library/data_structures_[l,r)/seg_tree_uncommon/wavelet_matrix.hpp index f66a237f..01180104 100644 --- a/library/data_structures_[l,r)/seg_tree_uncommon/wavelet_matrix.hpp +++ b/library/data_structures_[l,r)/seg_tree_uncommon/wavelet_matrix.hpp @@ -11,7 +11,8 @@ struct wavelet_matrix { int n; vector bv; - wavelet_matrix(vector a, int lg): n(sz(a)), bv(lg, {{}}) { + wavelet_matrix(vector a, int lg): + n(sz(a)), bv(lg, {{}}) { for (int h = sz(bv); h--;) { vector b(n); rep(i, 0, n) b[i] = (~a[i] >> h) & 1; From b363e4fe1eac836a4aa6a165bac4fe13001d44d5 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Mon, 19 Jan 2026 02:49:20 -0700 Subject: [PATCH 08/11] fix --- .../data_structures/kth_smallest_wavelet_matrix.test.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/library_checker_aizu_tests/data_structures/kth_smallest_wavelet_matrix.test.cpp b/tests/library_checker_aizu_tests/data_structures/kth_smallest_wavelet_matrix.test.cpp index 7b0c0c04..17d40734 100644 --- a/tests/library_checker_aizu_tests/data_structures/kth_smallest_wavelet_matrix.test.cpp +++ b/tests/library_checker_aizu_tests/data_structures/kth_smallest_wavelet_matrix.test.cpp @@ -6,12 +6,12 @@ int main() { cin.tie(0)->sync_with_stdio(0); int n, q; cin >> n >> q; - vector a(n); + vector a(n); for (int i = 0; i < n; i++) { cin >> a[i]; a[i] <<= 23; } - wavelet_matrix wm(a, 1'000'000'000LL << 23); + wavelet_matrix wm(a, 30 + 23); while (q--) { int l, r, k; cin >> l >> r >> k; From 45dd8c62de08fcb1c1141b6cbf458667a0e97130 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Mon, 19 Jan 2026 02:51:03 -0700 Subject: [PATCH 09/11] another fix --- .../handmade_tests/merge_st_and_wavelet.test.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/library_checker_aizu_tests/handmade_tests/merge_st_and_wavelet.test.cpp b/tests/library_checker_aizu_tests/handmade_tests/merge_st_and_wavelet.test.cpp index 9f25b3cd..20cea7b1 100644 --- a/tests/library_checker_aizu_tests/handmade_tests/merge_st_and_wavelet.test.cpp +++ b/tests/library_checker_aizu_tests/handmade_tests/merge_st_and_wavelet.test.cpp @@ -16,9 +16,9 @@ int main() { generate(begin(arr), end(arr), [&]() { return rnd(minn, maxn); }); merge_sort_tree mst(arr); - vector arr_shifted(n); + vector arr_shifted(n); rep(i, 0, n) arr_shifted[i] = arr[i] - minn; - wavelet_matrix wm(arr_shifted, maxn - minn); + wavelet_matrix wm(arr_shifted, 11); for (int queries = 30; queries--;) { int x = rnd(minn, maxn); int y = rnd(minn, maxn); From 17cdadb3594b2307a0656651fcfb18eebf39a4ae Mon Sep 17 00:00:00 2001 From: GitHub Date: Mon, 19 Jan 2026 09:53:23 +0000 Subject: [PATCH 10/11] [auto-verifier] verify commit 45dd8c62de08fcb1c1141b6cbf458667a0e97130 --- .verify-helper/timestamps.remote.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.verify-helper/timestamps.remote.json b/.verify-helper/timestamps.remote.json index 869238d2..ecaa6200 100644 --- a/.verify-helper/timestamps.remote.json +++ b/.verify-helper/timestamps.remote.json @@ -25,6 +25,7 @@ "tests/library_checker_aizu_tests/data_structures/implicit_seg_tree.test.cpp": "2026-01-18 11:15:41 +0000", "tests/library_checker_aizu_tests/data_structures/kruskal_tree_aizu.test.cpp": "2026-01-18 02:20:40 +0000", "tests/library_checker_aizu_tests/data_structures/kth_smallest_pst.test.cpp": "2026-01-18 11:15:41 +0000", +"tests/library_checker_aizu_tests/data_structures/kth_smallest_wavelet_matrix.test.cpp": "2026-01-19 02:49:20 -0700", "tests/library_checker_aizu_tests/data_structures/lazy_segment_tree.test.cpp": "2026-01-18 04:18:37 -0700", "tests/library_checker_aizu_tests/data_structures/lazy_segment_tree_constructor.test.cpp": "2026-01-18 04:18:37 -0700", "tests/library_checker_aizu_tests/data_structures/lazy_segment_tree_inc.test.cpp": "2026-01-18 11:04:58 +0000", @@ -74,6 +75,7 @@ "tests/library_checker_aizu_tests/handmade_tests/functional_graph.test.cpp": "2025-08-06 16:18:37 -0600", "tests/library_checker_aizu_tests/handmade_tests/hilbert_mos.test.cpp": "2026-01-18 02:20:40 +0000", "tests/library_checker_aizu_tests/handmade_tests/manacher.test.cpp": "2026-01-18 11:15:41 +0000", +"tests/library_checker_aizu_tests/handmade_tests/merge_st_and_wavelet.test.cpp": "2026-01-19 02:51:03 -0700", "tests/library_checker_aizu_tests/handmade_tests/mobius.test.cpp": "2025-02-10 14:50:36 -0700", "tests/library_checker_aizu_tests/handmade_tests/mod_division.test.cpp": "2025-09-07 16:12:35 -0600", "tests/library_checker_aizu_tests/handmade_tests/n_choose_k.test.cpp": "2025-08-28 13:19:16 -0600", From 87b09c912cba57cf40802cdfa86a00538d49513c Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Mon, 19 Jan 2026 02:55:42 -0700 Subject: [PATCH 11/11] another nit --- .../data_structures_[l,r)/seg_tree_uncommon/wavelet_matrix.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/data_structures_[l,r)/seg_tree_uncommon/wavelet_matrix.hpp b/library/data_structures_[l,r)/seg_tree_uncommon/wavelet_matrix.hpp index 01180104..bfa64728 100644 --- a/library/data_structures_[l,r)/seg_tree_uncommon/wavelet_matrix.hpp +++ b/library/data_structures_[l,r)/seg_tree_uncommon/wavelet_matrix.hpp @@ -2,7 +2,7 @@ #include "wavelet_bit_vec.hpp" //! @code //! vector a(n); -//! wavelet_matrix wm(a, 30); // 0 <= a[i] < (1<<30) +//! wavelet_matrix wm(a, 30); // 0 <= a[i] < (1LL<<30) //! wm.kth(l, r, k); //(k+1)th smallest number in [l,r) //! wm.kth(l, r, 0); //min in [l,r) //! @endcode