Skip to content

Commit d3d4b85

Browse files
lrvideckisweb-flow
andauthored
Golf (#181)
* Enhance walk function with optimized logic Refactor walk function to improve performance and clarity. * Fix boundary condition in segment tree walk * [auto-verifier] verify commit 3beb2d1 * Fix boundary condition in segment tree walk function * Fix bug in segment tree walk function * [auto-verifier] verify commit 267032c * Update segment tree walk logic with good flag Refactor segment tree walk function to use a boolean flag for condition checking. * [auto-verifier] verify commit cc93d62 * Fix loop condition in segment tree walk function * Fix return statement syntax in walk.hpp * [auto-verifier] verify commit 3f31866 * Fix loop condition and variable initialization in walk.hpp * [auto-verifier] verify commit b741e86 * Update walk.hpp * [auto-verifier] verify commit a837375 * more golf! * [auto-verifier] verify commit b9c25bc * Reverse conditions in walk function logic * Change condition in walk function for sum check * Fix comment to clarify index search condition * [auto-verifier] verify commit 6405c5e * format * [auto-verifier] verify commit 434c09a * Fix formatting of if statement in test code * [auto-verifier] verify commit abdb86b * format * [auto-verifier] verify commit 7949f33 --------- Co-authored-by: GitHub <noreply@github.com>
1 parent 350f6d9 commit d3d4b85

File tree

4 files changed

+15
-20
lines changed

4 files changed

+15
-20
lines changed

.verify-helper/timestamps.remote.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,6 @@
4343
"tests/library_checker_aizu_tests/data_structures/rmq_linear.test.cpp": "2026-01-18 11:15:41 +0000",
4444
"tests/library_checker_aizu_tests/data_structures/rmq_sparse_table.test.cpp": "2026-01-18 11:15:41 +0000",
4545
"tests/library_checker_aizu_tests/data_structures/rmq_sparse_table_inc.test.cpp": "2026-01-18 11:15:41 +0000",
46-
"tests/library_checker_aizu_tests/data_structures/simple_tree.test.cpp": "2026-02-07 20:45:45 +0000",
47-
"tests/library_checker_aizu_tests/data_structures/simple_tree_inc.test.cpp": "2026-02-05 16:06:35 -0700",
48-
"tests/library_checker_aizu_tests/data_structures/simple_tree_inc_line.test.cpp": "2026-02-05 16:06:35 -0700",
49-
"tests/library_checker_aizu_tests/data_structures/simple_tree_inc_walk.test.cpp": "2026-02-05 16:06:35 -0700",
50-
"tests/library_checker_aizu_tests/data_structures/simple_tree_line.test.cpp": "2026-02-07 20:45:45 +0000",
51-
"tests/library_checker_aizu_tests/data_structures/simple_tree_walk.test.cpp": "2026-02-07 20:45:45 +0000",
5246
"tests/library_checker_aizu_tests/flow/dinic_aizu.test.cpp": "2024-11-17 14:04:03 -0600",
5347
"tests/library_checker_aizu_tests/flow/hungarian.test.cpp": "2024-11-17 14:04:03 -0600",
5448
"tests/library_checker_aizu_tests/flow/min_cost_max_flow.test.cpp": "2024-12-05 10:41:42 -0600",

library/data_structures_[l,r]/seg_tree.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//! tree st(n, INT_MAX, ranges::min);
1414
//! int idx = st.walk(l, r, [&](int value) {
1515
//! return value <= x;
16-
//! }); // smallest index in [l, r] s.t. f is true
16+
//! }); // smallest index in [l, r] s.t. f is false
1717
//! }
1818
//! @endcode
1919
//! @time O(n + q log n)
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
int walk(int l, int r, const auto& f) {
2-
for (l += n, r += n; l <= r;)
3-
if (int u = nxt(l, r); f(s[u])) {
4-
while (u < n)
5-
if (f(s[2 * u])) u *= 2;
6-
else (u *= 2)++;
7-
return u - n;
8-
}
9-
return -1;
2+
while (l <= r) {
3+
int u = l + n, x = __lg(min(u & -u, r - l + 1));
4+
if (f(s[u >> x])) l += 1 << x;
5+
else r = l + (1 << x) - 2;
6+
}
7+
return l;
108
}

tests/library_checker_aizu_tests/data_structures/simple_tree_inc_walk.test.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ int main() {
2222
cout << st.query(k, k) << '\n';
2323
} else if (type == 3) {
2424
// returns first element in [k,n-1] such that sum > 0
25-
cout << st.walk(k, n - 1, [&](int sum) {
26-
return sum > 0;
27-
}) << '\n';
25+
int idx = st.walk(k, n - 1,
26+
[&](int sum) { return sum == 0; });
27+
if (idx == n) idx = -1;
28+
cout << idx << '\n';
2829
} else {
2930
assert(type == 4);
3031
int total = st.query(0, k);
@@ -33,8 +34,10 @@ int main() {
3334
} else {
3435
int pref_sum = 0;
3536
cout << st.walk(0, k, [&](int sum) {
36-
if (pref_sum + sum == total) return 1;
37-
pref_sum += sum;
37+
if (pref_sum + sum < total) {
38+
pref_sum += sum;
39+
return 1;
40+
}
3841
return 0;
3942
}) << '\n';
4043
}

0 commit comments

Comments
 (0)