We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent f833ab1 commit 040cc2dCopy full SHA for 040cc2d
library/data_structures/bit_uncommon/walk.hpp
@@ -1,10 +1,11 @@
1
-//! Requires s[i] >= 0
2
-//! max r such that sum of [0,r) < sum, or -1
+//! Requires sum of [i,i] >= 0
+//! Returns min r st sum of [0,r] >= sum
3
+//! Returns n if sum of [0,n-1] < sum
4
int walk(ll sum) {
5
if (sum <= 0) return -1;
6
int r = 0;
- for (int pw = bit_floor(size(s)); pw; pw >>= 1)
7
- if (r + pw <= sz(s) && s[r + pw - 1] < sum)
8
- sum -= s[(r += pw) - 1];
+ for (int i = bit_floor(size(s)); i; i /= 2)
+ if (r + i <= sz(s) && s[r + i - 1] < sum)
9
+ sum -= s[(r += i) - 1];
10
return r;
11
}
0 commit comments