Skip to content

Commit c762fa0

Browse files
lrvideckisweb-flow
andauthored
wavelet nit (#172)
* Optimize wavelet_matrix constructor logic * [auto-verifier] verify commit fcb8967 * Update wavelet_matrix.hpp * [auto-verifier] verify commit cd5257c * Revert "Update wavelet_matrix.hpp" This reverts commit cd5257c. * fix * fix * fix * another fix * [auto-verifier] verify commit 45dd8c6 * another nit --------- Co-authored-by: GitHub <noreply@github.com>
1 parent 6f3a198 commit c762fa0

File tree

5 files changed

+18
-18
lines changed

5 files changed

+18
-18
lines changed

.verify-helper/timestamps.remote.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"tests/library_checker_aizu_tests/data_structures/implicit_seg_tree.test.cpp": "2026-01-18 11:15:41 +0000",
2626
"tests/library_checker_aizu_tests/data_structures/kruskal_tree_aizu.test.cpp": "2026-01-18 02:20:40 +0000",
2727
"tests/library_checker_aizu_tests/data_structures/kth_smallest_pst.test.cpp": "2026-01-18 11:15:41 +0000",
28-
"tests/library_checker_aizu_tests/data_structures/kth_smallest_wavelet_matrix.test.cpp": "2026-01-18 02:20:40 +0000",
28+
"tests/library_checker_aizu_tests/data_structures/kth_smallest_wavelet_matrix.test.cpp": "2026-01-19 02:49:20 -0700",
2929
"tests/library_checker_aizu_tests/data_structures/lazy_segment_tree.test.cpp": "2026-01-18 04:18:37 -0700",
3030
"tests/library_checker_aizu_tests/data_structures/lazy_segment_tree_constructor.test.cpp": "2026-01-18 04:18:37 -0700",
3131
"tests/library_checker_aizu_tests/data_structures/lazy_segment_tree_inc.test.cpp": "2026-01-18 11:04:58 +0000",
@@ -75,7 +75,7 @@
7575
"tests/library_checker_aizu_tests/handmade_tests/functional_graph.test.cpp": "2025-08-06 16:18:37 -0600",
7676
"tests/library_checker_aizu_tests/handmade_tests/hilbert_mos.test.cpp": "2026-01-18 02:20:40 +0000",
7777
"tests/library_checker_aizu_tests/handmade_tests/manacher.test.cpp": "2026-01-18 11:15:41 +0000",
78-
"tests/library_checker_aizu_tests/handmade_tests/merge_st_and_wavelet.test.cpp": "2026-01-18 11:04:58 +0000",
78+
"tests/library_checker_aizu_tests/handmade_tests/merge_st_and_wavelet.test.cpp": "2026-01-19 02:51:03 -0700",
7979
"tests/library_checker_aizu_tests/handmade_tests/mobius.test.cpp": "2025-02-10 14:50:36 -0700",
8080
"tests/library_checker_aizu_tests/handmade_tests/mod_division.test.cpp": "2025-09-07 16:12:35 -0600",
8181
"tests/library_checker_aizu_tests/handmade_tests/n_choose_k.test.cpp": "2025-08-28 13:19:16 -0600",

library/data_structures_[l,r)/seg_tree_uncommon/wavelet_count_less.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#pragma once
22
//! count of i in [l..r) such that a[i] < ub
3-
//! @time O(log(max_val))
3+
//! @time O(lg)
44
//! @space O(1)
5-
int count(int l, int r, ull ub) {
5+
int count(int l, int r, ll ub) {
66
int res = 0;
77
for (int h = sz(bv); h--;) {
88
int l0 = bv[h].cnt(l), r0 = bv[h].cnt(r);

library/data_structures_[l,r)/seg_tree_uncommon/wavelet_matrix.hpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
#pragma once
22
#include "wavelet_bit_vec.hpp"
33
//! @code
4-
//! vector<ull> a(n);
5-
//! wavelet_matrix wm(a, 1e9); // requires a[i] <= 1e9
4+
//! vector<ll> a(n);
5+
//! wavelet_matrix wm(a, 30); // 0 <= a[i] < (1LL<<30)
66
//! wm.kth(l, r, k); //(k+1)th smallest number in [l,r)
77
//! wm.kth(l, r, 0); //min in [l,r)
88
//! @endcode
9-
//! @time O(n * log(max_val) + q * log(max_val))
10-
//! @space O(n * log(max_val) / 64)
9+
//! @time O(n * lg + q * lg)
10+
//! @space O(n * lg / 64)
1111
struct wavelet_matrix {
1212
int n;
1313
vector<bit_vec> bv;
14-
wavelet_matrix(vector<ull> a, ull max_val):
15-
n(sz(a)), bv(bit_width(max_val), {{}}) {
14+
wavelet_matrix(vector<ll> a, int lg):
15+
n(sz(a)), bv(lg, {{}}) {
1616
for (int h = sz(bv); h--;) {
17-
int i = 0;
1817
vector<bool> b(n);
19-
ranges::stable_partition(a,
20-
[&](ull x) { return b[i++] = (~x >> h) & 1; });
18+
rep(i, 0, n) b[i] = (~a[i] >> h) & 1;
2119
bv[h] = b;
20+
ranges::stable_partition(a,
21+
[&](ll x) { return (~x >> h) & 1; });
2222
}
2323
}
24-
ull kth(int l, int r, int k) {
24+
ll kth(int l, int r, int k) {
2525
ll res = 0;
2626
for (int h = sz(bv); h--;) {
2727
int l0 = bv[h].cnt(l), r0 = bv[h].cnt(r);

tests/library_checker_aizu_tests/data_structures/kth_smallest_wavelet_matrix.test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ int main() {
66
cin.tie(0)->sync_with_stdio(0);
77
int n, q;
88
cin >> n >> q;
9-
vector<ull> a(n);
9+
vector<ll> a(n);
1010
for (int i = 0; i < n; i++) {
1111
cin >> a[i];
1212
a[i] <<= 23;
1313
}
14-
wavelet_matrix wm(a, 1'000'000'000LL << 23);
14+
wavelet_matrix wm(a, 30 + 23);
1515
while (q--) {
1616
int l, r, k;
1717
cin >> l >> r >> k;

tests/library_checker_aizu_tests/handmade_tests/merge_st_and_wavelet.test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ int main() {
1616
generate(begin(arr), end(arr),
1717
[&]() { return rnd<int>(minn, maxn); });
1818
merge_sort_tree mst(arr);
19-
vector<ull> arr_shifted(n);
19+
vector<ll> arr_shifted(n);
2020
rep(i, 0, n) arr_shifted[i] = arr[i] - minn;
21-
wavelet_matrix wm(arr_shifted, maxn - minn);
21+
wavelet_matrix wm(arr_shifted, 11);
2222
for (int queries = 30; queries--;) {
2323
int x = rnd<int>(minn, maxn);
2424
int y = rnd<int>(minn, maxn);

0 commit comments

Comments
 (0)