From 6ff9e79200f400980b46f3a9058b4862ebe1428a Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Mon, 1 Jul 2024 09:14:49 -0500 Subject: [PATCH 01/39] saving progress --- Cargo.toml | 4 ++++ examples/monotonic/mono_st.rs | 13 +++++++++++++ src/lib.rs | 1 + src/monotonic/mod.rs | 2 ++ src/monotonic/mono_st.rs | 10 ++++++++++ 5 files changed, 30 insertions(+) create mode 100644 examples/monotonic/mono_st.rs create mode 100644 src/monotonic/mod.rs create mode 100644 src/monotonic/mono_st.rs diff --git a/Cargo.toml b/Cargo.toml index ebf48e8d..2d16723f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -203,3 +203,7 @@ path = "examples/helpers/lis_yosupo.rs" [[example]] name = "lis_pop" path = "examples/helpers/lis_pop.rs" + +[[example]] +name = "mono_st" +path = "examples/monotonic/mono_st.rs" diff --git a/examples/monotonic/mono_st.rs b/examples/monotonic/mono_st.rs new file mode 100644 index 00000000..b5b6c1c1 --- /dev/null +++ b/examples/monotonic/mono_st.rs @@ -0,0 +1,13 @@ +// verification-helper: PROBLEM https://judge.yosupo.jp/problem/staticrmq + +use proconio::input; +use programming_team_code_rust::monotonic::mono_st::mono_st; + +fn main() { + input! { + n: usize, + q: usize, + a: [u32], + } + let le = mono_st(&a); +} diff --git a/src/lib.rs b/src/lib.rs index 302bb761..75abd2e0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,5 +2,6 @@ pub mod data_structures; pub mod graphs; pub mod helpers; +pub mod monotonic; pub mod numbers; pub mod strings; diff --git a/src/monotonic/mod.rs b/src/monotonic/mod.rs new file mode 100644 index 00000000..0a15b5f6 --- /dev/null +++ b/src/monotonic/mod.rs @@ -0,0 +1,2 @@ +//! # Monotonic +pub mod mono_st; diff --git a/src/monotonic/mono_st.rs b/src/monotonic/mono_st.rs new file mode 100644 index 00000000..73dab95e --- /dev/null +++ b/src/monotonic/mono_st.rs @@ -0,0 +1,10 @@ +pub fn mono_st(a: &[T]) -> Vec> { + let mut le = vec![None; a.len()]; + for (i, num) in a.iter().enumerate() { + le[i] = Some(i - 1); + while le[i].is_some() && a[le[i].unwrap()] < *num { + le[i] = le[le[i].unwrap()]; + } + } + le +} From fef6a441867d8dbfce46fac168107c9b9e924d59 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Mon, 1 Jul 2024 09:33:09 -0500 Subject: [PATCH 02/39] saving progress --- examples/monotonic/mono_st.rs | 20 ++++++++++++++++++++ src/monotonic/mono_st.rs | 9 ++++----- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/examples/monotonic/mono_st.rs b/examples/monotonic/mono_st.rs index b5b6c1c1..4a2eaffa 100644 --- a/examples/monotonic/mono_st.rs +++ b/examples/monotonic/mono_st.rs @@ -1,6 +1,7 @@ // verification-helper: PROBLEM https://judge.yosupo.jp/problem/staticrmq use proconio::input; +use programming_team_code_rust::data_structures::rmq::RMQ; use programming_team_code_rust::monotonic::mono_st::mono_st; fn main() { @@ -9,5 +10,24 @@ fn main() { q: usize, a: [u32], } + let le = mono_st(&a); + + let rmq = RMQ::new(&(0..n).collect::>(), |i1, i2| { + if a[i1] < a[i2] { + i1 + } else { + i2 + } + }); + + for _ in 0..q { + input! { + le: usize, + ri: usize, + } + let idx_min = rmq.query(le..ri); + + println!("{}", idx_min); + } } diff --git a/src/monotonic/mono_st.rs b/src/monotonic/mono_st.rs index 73dab95e..9da6494d 100644 --- a/src/monotonic/mono_st.rs +++ b/src/monotonic/mono_st.rs @@ -1,9 +1,8 @@ -pub fn mono_st(a: &[T]) -> Vec> { - let mut le = vec![None; a.len()]; +pub fn mono_st(a: &[T]) -> Vec { + let mut le = (0..a.len()).collect::>(); for (i, num) in a.iter().enumerate() { - le[i] = Some(i - 1); - while le[i].is_some() && a[le[i].unwrap()] < *num { - le[i] = le[le[i].unwrap()]; + while le[i] > 0 && a[le[i] - 1] < *num { + le[i] = le[le[i] - 1]; } } le From c40002e9f2222da2b662b45e2dc2e4b0cf09ce7f Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Mon, 1 Jul 2024 09:56:21 -0500 Subject: [PATCH 03/39] Update mono_st.rs --- examples/monotonic/mono_st.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/monotonic/mono_st.rs b/examples/monotonic/mono_st.rs index 4a2eaffa..8e72667f 100644 --- a/examples/monotonic/mono_st.rs +++ b/examples/monotonic/mono_st.rs @@ -28,6 +28,6 @@ fn main() { } let idx_min = rmq.query(le..ri); - println!("{}", idx_min); + println!("{}", a[idx_min]); } } From 7a899cc8b0cf45e28ec78d9155a18a60b665e7b6 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Mon, 1 Jul 2024 11:40:59 -0500 Subject: [PATCH 04/39] saving progress --- examples/monotonic/mono_st.rs | 12 ++++++++---- src/monotonic/mono_st.rs | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/examples/monotonic/mono_st.rs b/examples/monotonic/mono_st.rs index 8e72667f..429b75b9 100644 --- a/examples/monotonic/mono_st.rs +++ b/examples/monotonic/mono_st.rs @@ -8,7 +8,7 @@ fn main() { input! { n: usize, q: usize, - a: [u32], + a: [u32; n], } let le = mono_st(&a); @@ -23,10 +23,14 @@ fn main() { for _ in 0..q { input! { - le: usize, - ri: usize, + l: usize, + r: usize, + } + let idx_min = rmq.query(l..r); + assert!(le[idx_min] <= l); + if le[idx_min] > 0 { + assert!(a[le[idx_min] - 1] < a[idx_min]); } - let idx_min = rmq.query(le..ri); println!("{}", a[idx_min]); } diff --git a/src/monotonic/mono_st.rs b/src/monotonic/mono_st.rs index 9da6494d..3df80c3d 100644 --- a/src/monotonic/mono_st.rs +++ b/src/monotonic/mono_st.rs @@ -1,7 +1,7 @@ pub fn mono_st(a: &[T]) -> Vec { let mut le = (0..a.len()).collect::>(); for (i, num) in a.iter().enumerate() { - while le[i] > 0 && a[le[i] - 1] < *num { + while le[i] > 0 && a[le[i] - 1] > *num { le[i] = le[le[i] - 1]; } } From fd59fe11104010d40f0dbbbba3de702bcc4cce17 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Mon, 1 Jul 2024 11:47:28 -0500 Subject: [PATCH 05/39] saving progress --- examples/monotonic/mono_st.rs | 4 ++++ src/monotonic/mod.rs | 1 + src/monotonic/mono_range.rs | 11 +++++++++++ 3 files changed, 16 insertions(+) create mode 100644 src/monotonic/mono_range.rs diff --git a/examples/monotonic/mono_st.rs b/examples/monotonic/mono_st.rs index 429b75b9..8a84c87c 100644 --- a/examples/monotonic/mono_st.rs +++ b/examples/monotonic/mono_st.rs @@ -2,6 +2,7 @@ use proconio::input; use programming_team_code_rust::data_structures::rmq::RMQ; +use programming_team_code_rust::monotonic::mono_range::mono_range; use programming_team_code_rust::monotonic::mono_st::mono_st; fn main() { @@ -12,6 +13,7 @@ fn main() { } let le = mono_st(&a); + let ri = mono_range(&le); let rmq = RMQ::new(&(0..n).collect::>(), |i1, i2| { if a[i1] < a[i2] { @@ -32,6 +34,8 @@ fn main() { assert!(a[le[idx_min] - 1] < a[idx_min]); } + assert!(r <= ri[idx_min]); + println!("{}", a[idx_min]); } } diff --git a/src/monotonic/mod.rs b/src/monotonic/mod.rs index 0a15b5f6..0bf4863a 100644 --- a/src/monotonic/mod.rs +++ b/src/monotonic/mod.rs @@ -1,2 +1,3 @@ //! # Monotonic +pub mod mono_range; pub mod mono_st; diff --git a/src/monotonic/mono_range.rs b/src/monotonic/mono_range.rs new file mode 100644 index 00000000..69070fe9 --- /dev/null +++ b/src/monotonic/mono_range.rs @@ -0,0 +1,11 @@ +pub fn mono_range(le: &[usize]) -> Vec { + let mut ri = vec![le.len(); le.len()]; + for (i, &num) in le.iter().enumerate() { + let mut j = i; + while j != num { + ri[j - 1] = i; + j = le[j - 1]; + } + } + ri +} From 90572e94561304b0b9116bf1d284b22170d8a68b Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Mon, 1 Jul 2024 11:48:10 -0500 Subject: [PATCH 06/39] saving progress --- examples/monotonic/mono_st.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/monotonic/mono_st.rs b/examples/monotonic/mono_st.rs index 8a84c87c..5c2ee2b1 100644 --- a/examples/monotonic/mono_st.rs +++ b/examples/monotonic/mono_st.rs @@ -35,6 +35,9 @@ fn main() { } assert!(r <= ri[idx_min]); + if ri[idx_min] < n { + assert!(a[ri[idx_min]] < a[idx_min]); + } println!("{}", a[idx_min]); } From 96635dd2ffb9d4ed4339ef869f41030b9f24807a Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Mon, 1 Jul 2024 11:51:40 -0500 Subject: [PATCH 07/39] another assert --- examples/monotonic/mono_st.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/monotonic/mono_st.rs b/examples/monotonic/mono_st.rs index 5c2ee2b1..eacad311 100644 --- a/examples/monotonic/mono_st.rs +++ b/examples/monotonic/mono_st.rs @@ -28,13 +28,15 @@ fn main() { l: usize, r: usize, } + let idx_min = rmq.query(l..r); + assert!(le[idx_min] <= l); + assert!(r <= ri[idx_min]); + assert_eq!(a[rmq.query(le[idx_min]..ri[idx_min])], a[idx_min]); if le[idx_min] > 0 { assert!(a[le[idx_min] - 1] < a[idx_min]); } - - assert!(r <= ri[idx_min]); if ri[idx_min] < n { assert!(a[ri[idx_min]] < a[idx_min]); } From 882bdba826d1c8a5f3c563638ce54f80184c93b9 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Mon, 1 Jul 2024 11:53:03 -0500 Subject: [PATCH 08/39] remove --- examples/monotonic/mono_st.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/examples/monotonic/mono_st.rs b/examples/monotonic/mono_st.rs index eacad311..74c49799 100644 --- a/examples/monotonic/mono_st.rs +++ b/examples/monotonic/mono_st.rs @@ -31,8 +31,6 @@ fn main() { let idx_min = rmq.query(l..r); - assert!(le[idx_min] <= l); - assert!(r <= ri[idx_min]); assert_eq!(a[rmq.query(le[idx_min]..ri[idx_min])], a[idx_min]); if le[idx_min] > 0 { assert!(a[le[idx_min] - 1] < a[idx_min]); From 03682e84c1cbe22806ea19d1ff2e3d8d8dee356a Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Mon, 1 Jul 2024 13:25:56 -0500 Subject: [PATCH 09/39] add test --- examples/monotonic/mono_st.rs | 4 +++- src/monotonic/mono_st.rs | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/examples/monotonic/mono_st.rs b/examples/monotonic/mono_st.rs index 74c49799..d13e5b3a 100644 --- a/examples/monotonic/mono_st.rs +++ b/examples/monotonic/mono_st.rs @@ -12,7 +12,9 @@ fn main() { a: [u32; n], } - let le = mono_st(&a); + let le = mono_st(&a, |x, y| x.le(y)); + let le1 = mono_st(&a, |x, y| x.lt(y)); + assert_eq!(le, le1); let ri = mono_range(&le); let rmq = RMQ::new(&(0..n).collect::>(), |i1, i2| { diff --git a/src/monotonic/mono_st.rs b/src/monotonic/mono_st.rs index 3df80c3d..96cbbd6a 100644 --- a/src/monotonic/mono_st.rs +++ b/src/monotonic/mono_st.rs @@ -1,7 +1,7 @@ -pub fn mono_st(a: &[T]) -> Vec { +pub fn mono_st bool>(a: &[T], cmp: F) -> Vec { let mut le = (0..a.len()).collect::>(); for (i, num) in a.iter().enumerate() { - while le[i] > 0 && a[le[i] - 1] > *num { + while le[i] > 0 && !cmp(&a[le[i] - 1], num) { le[i] = le[le[i] - 1]; } } From 2eb1c128fae91c06bc0909805799b6e3526bdbec Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Mon, 1 Jul 2024 13:31:33 -0500 Subject: [PATCH 10/39] asdf --- examples/monotonic/mono_st.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/monotonic/mono_st.rs b/examples/monotonic/mono_st.rs index d13e5b3a..e00e98eb 100644 --- a/examples/monotonic/mono_st.rs +++ b/examples/monotonic/mono_st.rs @@ -13,8 +13,8 @@ fn main() { } let le = mono_st(&a, |x, y| x.le(y)); - let le1 = mono_st(&a, |x, y| x.lt(y)); - assert_eq!(le, le1); + assert_eq!(le, mono_st(&a, |x, y| x.lt(y))); //TODO wait till lib checker PR merges and watch + //this fail let ri = mono_range(&le); let rmq = RMQ::new(&(0..n).collect::>(), |i1, i2| { From 72658ee329af01201e07aef811843223c7335996 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Mon, 1 Jul 2024 13:56:05 -0500 Subject: [PATCH 11/39] finish --- src/monotonic/mono_st.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/monotonic/mono_st.rs b/src/monotonic/mono_st.rs index 96cbbd6a..af11c6b9 100644 --- a/src/monotonic/mono_st.rs +++ b/src/monotonic/mono_st.rs @@ -1,3 +1,19 @@ +//! # Monotonic Stack + +/// # Example +/// ``` +/// use programming_team_code_rust::monotonic::mono_st::mono_st; +/// +/// let a: Vec = vec![3, 1, 2, 2]; +/// assert_eq!(mono_st(&a, |x, y| x.lt(y)), [0, 0, 2, 2]); +/// assert_eq!(mono_st(&a, |x, y| x.le(y)), [0, 0, 2, 3]); +/// assert_eq!(mono_st(&a, |x, y| x.gt(y)), [0, 1, 1, 1]); +/// assert_eq!(mono_st(&a, |x, y| x.ge(y)), [0, 1, 1, 3]); +/// ``` +/// +/// # Complexity +/// - Time: O(n) +/// - Space: O(n) pub fn mono_st bool>(a: &[T], cmp: F) -> Vec { let mut le = (0..a.len()).collect::>(); for (i, num) in a.iter().enumerate() { From 8eb0abe2f1c30251c8c19e89907950d8ee1dfff9 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Mon, 1 Jul 2024 18:46:24 -0500 Subject: [PATCH 12/39] better --- examples/monotonic/mono_st.rs | 34 +++++++++++++++++++++++++++++----- src/monotonic/mono_range.rs | 10 +++++----- src/monotonic/mono_st.rs | 29 ++++++++++++++++++++++++----- 3 files changed, 58 insertions(+), 15 deletions(-) diff --git a/examples/monotonic/mono_st.rs b/examples/monotonic/mono_st.rs index e00e98eb..d5d231ed 100644 --- a/examples/monotonic/mono_st.rs +++ b/examples/monotonic/mono_st.rs @@ -12,11 +12,6 @@ fn main() { a: [u32; n], } - let le = mono_st(&a, |x, y| x.le(y)); - assert_eq!(le, mono_st(&a, |x, y| x.lt(y))); //TODO wait till lib checker PR merges and watch - //this fail - let ri = mono_range(&le); - let rmq = RMQ::new(&(0..n).collect::>(), |i1, i2| { if a[i1] < a[i2] { i1 @@ -25,6 +20,35 @@ fn main() { } }); + let le = mono_st(&a, |x, y| x.le(y)); + assert_eq!(le, mono_st(&a, |x, y| x.lt(y))); //TODO wait till lib checker PR merges and watch + //this fail + let ri = mono_range(&le); + + { + let mut iterations = 0; + for i in 0..n { + let mut j = i; + while j != le[i] { + iterations += 1; + //TODO: change these to asserts + //assert!(a[rmq.query(le[j - 1]..j)] >= a[j]); + if le[j - 1] > 0 { + //assert!(a[le[j - 1] - 1] <= a[j]); + } + // !cmp(a[k], a[j]) is true for all k in [le[j - 1], j) + // cmp(a[le[j - 1] - 1], a[j]) is true + j = le[j - 1]; + } + } + let mut j = n; + while j != 0 { + iterations += 1; + j = le[j - 1]; + } + assert_eq!(iterations, n); + } + for _ in 0..q { input! { l: usize, diff --git a/src/monotonic/mono_range.rs b/src/monotonic/mono_range.rs index 69070fe9..fa33a42f 100644 --- a/src/monotonic/mono_range.rs +++ b/src/monotonic/mono_range.rs @@ -1,10 +1,10 @@ -pub fn mono_range(le: &[usize]) -> Vec { +pub fn mono_range(le: &[Option]) -> Vec { let mut ri = vec![le.len(); le.len()]; - for (i, &num) in le.iter().enumerate() { - let mut j = i; + for (i, &num) in le.iter().enumerate().skip(1) { + let mut j = Some(i - 1); while j != num { - ri[j - 1] = i; - j = le[j - 1]; + ri[j.unwrap()] = i; + j = le[j.unwrap()]; } } ri diff --git a/src/monotonic/mono_st.rs b/src/monotonic/mono_st.rs index af11c6b9..81d0a40d 100644 --- a/src/monotonic/mono_st.rs +++ b/src/monotonic/mono_st.rs @@ -9,16 +9,35 @@ /// assert_eq!(mono_st(&a, |x, y| x.le(y)), [0, 0, 2, 3]); /// assert_eq!(mono_st(&a, |x, y| x.gt(y)), [0, 1, 1, 1]); /// assert_eq!(mono_st(&a, |x, y| x.ge(y)), [0, 1, 1, 3]); +/// +/// let le = mono_st(&a, |x, y| x.lt(y)); +/// let mut iterations = 0; +/// for i in 0..a.len() { +/// let mut j = i; +/// while j != le[i] { +/// iterations += 1; +/// // !cmp(a[k], a[j]) is true for all k in [le[j - 1], j) +/// // cmp(a[le[j - 1] - 1], a[j]) is true +/// j = le[j - 1]; +/// } +/// } +/// let mut j = n; +/// while le[i] != le[j] { +/// iterations += 1; +/// j = le[j - 1]; +/// } +/// assert_eq!(iterations, a.len()); /// ``` /// /// # Complexity /// - Time: O(n) /// - Space: O(n) -pub fn mono_st bool>(a: &[T], cmp: F) -> Vec { - let mut le = (0..a.len()).collect::>(); - for (i, num) in a.iter().enumerate() { - while le[i] > 0 && !cmp(&a[le[i] - 1], num) { - le[i] = le[le[i] - 1]; +pub fn mono_st bool>(a: &[T], cmp: F) -> Vec> { + let mut le = vec![None; a.len()]; + for (i, num) in a.iter().enumerate().skip(1) { + le[i] = Some(i - 1); + while le[i].is_some() && !cmp(&a[le[i].unwrap()], num) { + le[i] = le[le[i].unwrap()]; } } le From c656614ee1e4bd4bc61c0998bdb0308f74f35529 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Mon, 1 Jul 2024 18:49:04 -0500 Subject: [PATCH 13/39] fix --- examples/monotonic/mono_st.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/examples/monotonic/mono_st.rs b/examples/monotonic/mono_st.rs index d5d231ed..ebff8b1d 100644 --- a/examples/monotonic/mono_st.rs +++ b/examples/monotonic/mono_st.rs @@ -25,6 +25,7 @@ fn main() { //this fail let ri = mono_range(&le); + /* { let mut iterations = 0; for i in 0..n { @@ -48,6 +49,7 @@ fn main() { } assert_eq!(iterations, n); } + */ for _ in 0..q { input! { @@ -57,9 +59,12 @@ fn main() { let idx_min = rmq.query(l..r); - assert_eq!(a[rmq.query(le[idx_min]..ri[idx_min])], a[idx_min]); - if le[idx_min] > 0 { - assert!(a[le[idx_min] - 1] < a[idx_min]); + assert_eq!( + a[rmq.query(le[idx_min].unwrap_or(usize::MAX).wrapping_add(1)..ri[idx_min])], + a[idx_min] + ); + if let Some(idx) = le[idx_min] { + assert!(a[idx] < a[idx_min]); } if ri[idx_min] < n { assert!(a[ri[idx_min]] < a[idx_min]); From 153cec04af565a62ced75bed3db9e57a1a2e6f56 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Mon, 1 Jul 2024 18:51:05 -0500 Subject: [PATCH 14/39] uncomment --- examples/monotonic/mono_st.rs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/examples/monotonic/mono_st.rs b/examples/monotonic/mono_st.rs index ebff8b1d..207e4560 100644 --- a/examples/monotonic/mono_st.rs +++ b/examples/monotonic/mono_st.rs @@ -25,31 +25,29 @@ fn main() { //this fail let ri = mono_range(&le); - /* { let mut iterations = 0; - for i in 0..n { - let mut j = i; + for i in 1..n { + let mut j = Some(i - 1); while j != le[i] { iterations += 1; //TODO: change these to asserts //assert!(a[rmq.query(le[j - 1]..j)] >= a[j]); - if le[j - 1] > 0 { - //assert!(a[le[j - 1] - 1] <= a[j]); - } + //if le[j - 1] > 0 { + //assert!(a[le[j - 1] - 1] <= a[j]); + //} // !cmp(a[k], a[j]) is true for all k in [le[j - 1], j) // cmp(a[le[j - 1] - 1], a[j]) is true - j = le[j - 1]; + j = le[j.unwrap()]; } } - let mut j = n; - while j != 0 { + let mut j = Some(n - 1); + while j.is_some() { iterations += 1; - j = le[j - 1]; + j = le[j.unwrap()]; } assert_eq!(iterations, n); } - */ for _ in 0..q { input! { From dc0c7366c60b91da398273831d36ad93197b45ae Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Wed, 3 Jul 2024 09:55:00 -0500 Subject: [PATCH 15/39] Update mono_st.rs --- src/monotonic/mono_st.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/monotonic/mono_st.rs b/src/monotonic/mono_st.rs index 81d0a40d..4c3a9805 100644 --- a/src/monotonic/mono_st.rs +++ b/src/monotonic/mono_st.rs @@ -32,12 +32,12 @@ /// # Complexity /// - Time: O(n) /// - Space: O(n) -pub fn mono_st bool>(a: &[T], cmp: F) -> Vec> { - let mut le = vec![None; a.len()]; +pub fn mono_st bool>(a: &[T], cmp: F) -> Vec { + let mut le = vec![usize::MAX; a.len()]; for (i, num) in a.iter().enumerate().skip(1) { - le[i] = Some(i - 1); - while le[i].is_some() && !cmp(&a[le[i].unwrap()], num) { - le[i] = le[le[i].unwrap()]; + le[i] = i - 1; + while le[i] != usize::MAX && !cmp(&a[le[i]], num) { + le[i] = le[le[i]]; } } le From c87a5441ae7e4386c27a8fb646932fad0f3e11fb Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Wed, 3 Jul 2024 09:56:12 -0500 Subject: [PATCH 16/39] Update mono_range.rs --- src/monotonic/mono_range.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/monotonic/mono_range.rs b/src/monotonic/mono_range.rs index fa33a42f..84bebc34 100644 --- a/src/monotonic/mono_range.rs +++ b/src/monotonic/mono_range.rs @@ -1,10 +1,10 @@ -pub fn mono_range(le: &[Option]) -> Vec { +pub fn mono_range(le: &[usize]) -> Vec { let mut ri = vec![le.len(); le.len()]; for (i, &num) in le.iter().enumerate().skip(1) { - let mut j = Some(i - 1); + let mut j = i - 1; while j != num { - ri[j.unwrap()] = i; - j = le[j.unwrap()]; + ri[j] = i; + j = le[j]; } } ri From 574b24f9b59d1fd29cddf2ea29ea242f2d012a8a Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Wed, 3 Jul 2024 09:57:51 -0500 Subject: [PATCH 17/39] Update mono_st.rs --- examples/monotonic/mono_st.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/monotonic/mono_st.rs b/examples/monotonic/mono_st.rs index 207e4560..3d2e32d2 100644 --- a/examples/monotonic/mono_st.rs +++ b/examples/monotonic/mono_st.rs @@ -28,7 +28,7 @@ fn main() { { let mut iterations = 0; for i in 1..n { - let mut j = Some(i - 1); + let mut j = i - 1; while j != le[i] { iterations += 1; //TODO: change these to asserts @@ -38,13 +38,13 @@ fn main() { //} // !cmp(a[k], a[j]) is true for all k in [le[j - 1], j) // cmp(a[le[j - 1] - 1], a[j]) is true - j = le[j.unwrap()]; + j = le[j]; } } - let mut j = Some(n - 1); - while j.is_some() { + let mut j = n - 1; + while j != usize::MAX { iterations += 1; - j = le[j.unwrap()]; + j = le[j]; } assert_eq!(iterations, n); } @@ -58,7 +58,7 @@ fn main() { let idx_min = rmq.query(l..r); assert_eq!( - a[rmq.query(le[idx_min].unwrap_or(usize::MAX).wrapping_add(1)..ri[idx_min])], + a[rmq.query(le[idx_min].wrapping_add(1)..ri[idx_min])], a[idx_min] ); if let Some(idx) = le[idx_min] { From e1f81e5ada6b008c4bc74ff5d7ec5acff8f04cb4 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Wed, 3 Jul 2024 10:10:04 -0500 Subject: [PATCH 18/39] fix --- examples/monotonic/mono_st.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/monotonic/mono_st.rs b/examples/monotonic/mono_st.rs index 3d2e32d2..559be11a 100644 --- a/examples/monotonic/mono_st.rs +++ b/examples/monotonic/mono_st.rs @@ -61,8 +61,8 @@ fn main() { a[rmq.query(le[idx_min].wrapping_add(1)..ri[idx_min])], a[idx_min] ); - if let Some(idx) = le[idx_min] { - assert!(a[idx] < a[idx_min]); + if le[idx_min] != usize::MAX { + assert!(a[le[idx_min]] < a[idx_min]); } if ri[idx_min] < n { assert!(a[ri[idx_min]] < a[idx_min]); From 8b1614a9c8f9863e017a7dd4b6ec912ffcfc84d5 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Wed, 3 Jul 2024 10:12:49 -0500 Subject: [PATCH 19/39] change style --- examples/monotonic/mono_st.rs | 4 ++-- src/monotonic/mono_range.rs | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/examples/monotonic/mono_st.rs b/examples/monotonic/mono_st.rs index 559be11a..760769b2 100644 --- a/examples/monotonic/mono_st.rs +++ b/examples/monotonic/mono_st.rs @@ -27,9 +27,9 @@ fn main() { { let mut iterations = 0; - for i in 1..n { + for (i, &num) in le.iter().enumerate().skip(1) { let mut j = i - 1; - while j != le[i] { + while j != num { iterations += 1; //TODO: change these to asserts //assert!(a[rmq.query(le[j - 1]..j)] >= a[j]); diff --git a/src/monotonic/mono_range.rs b/src/monotonic/mono_range.rs index 84bebc34..25d8333c 100644 --- a/src/monotonic/mono_range.rs +++ b/src/monotonic/mono_range.rs @@ -1,8 +1,9 @@ pub fn mono_range(le: &[usize]) -> Vec { - let mut ri = vec![le.len(); le.len()]; - for (i, &num) in le.iter().enumerate().skip(1) { + let n = le.len(); + let mut ri = vec![n; n]; + for i in 1..n { let mut j = i - 1; - while j != num { + while j != le[i] { ri[j] = i; j = le[j]; } From 35a88ad2d56b4239b9638281ec35921406ee8235 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Wed, 3 Jul 2024 10:14:24 -0500 Subject: [PATCH 20/39] nit --- src/monotonic/mono_range.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/monotonic/mono_range.rs b/src/monotonic/mono_range.rs index 25d8333c..32ed0e5e 100644 --- a/src/monotonic/mono_range.rs +++ b/src/monotonic/mono_range.rs @@ -1,8 +1,8 @@ pub fn mono_range(le: &[usize]) -> Vec { let n = le.len(); let mut ri = vec![n; n]; - for i in 1..n { - let mut j = i - 1; + for i in 0..n { + let mut j = i.wrapping_sub(1); while j != le[i] { ri[j] = i; j = le[j]; From 7646a4a8f80249988d98697c12fdbc99e782ef9d Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Wed, 3 Jul 2024 10:15:21 -0500 Subject: [PATCH 21/39] change --- examples/monotonic/mono_st.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/monotonic/mono_st.rs b/examples/monotonic/mono_st.rs index 760769b2..b031bc7b 100644 --- a/examples/monotonic/mono_st.rs +++ b/examples/monotonic/mono_st.rs @@ -27,9 +27,9 @@ fn main() { { let mut iterations = 0; - for (i, &num) in le.iter().enumerate().skip(1) { - let mut j = i - 1; - while j != num { + for i in 0..n { + let mut j = i.wrapping_sub(1); + while j != le[i] { iterations += 1; //TODO: change these to asserts //assert!(a[rmq.query(le[j - 1]..j)] >= a[j]); @@ -41,7 +41,7 @@ fn main() { j = le[j]; } } - let mut j = n - 1; + let mut j = n.wrapping_sub(1); while j != usize::MAX { iterations += 1; j = le[j]; From 2dc1895ca72ed282e1a61fc5c8995202c5750366 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Wed, 3 Jul 2024 10:24:30 -0500 Subject: [PATCH 22/39] nits --- examples/monotonic/mono_st.rs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/examples/monotonic/mono_st.rs b/examples/monotonic/mono_st.rs index b031bc7b..56c51658 100644 --- a/examples/monotonic/mono_st.rs +++ b/examples/monotonic/mono_st.rs @@ -26,27 +26,30 @@ fn main() { let ri = mono_range(&le); { - let mut iterations = 0; + let mut count_index = vec![0; n]; + let mut do_asserts = |j: usize| { + count_index[j] += 1; + let range = le[j].wrapping_add(1)..j; + if !range.is_empty() { + assert!(a[rmq.query(range)] >= a[j]); + } + if le[j] != usize::MAX { + assert!(a[le[j]] < a[j]); + } + }; for i in 0..n { let mut j = i.wrapping_sub(1); while j != le[i] { - iterations += 1; - //TODO: change these to asserts - //assert!(a[rmq.query(le[j - 1]..j)] >= a[j]); - //if le[j - 1] > 0 { - //assert!(a[le[j - 1] - 1] <= a[j]); - //} - // !cmp(a[k], a[j]) is true for all k in [le[j - 1], j) - // cmp(a[le[j - 1] - 1], a[j]) is true + do_asserts(j); j = le[j]; } } let mut j = n.wrapping_sub(1); while j != usize::MAX { - iterations += 1; + do_asserts(j); j = le[j]; } - assert_eq!(iterations, n); + assert_eq!(count_index, vec![1; n]); } for _ in 0..q { From 0f85db91abdaaa90cb2b67c36704660a408e1d3e Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Wed, 3 Jul 2024 10:27:39 -0500 Subject: [PATCH 23/39] different style --- src/monotonic/mono_st.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/monotonic/mono_st.rs b/src/monotonic/mono_st.rs index 4c3a9805..e20cd9a2 100644 --- a/src/monotonic/mono_st.rs +++ b/src/monotonic/mono_st.rs @@ -33,10 +33,11 @@ /// - Time: O(n) /// - Space: O(n) pub fn mono_st bool>(a: &[T], cmp: F) -> Vec { - let mut le = vec![usize::MAX; a.len()]; - for (i, num) in a.iter().enumerate().skip(1) { - le[i] = i - 1; - while le[i] != usize::MAX && !cmp(&a[le[i]], num) { + let n = a.len(); + let mut le = vec![0; n]; + for i in 0..n { + le[i] = i.wrapping_sub(1); + while le[i] != usize::MAX && !cmp(&a[le[i]], &a[i]) { le[i] = le[le[i]]; } } From 460f3321b95f7cd373e7d7410e60ee4628cf5d8a Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Wed, 3 Jul 2024 10:28:19 -0500 Subject: [PATCH 24/39] golf --- src/monotonic/mono_st.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/monotonic/mono_st.rs b/src/monotonic/mono_st.rs index e20cd9a2..6fe5d2d6 100644 --- a/src/monotonic/mono_st.rs +++ b/src/monotonic/mono_st.rs @@ -33,9 +33,8 @@ /// - Time: O(n) /// - Space: O(n) pub fn mono_st bool>(a: &[T], cmp: F) -> Vec { - let n = a.len(); - let mut le = vec![0; n]; - for i in 0..n { + let mut le = vec![0; a.len()]; + for i in 0..a.len() { le[i] = i.wrapping_sub(1); while le[i] != usize::MAX && !cmp(&a[le[i]], &a[i]) { le[i] = le[le[i]]; From fc29bdfbf3b1e4ec3f738f16d18d2cae2dd6e863 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Wed, 3 Jul 2024 10:30:02 -0500 Subject: [PATCH 25/39] consistency with c++ PTC --- src/monotonic/mono_range.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/monotonic/mono_range.rs b/src/monotonic/mono_range.rs index 32ed0e5e..3ff9f81f 100644 --- a/src/monotonic/mono_range.rs +++ b/src/monotonic/mono_range.rs @@ -1,7 +1,6 @@ pub fn mono_range(le: &[usize]) -> Vec { - let n = le.len(); - let mut ri = vec![n; n]; - for i in 0..n { + let mut ri = vec![le.len(); le.len()]; + for i in 0..le.len() { let mut j = i.wrapping_sub(1); while j != le[i] { ri[j] = i; From 55b71bd8454ef92ef0772ecd29a73354cc291b42 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Wed, 3 Jul 2024 10:38:09 -0500 Subject: [PATCH 26/39] finish doc for mono st --- src/monotonic/mono_st.rs | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/src/monotonic/mono_st.rs b/src/monotonic/mono_st.rs index 6fe5d2d6..485cb080 100644 --- a/src/monotonic/mono_st.rs +++ b/src/monotonic/mono_st.rs @@ -5,28 +5,41 @@ /// use programming_team_code_rust::monotonic::mono_st::mono_st; /// /// let a: Vec = vec![3, 1, 2, 2]; -/// assert_eq!(mono_st(&a, |x, y| x.lt(y)), [0, 0, 2, 2]); -/// assert_eq!(mono_st(&a, |x, y| x.le(y)), [0, 0, 2, 3]); -/// assert_eq!(mono_st(&a, |x, y| x.gt(y)), [0, 1, 1, 1]); -/// assert_eq!(mono_st(&a, |x, y| x.ge(y)), [0, 1, 1, 3]); +/// let n = a.len(); +/// assert_eq!(mono_st(&a, |x, y| x.lt(y)), [usize::MAX, usize::MAX, 1, 1]); +/// assert_eq!(mono_st(&a, |x, y| x.le(y)), [usize::MAX, usize::MAX, 1, 2]); +/// assert_eq!(mono_st(&a, |x, y| x.gt(y)), [usize::MAX, 0, 0, 0]); +/// assert_eq!(mono_st(&a, |x, y| x.ge(y)), [usize::MAX, 0, 0, 2]); /// /// let le = mono_st(&a, |x, y| x.lt(y)); /// let mut iterations = 0; -/// for i in 0..a.len() { -/// let mut j = i; +/// for i in 0..n { +/// let mut j = i.wrapping_sub(1); /// while j != le[i] { /// iterations += 1; -/// // !cmp(a[k], a[j]) is true for all k in [le[j - 1], j) -/// // cmp(a[le[j - 1] - 1], a[j]) is true -/// j = le[j - 1]; +/// for k in le[j].wrapping_add(1)..j { +/// assert!(!a[k].lt(&a[j])); +/// } +/// if le[j] != usize::MAX { +/// assert!(a[le[j]].lt(&a[j])); +/// } +/// j = le[j]; /// } /// } -/// let mut j = n; -/// while le[i] != le[j] { +/// +/// // clear the stack at the end +/// let mut j = n.wrapping_sub(1); +/// while j != usize::MAX { /// iterations += 1; -/// j = le[j - 1]; +/// for k in le[j].wrapping_add(1)..j { +/// assert!(!a[k].lt(&a[j])); +/// } +/// if le[j] != usize::MAX { +/// assert!(a[le[j]].lt(&a[j])); +/// } +/// j = le[j]; /// } -/// assert_eq!(iterations, a.len()); +/// assert_eq!(iterations, n); /// ``` /// /// # Complexity From fd3b167d3fa15633de5d94fafea105f5a61298ce Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Wed, 3 Jul 2024 10:40:30 -0500 Subject: [PATCH 27/39] simplify test --- src/monotonic/mono_st.rs | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/src/monotonic/mono_st.rs b/src/monotonic/mono_st.rs index 485cb080..16aecd18 100644 --- a/src/monotonic/mono_st.rs +++ b/src/monotonic/mono_st.rs @@ -12,17 +12,11 @@ /// assert_eq!(mono_st(&a, |x, y| x.ge(y)), [usize::MAX, 0, 0, 2]); /// /// let le = mono_st(&a, |x, y| x.lt(y)); -/// let mut iterations = 0; +/// let mut seen_index = vec![0; n]; /// for i in 0..n { /// let mut j = i.wrapping_sub(1); /// while j != le[i] { -/// iterations += 1; -/// for k in le[j].wrapping_add(1)..j { -/// assert!(!a[k].lt(&a[j])); -/// } -/// if le[j] != usize::MAX { -/// assert!(a[le[j]].lt(&a[j])); -/// } +/// seen_index[j] += 1; /// j = le[j]; /// } /// } @@ -30,16 +24,10 @@ /// // clear the stack at the end /// let mut j = n.wrapping_sub(1); /// while j != usize::MAX { -/// iterations += 1; -/// for k in le[j].wrapping_add(1)..j { -/// assert!(!a[k].lt(&a[j])); -/// } -/// if le[j] != usize::MAX { -/// assert!(a[le[j]].lt(&a[j])); -/// } +/// seen_index[j] += 1; /// j = le[j]; /// } -/// assert_eq!(iterations, n); +/// assert_eq!(seen_index, vec![1; n]); /// ``` /// /// # Complexity From 00acb23b25839435865ddbdda357be2b67684107 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Wed, 3 Jul 2024 10:45:06 -0500 Subject: [PATCH 28/39] add --- src/monotonic/mono_st.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/monotonic/mono_st.rs b/src/monotonic/mono_st.rs index 16aecd18..26a00b6e 100644 --- a/src/monotonic/mono_st.rs +++ b/src/monotonic/mono_st.rs @@ -1,16 +1,20 @@ //! # Monotonic Stack +/// Gets vec le where le[i] = largest index such that le[i] < i && a[le[i]].cmp(&a[i]), or usize::MAX +/// /// # Example /// ``` /// use programming_team_code_rust::monotonic::mono_st::mono_st; /// /// let a: Vec = vec![3, 1, 2, 2]; /// let n = a.len(); +/// /// assert_eq!(mono_st(&a, |x, y| x.lt(y)), [usize::MAX, usize::MAX, 1, 1]); /// assert_eq!(mono_st(&a, |x, y| x.le(y)), [usize::MAX, usize::MAX, 1, 2]); /// assert_eq!(mono_st(&a, |x, y| x.gt(y)), [usize::MAX, 0, 0, 0]); /// assert_eq!(mono_st(&a, |x, y| x.ge(y)), [usize::MAX, 0, 0, 2]); /// +/// // simulate popping off stack for single index /// let le = mono_st(&a, |x, y| x.lt(y)); /// let mut seen_index = vec![0; n]; /// for i in 0..n { From cbb0b8284110ff1377a41c5e84e7f25228aabf1e Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Wed, 3 Jul 2024 10:50:50 -0500 Subject: [PATCH 29/39] add docs for mono range --- src/monotonic/mono_range.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/monotonic/mono_range.rs b/src/monotonic/mono_range.rs index 3ff9f81f..fbe7d0a1 100644 --- a/src/monotonic/mono_range.rs +++ b/src/monotonic/mono_range.rs @@ -1,3 +1,22 @@ +//! # Monotonic Range + +/// Gets vec ri where ri[i] = smallese index such that i < ri[i] && !a[i].cmp(&a[ri[i]]), or n +/// +/// # Example +/// ``` +/// use programming_team_code_rust::monotonic::mono_st::mono_st; +/// use programming_team_code_rust::monotonic::mono_range::mono_range; +/// +/// let a: Vec = vec![3, 1, 2, 2]; +/// +/// let le = mono_st(&a, |x, y| x.lt(y)); +/// let ri = mono_range(&le); +/// assert_eq!(ri, [1, 4, 3, 4]); +/// ``` +/// +/// # Complexity +/// - Time: O(n) +/// - Space: O(n) pub fn mono_range(le: &[usize]) -> Vec { let mut ri = vec![le.len(); le.len()]; for i in 0..le.len() { From 5f49d8c559e277821263d755db2acecf5161d55d Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Wed, 3 Jul 2024 10:51:09 -0500 Subject: [PATCH 30/39] fix typo --- src/monotonic/mono_range.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/monotonic/mono_range.rs b/src/monotonic/mono_range.rs index fbe7d0a1..b416ba2d 100644 --- a/src/monotonic/mono_range.rs +++ b/src/monotonic/mono_range.rs @@ -1,6 +1,6 @@ //! # Monotonic Range -/// Gets vec ri where ri[i] = smallese index such that i < ri[i] && !a[i].cmp(&a[ri[i]]), or n +/// Gets vec ri where ri[i] = smallest index such that i < ri[i] && !a[i].cmp(&a[ri[i]]), or n /// /// # Example /// ``` From 016e02e4177f259748da368c95b4d6875b066f6f Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Wed, 3 Jul 2024 10:55:14 -0500 Subject: [PATCH 31/39] nit --- src/monotonic/mono_st.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/monotonic/mono_st.rs b/src/monotonic/mono_st.rs index 26a00b6e..78efffe9 100644 --- a/src/monotonic/mono_st.rs +++ b/src/monotonic/mono_st.rs @@ -31,6 +31,7 @@ /// seen_index[j] += 1; /// j = le[j]; /// } +/// /// assert_eq!(seen_index, vec![1; n]); /// ``` /// From c97712afdece3ad18f3b2da37e73d5d18f2b4017 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Wed, 3 Jul 2024 10:57:36 -0500 Subject: [PATCH 32/39] fix --- src/monotonic/mono_range.rs | 2 +- src/monotonic/mono_st.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/monotonic/mono_range.rs b/src/monotonic/mono_range.rs index b416ba2d..6243315b 100644 --- a/src/monotonic/mono_range.rs +++ b/src/monotonic/mono_range.rs @@ -1,6 +1,6 @@ //! # Monotonic Range -/// Gets vec ri where ri[i] = smallest index such that i < ri[i] && !a[i].cmp(&a[ri[i]]), or n +/// Gets vec ri where ri\[i\] = smallest index such that i < ri\[i\] && !a\[i\].cmp(&a\[ri\[i\]\]), or n /// /// # Example /// ``` diff --git a/src/monotonic/mono_st.rs b/src/monotonic/mono_st.rs index 78efffe9..94bdc350 100644 --- a/src/monotonic/mono_st.rs +++ b/src/monotonic/mono_st.rs @@ -1,6 +1,6 @@ //! # Monotonic Stack -/// Gets vec le where le[i] = largest index such that le[i] < i && a[le[i]].cmp(&a[i]), or usize::MAX +/// Gets vec le where le\[i\] = largest index such that le\[i\] < i && a\[le\[i\]\].cmp(&a\[i\]), or usize::MAX /// /// # Example /// ``` From 54936fb3bd74d0c4443989802d244a235b65d264 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Thu, 4 Jul 2024 09:51:05 -0500 Subject: [PATCH 33/39] nit to docs --- src/monotonic/mono_st.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/monotonic/mono_st.rs b/src/monotonic/mono_st.rs index 94bdc350..0524eb26 100644 --- a/src/monotonic/mono_st.rs +++ b/src/monotonic/mono_st.rs @@ -20,6 +20,7 @@ /// for i in 0..n { /// let mut j = i.wrapping_sub(1); /// while j != le[i] { +/// let range = le[j].wrapping_add(1)..j; // for all indexes k in range: !cmp(&a[k], &a[j]) /// seen_index[j] += 1; /// j = le[j]; /// } From 62e4fc6bee02a449c018d1278f5acf9fe8f6111c Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Thu, 4 Jul 2024 10:22:06 -0500 Subject: [PATCH 34/39] now ACs --- examples/monotonic/mono_st.rs | 87 ++++++++++++++++++++--------------- 1 file changed, 49 insertions(+), 38 deletions(-) diff --git a/examples/monotonic/mono_st.rs b/examples/monotonic/mono_st.rs index 56c51658..75c3c845 100644 --- a/examples/monotonic/mono_st.rs +++ b/examples/monotonic/mono_st.rs @@ -12,42 +12,52 @@ fn main() { a: [u32; n], } - let rmq = RMQ::new(&(0..n).collect::>(), |i1, i2| { - if a[i1] < a[i2] { - i1 - } else { - i2 - } - }); + let rmq = RMQ::new( + &(0..n).map(|i| (i, i)).collect::>(), + |(min_i1, max_i1), (min_i2, max_i2)| { + let min_idx = if a[min_i1] < a[min_i2] { + min_i1 + } else { + min_i2 + }; + let max_idx = if a[max_i1] > a[max_i2] { + max_i1 + } else { + max_i2 + }; + (min_idx, max_idx) + }, + ); + + let compares = vec![ + |x: u32, y: u32| -> bool { x.le(&y) }, + |x: u32, y: u32| -> bool { x.lt(&y) }, + |x: u32, y: u32| -> bool { x.ge(&y) }, + |x: u32, y: u32| -> bool { x.gt(&y) }, + ]; - let le = mono_st(&a, |x, y| x.le(y)); - assert_eq!(le, mono_st(&a, |x, y| x.lt(y))); //TODO wait till lib checker PR merges and watch - //this fail - let ri = mono_range(&le); + let mut le = Vec::new(); + let mut ri = Vec::new(); + for &cmp in &compares { + le.push(mono_st(&a, |&x, &y| cmp(x, y))); + ri.push(mono_range(le.last().unwrap())); + } + let le = le; + let ri = ri; - { + for curr_le in &le { let mut count_index = vec![0; n]; - let mut do_asserts = |j: usize| { - count_index[j] += 1; - let range = le[j].wrapping_add(1)..j; - if !range.is_empty() { - assert!(a[rmq.query(range)] >= a[j]); - } - if le[j] != usize::MAX { - assert!(a[le[j]] < a[j]); - } - }; for i in 0..n { let mut j = i.wrapping_sub(1); - while j != le[i] { - do_asserts(j); - j = le[j]; + while j != curr_le[i] { + count_index[j] += 1; + j = curr_le[j]; } } let mut j = n.wrapping_sub(1); while j != usize::MAX { - do_asserts(j); - j = le[j]; + count_index[j] += 1; + j = curr_le[j]; } assert_eq!(count_index, vec![1; n]); } @@ -58,19 +68,20 @@ fn main() { r: usize, } - let idx_min = rmq.query(l..r); + let (min_idx, max_idx) = rmq.query(l..r); - assert_eq!( - a[rmq.query(le[idx_min].wrapping_add(1)..ri[idx_min])], - a[idx_min] - ); - if le[idx_min] != usize::MAX { - assert!(a[le[idx_min]] < a[idx_min]); - } - if ri[idx_min] < n { - assert!(a[ri[idx_min]] < a[idx_min]); + for i in 0..4 { + let idx = if i < 2 { min_idx } else { max_idx }; + let rmq_res = rmq.query(le[i][idx].wrapping_add(1)..ri[i][idx]); + assert_eq!(a[if i < 2 { rmq_res.0 } else { rmq_res.1 }], a[idx]); + if le[i][idx] != usize::MAX { + assert!(compares[i](a[le[i][idx]], a[idx])); + } + if ri[i][idx] < n { + assert!(!compares[i](a[idx], a[ri[i][idx]])); + } } - println!("{}", a[idx_min]); + println!("{}", a[min_idx]); } } From 2b56c9d35ca7d1a1227f75b6222fd3c40b7b964b Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Fri, 5 Jul 2024 22:20:13 -0500 Subject: [PATCH 35/39] virt tree --- src/graphs/hld.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/graphs/hld.rs b/src/graphs/hld.rs index 076a60d8..8743542f 100644 --- a/src/graphs/hld.rs +++ b/src/graphs/hld.rs @@ -1,6 +1,7 @@ //! # Heavy Light Decomposition use crate::graphs::dfs_order::{get_dfs_postorder, get_dfs_preorder}; +use crate::monotonic::mono_st::mono_st; use std::ops::Range; /// # Example @@ -27,6 +28,10 @@ use std::ops::Range; /// let mut sum = 0; /// hld.path(1, 2, |range, _| sum += fenwick.sum(range)); /// assert_eq!(sum, 111); +/// +/// let (par, to_node) = hld.virt_tree(vec![1, 3]); +/// assert_eq!(par, [usize::MAX, 0, 0]); +/// assert_eq!(to_node, [0, 3, 1]); /// ``` pub struct HLD { /// parent @@ -200,4 +205,23 @@ impl HLD { None } } + + /// # Virtual Tree + /// + /// - see + /// + /// # Complexity + /// - k = subset.len() + /// - Time: O((k log k) + (k log n)) + /// - Space: O(k) + pub fn virt_tree(&self, mut subset: Vec) -> (Vec, Vec) { + subset.sort_by(|&a, &b| self.tin[a].cmp(&self.tin[b])); + let siz = subset.len(); + for i in 1..siz { + subset.push(self.lca(subset[i - 1], subset[i])); + } + subset.sort_by(|&a, &b| self.tin[a].cmp(&self.tin[b])); + subset.dedup(); + (mono_st(&subset, |&x, &y| self.in_sub(x, y)), subset) + } } From 438c6f7378ae6fcbc7ad3ca5dfec6946ad82a92b Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Fri, 5 Jul 2024 22:42:04 -0500 Subject: [PATCH 36/39] finish docs --- src/graphs/hld.rs | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/src/graphs/hld.rs b/src/graphs/hld.rs index 8743542f..d2768fd0 100644 --- a/src/graphs/hld.rs +++ b/src/graphs/hld.rs @@ -28,10 +28,6 @@ use std::ops::Range; /// let mut sum = 0; /// hld.path(1, 2, |range, _| sum += fenwick.sum(range)); /// assert_eq!(sum, 111); -/// -/// let (par, to_node) = hld.virt_tree(vec![1, 3]); -/// assert_eq!(par, [usize::MAX, 0, 0]); -/// assert_eq!(to_node, [0, 3, 1]); /// ``` pub struct HLD { /// parent @@ -206,22 +202,41 @@ impl HLD { } } - /// # Virtual Tree + /// # Auxiliary Tree /// /// - see /// + /// # Example + /// ``` + /// use programming_team_code_rust::graphs::hld::HLD; + /// + /// let n = 5; + /// let mut adj = vec![vec![]; n]; + /// for (u, v) in [(0,1), (1,2), (2,3), (2,4)] { + /// adj[u].push(v); + /// adj[v].push(u); + /// } + /// + /// let hld = HLD::new(&mut adj, false); + /// + /// let (par, to_node) = hld.aux_tree(vec![0, 3, 4]); + /// // 0, 1, .., par.len()-1 is a topological/dfs order of aux tree + /// assert_eq!(par, [usize::MAX, 0, 1, 1]); + /// assert_eq!(to_node, [0, 2, 3, 4]); + /// ``` + /// /// # Complexity - /// - k = subset.len() + /// - k = nodes.len() /// - Time: O((k log k) + (k log n)) /// - Space: O(k) - pub fn virt_tree(&self, mut subset: Vec) -> (Vec, Vec) { - subset.sort_by(|&a, &b| self.tin[a].cmp(&self.tin[b])); - let siz = subset.len(); + pub fn aux_tree(&self, mut nodes: Vec) -> (Vec, Vec) { + nodes.sort_by(|&a, &b| self.tin[a].cmp(&self.tin[b])); + let siz = nodes.len(); for i in 1..siz { - subset.push(self.lca(subset[i - 1], subset[i])); + nodes.push(self.lca(nodes[i - 1], nodes[i])); } - subset.sort_by(|&a, &b| self.tin[a].cmp(&self.tin[b])); - subset.dedup(); - (mono_st(&subset, |&x, &y| self.in_sub(x, y)), subset) + nodes.sort_by(|&a, &b| self.tin[a].cmp(&self.tin[b])); + nodes.dedup(); + (mono_st(&nodes, |&x, &y| self.in_sub(x, y)), nodes) } } From 3a1fb2784ab250b16031d96e15bf730cdcc94fed Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Fri, 5 Jul 2024 23:15:28 -0500 Subject: [PATCH 37/39] finished test --- Cargo.toml | 4 ++ examples/graphs/hld_aux_tree.rs | 79 +++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 examples/graphs/hld_aux_tree.rs diff --git a/Cargo.toml b/Cargo.toml index 2d16723f..138558cc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -207,3 +207,7 @@ path = "examples/helpers/lis_pop.rs" [[example]] name = "mono_st" path = "examples/monotonic/mono_st.rs" + +[[example]] +name = "hld_aux_tree" +path = "examples/graphs/hld_aux_tree.rs" diff --git a/examples/graphs/hld_aux_tree.rs b/examples/graphs/hld_aux_tree.rs new file mode 100644 index 00000000..8dd1b829 --- /dev/null +++ b/examples/graphs/hld_aux_tree.rs @@ -0,0 +1,79 @@ +// verification-helper: PROBLEM https://onlinejudge.u-aizu.ac.jp/problems/GRL_5_B + +use proconio::input; +use programming_team_code_rust::graphs::hld::HLD; +use std::collections::VecDeque; + +fn main() { + input! { + n: usize, + edges: [(usize, usize, u32); n - 1], + } + + let mut adj_weighted = vec![vec![]; n]; + let mut adj = vec![vec![]; n]; + for &(u, v, w) in &edges { + adj[u].push(v); + adj[v].push(u); + adj_weighted[u].push((v, w)); + adj_weighted[v].push((u, w)); + } + let adj_weighted = adj_weighted; + + let mut dist = vec![0; n]; + { + fn dfs(u: usize, p: Option, adj_weighted: &[Vec<(usize, u32)>], dist: &mut [u32]) { + for &(v, w) in &adj_weighted[u] { + if Some(v) == p { + continue; + } + dist[v] = w + dist[u]; + dfs(v, Some(u), adj_weighted, dist); + } + } + dfs(0, None, &adj_weighted, &mut dist); + } + + let hld = HLD::new(&mut adj, true); + + let weighted_dist = |u: usize, v: usize| -> u32 { + let lc = hld.lca(u, v); + dist[u] + dist[v] - 2 * dist[lc] + }; + + let mut diam_u = 0; + for i in 1..n { + if weighted_dist(0, i) > weighted_dist(0, diam_u) { + diam_u = i; + } + } + let mut diam_v = 0; + for i in 1..n { + if weighted_dist(diam_u, i) > weighted_dist(diam_u, diam_v) { + diam_v = i; + } + } + + for u in 0..n { + let (par, to_node) = hld.aux_tree(vec![diam_u, diam_v, u]); + let mut aux_adj = vec![vec![]; par.len()]; + for i in 1..par.len() { + let edge_w = dist[to_node[i]] - dist[to_node[par[i]]]; + aux_adj[i].push((par[i], edge_w)); + aux_adj[par[i]].push((i, edge_w)); + } + let mut q = VecDeque::new(); + q.push_back((to_node.iter().position(|&x| x == u).unwrap(), None, 0)); + let mut res = 0; + while let Some((node, parent, curr_dist)) = q.pop_front() { + res = res.max(curr_dist); + for &(v, w) in &aux_adj[node] { + if Some(v) == parent { + continue; + } + q.push_back((v, Some(node), curr_dist + w)); + } + } + println!("{}", res); + } +} From ff31ec14da42b22ec6fe78cda49099e449b81d56 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Fri, 5 Jul 2024 23:34:47 -0500 Subject: [PATCH 38/39] make dist const --- examples/graphs/hld_aux_tree.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/graphs/hld_aux_tree.rs b/examples/graphs/hld_aux_tree.rs index 8dd1b829..deed9fa9 100644 --- a/examples/graphs/hld_aux_tree.rs +++ b/examples/graphs/hld_aux_tree.rs @@ -33,6 +33,7 @@ fn main() { } dfs(0, None, &adj_weighted, &mut dist); } + let dist = dist; let hld = HLD::new(&mut adj, true); From 38343f05d79bdb513f022ad14503baa546eaf734 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Sun, 7 Jul 2024 09:08:09 -0500 Subject: [PATCH 39/39] consistent var names --- src/graphs/hld.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/graphs/hld.rs b/src/graphs/hld.rs index d2768fd0..bca5869b 100644 --- a/src/graphs/hld.rs +++ b/src/graphs/hld.rs @@ -230,13 +230,13 @@ impl HLD { /// - Time: O((k log k) + (k log n)) /// - Space: O(k) pub fn aux_tree(&self, mut nodes: Vec) -> (Vec, Vec) { - nodes.sort_by(|&a, &b| self.tin[a].cmp(&self.tin[b])); + nodes.sort_by(|&u, &v| self.tin[u].cmp(&self.tin[v])); let siz = nodes.len(); for i in 1..siz { nodes.push(self.lca(nodes[i - 1], nodes[i])); } - nodes.sort_by(|&a, &b| self.tin[a].cmp(&self.tin[b])); + nodes.sort_by(|&u, &v| self.tin[u].cmp(&self.tin[v])); nodes.dedup(); - (mono_st(&nodes, |&x, &y| self.in_sub(x, y)), nodes) + (mono_st(&nodes, |&u, &v| self.in_sub(u, v)), nodes) } }