Skip to content

Commit 0cdd3be

Browse files
committed
add lambda walk to BIT
1 parent 2db3ca4 commit 0cdd3be

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

library/data_structures_[l,r)/bit.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ struct BIT {
1515
return ret;
1616
}
1717
ll query(int l, int r) { return query(r) - query(l); }
18+
#include "bit_uncommon/walk_lambda.hpp"
1819
#include "bit_uncommon/walk.hpp"
1920
};

library/data_structures_[l,r)/bit_uncommon/walk.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Requires sum of [i,i] >= 0
22
//! Returns min r s.t. sum of [0,r] >= sum
33
//! Returns n if sum of [0,n-1] < sum
4-
int walk(ll sum) {
4+
int walk2(ll sum) {
55
if (sum <= 0) return -1;
66
int r = 0;
77
for (int i = bit_floor(size(s)); i; i /= 2)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
void walk(const auto& f) {
2+
ll sum = 0;
3+
for (int i = bit_floor(size(s)), r = 0; i; i /= 2)
4+
if (r + i <= sz(s) && f(r + i, sum + s[r + i - 1]))
5+
sum += s[(r += i) - 1];
6+
}

tests/library_checker_aizu_tests/data_structures/bit_walk.test.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,23 @@ int main() {
4747
}
4848
return 1;
4949
};
50-
int res = bit.walk(order + 1);
50+
int res = bit.walk2(order + 1);
5151
assert(res == st.find_first(0, n, f));
5252
assert(res ==
5353
st.find_first(k, n,
5454
[&](int64_t x, int, int) -> bool {
5555
return x > 0;
5656
}));
5757
if (res == n) res = -1;
58+
int res_lambda = -1;
59+
bit.walk([&](int r, ll sum) {
60+
if (sum > order) {
61+
res_lambda = r - 1;
62+
return 0;
63+
}
64+
return 1;
65+
});
66+
assert(res == res_lambda);
5867
cout << res << '\n';
5968
} else {
6069
if (bit.query(k, k + 1) == 1) {
@@ -71,13 +80,25 @@ int main() {
7180
}
7281
return 1;
7382
};
74-
int res = bit.walk(order);
83+
int res = bit.walk2(order);
7584
assert(max(res, 0) == st.find_first(0, n, f));
7685
assert(res ==
7786
st.find_last(0, k + 1,
7887
[&](int64_t x, int, int) -> bool {
7988
return x > 0;
8089
}));
90+
int res_lambda = -1;
91+
if (order) {
92+
bit.walk([&](int r, ll sum) {
93+
if (sum < order) {
94+
res_lambda = r;
95+
return 1;
96+
}
97+
res_lambda = r - 1;
98+
return 0;
99+
});
100+
}
101+
assert(res == res_lambda);
81102
cout << res << '\n';
82103
}
83104
}

0 commit comments

Comments
 (0)