Skip to content

Commit adaa1d2

Browse files
committed
nit to wavelet matrix
1 parent 0bb7ffc commit adaa1d2

File tree

3 files changed

+11
-13
lines changed

3 files changed

+11
-13
lines changed

library/data_structures/seg_tree_uncommon/wavelet_bit_vec.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ struct bit_vec {
1010
rep(i, 0, sz(b) - 1) b[i + 1].second =
1111
popcount(b[i].first) + b[i].second;
1212
}
13-
//! @returns !a[0] + !a[1] + ... + !a[r - 1]
13+
//! @returns a[0] + a[1] + ... + a[r - 1]
1414
//! @time O(1)
1515
//! @space O(1)
16-
int cnt0(int r) {
16+
int cnt(int r) {
1717
auto [x, y] = b[r >> 6];
18-
return r - y - popcount(x & ((1ULL << (r & 63)) - 1));
18+
return y + popcount(x & ((1ULL << (r & 63)) - 1));
1919
}
2020
};

library/data_structures/seg_tree_uncommon/wavelet_count_less.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
int count(int l, int r, ull ub) {
66
int res = 0;
77
for (int h = sz(bv); h--;) {
8-
int l0 = bv[h].cnt0(l), r0 = bv[h].cnt0(r);
8+
int l0 = bv[h].cnt(l), r0 = bv[h].cnt(r);
99
if ((~ub >> h) & 1) l = l0, r = r0;
1010
else
11-
res += r0 - l0, l += bv[h].cnt0(n) - l0,
12-
r += bv[h].cnt0(n) - r0;
11+
res += r0 - l0, l += bv[h].cnt(n) - l0,
12+
r += bv[h].cnt(n) - r0;
1313
}
1414
return res;
1515
}

library/data_structures/seg_tree_uncommon/wavelet_matrix.hpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@ struct wavelet_matrix {
88
//! @space O(n * log(max_val) / 64)
99
wavelet_matrix(vector<ull> a, ull max_val):
1010
n(sz(a)), bv(bit_width(max_val), {{}}) {
11-
vector<ull> nxt(n);
1211
for (int h = sz(bv); h--;) {
12+
int i = 0;
1313
vector<bool> b(n);
14-
rep(i, 0, n) b[i] = (a[i] >> h) & 1;
14+
ranges::stable_partition(a,
15+
[&](ull x) { return b[i++] = !((x >> h) & 1); });
1516
bv[h] = b;
16-
array it{begin(nxt), begin(nxt) + bv[h].cnt0(n)};
17-
rep(i, 0, n) * it[b[i]]++ = a[i];
18-
swap(a, nxt);
1917
}
2018
}
2119
//! (k+1)th smallest number in [l,r)
@@ -25,11 +23,11 @@ struct wavelet_matrix {
2523
ull kth(int l, int r, int k) {
2624
ll res = 0;
2725
for (int h = sz(bv); h--;) {
28-
int l0 = bv[h].cnt0(l), r0 = bv[h].cnt0(r);
26+
int l0 = bv[h].cnt(l), r0 = bv[h].cnt(r);
2927
if (k < r0 - l0) l = l0, r = r0;
3028
else
3129
k -= r0 - l0, res |= 1ULL << h,
32-
l += bv[h].cnt0(n) - l0, r += bv[h].cnt0(n) - r0;
30+
l += bv[h].cnt(n) - l0, r += bv[h].cnt(n) - r0;
3331
}
3432
return res;
3533
}

0 commit comments

Comments
 (0)