From b2d2c6d16d4862eb43e86233605d9a63c94c73e6 Mon Sep 17 00:00:00 2001 From: Andrei Navumenka Date: Sun, 7 Jul 2024 14:41:21 -0400 Subject: [PATCH 1/2] Improve java perstistent tree --- java/structures/PersistentTree.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/java/structures/PersistentTree.java b/java/structures/PersistentTree.java index b0694f5c..2b9450b5 100644 --- a/java/structures/PersistentTree.java +++ b/java/structures/PersistentTree.java @@ -13,10 +13,7 @@ public static class Node { Node(Node left, Node right) { this.left = left; this.right = right; - if (left != null) - sum += left.sum; - if (right != null) - sum += right.sum; + sum = left.sum + right.sum; } } From ca773958189d525ab059913227b6461eeeb42696 Mon Sep 17 00:00:00 2001 From: Andrei Navumenka Date: Sun, 7 Jul 2024 15:18:14 -0400 Subject: [PATCH 2/2] Add rust binary search --- rust/Cargo.toml | 4 ++++ rust/misc/binary_search.rs | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 rust/misc/binary_search.rs diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 9115b4f4..27937258 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -38,3 +38,7 @@ path = "structures/fenwick_tree.rs" [[bin]] name = "persistent_tree" path = "structures/persistent_tree.rs" + +[[bin]] +name = "binary_search" +path = "misc/binary_search.rs" diff --git a/rust/misc/binary_search.rs b/rust/misc/binary_search.rs new file mode 100644 index 00000000..f72281e1 --- /dev/null +++ b/rust/misc/binary_search.rs @@ -0,0 +1,35 @@ +// invariant: f[lo] == false, f[hi] == true +pub fn binary_search_first_true(f: F, from_inclusive: i32, to_inclusive: i32) -> i32 +where + F: Fn(i32) -> bool, +{ + let mut lo = from_inclusive - 1; + let mut hi = to_inclusive + 1; + while hi - lo > 1 { + let mid = (lo + hi) / 2; + if !f(mid) { + lo = mid; + } else { + hi = mid; + } + } + hi +} + +#[cfg(test)] +mod tests { + use rstest::rstest; + use crate::binary_search_first_true; + + #[rstest] + #[case(100, 0)] + #[case(100, 1)] + #[case(100, 50)] + #[case(100, 100)] + fn basic_test( + #[case] n: i32, + #[case] pos: i32, + ) { + assert_eq!(binary_search_first_true(|i| { i >= pos }, 0, n), pos); + } +}