pub struct SegTree<Spec: SegTreeSpec> { /* private fields */ }
Expand description
A generic Segment Tree data structure.
See SegTree for a detailed explanation and examples.
Implementations§
Source§impl<Spec: SegTreeSpec> SegTree<Spec>
impl<Spec: SegTreeSpec> SegTree<Spec>
Sourcepub fn new(size: usize) -> Self
pub fn new(size: usize) -> Self
Creates a new SegTree
of a given size
, initialized with identity elements.
The internal size of the tree will be the smallest power of two
greater than or equal to size
.
Time complexity: O(n)
where n is the smallest power of two >= size
.
Sourcepub fn from_vec(values: &[Spec::T]) -> Self
pub fn from_vec(values: &[Spec::T]) -> Self
Creates a new SegTree
from a vector of initial values.
The tree is built in O(n)
time, which is more efficient than creating an
empty tree and updating each element individually.
Time complexity: O(n)
where n is the length of values
.
Examples found in repository?
36fn custom_sum_example() {
37 println!("1. Custom SegTree for Sum Operations");
38 println!("-----------------------------------");
39
40 // Define a custom spec for sum operations
41 struct SumSpec;
42
43 impl SegTreeSpec for SumSpec {
44 type T = i64;
45 const ID: Self::T = 0; // Identity element for addition
46
47 fn op(a: &Self::T, b: &Self::T) -> Self::T {
48 a + b
49 }
50 }
51
52 let values = vec![1, 2, 3, 4, 5];
53 let mut seg_tree = SegTree::<SumSpec>::from_vec(&values);
54
55 println!("Initial values: {:?}", values);
56 println!("Sum of range [1, 4): {}", seg_tree.query(1, 4)); // 2 + 3 + 4 = 9
57 println!("Sum of entire array [0, 5): {}", seg_tree.query(0, 5)); // 15
58
59 // Update element at index 2 from 3 to 10
60 seg_tree.update(2, 10);
61 println!("\nAfter updating index 2 to 10:");
62 println!("Sum of range [1, 4): {}", seg_tree.query(1, 4)); // 2 + 10 + 4 = 16
63 println!("Sum of entire array [0, 5): {}", seg_tree.query(0, 5)); // 22
64 println!();
65}
66
67/// Example 2: Using helper types for common operations
68fn helper_types_example() {
69 println!("2. Helper Types for Common Operations");
70 println!("------------------------------------");
71
72 let values = vec![3, 1, 4, 1, 5, 9, 2, 6, 5, 3];
73 println!("Values: {:?}", values);
74
75 // Sum operations
76 let mut sum_tree = SegTreeSum::<i32>::from_vec(&values);
77 println!("Range sum [2, 6): {}", sum_tree.query(2, 6)); // 4 + 1 + 5 + 9 = 19
78
79 // Min operations
80 let mut min_tree = SegTreeMin::<i32>::from_vec(&values);
81 println!("Range min [2, 6): {}", min_tree.query(2, 6)); // min(4, 1, 5, 9) = 1
82
83 // Max operations
84 let mut max_tree = SegTreeMax::<i32>::from_vec(&values);
85 println!("Range max [2, 6): {}", max_tree.query(2, 6)); // max(4, 1, 5, 9) = 9
86
87 // Demonstrate updates
88 println!("\nAfter updating index 3 to 20:");
89 sum_tree.update(3, 20);
90 min_tree.update(3, 20);
91 max_tree.update(3, 20);
92
93 println!("Range sum [2, 6): {}", sum_tree.query(2, 6)); // 4 + 20 + 5 + 9 = 38
94 println!("Range min [2, 6): {}", min_tree.query(2, 6)); // min(4, 20, 5, 9) = 4
95 println!("Range max [2, 6): {}", max_tree.query(2, 6)); // max(4, 20, 5, 9) = 20
96 println!();
97}
98
99/// Example 3: Custom min operation with detailed explanation
100fn custom_min_example() {
101 println!("3. Custom Min Operation with Details");
102 println!("-----------------------------------");
103
104 struct MinSpec;
105
106 impl SegTreeSpec for MinSpec {
107 type T = i32;
108 const ID: Self::T = i32::MAX; // Identity for min is maximum possible value
109
110 fn op(a: &Self::T, b: &Self::T) -> Self::T {
111 (*a).min(*b)
112 }
113 }
114
115 let values = vec![7, 3, 9, 1, 6, 2, 8, 4];
116 let seg_tree = SegTree::<MinSpec>::from_vec(&values);
117
118 println!("Values: {:?}", values);
119 println!("Min of entire array: {}", seg_tree.query(0, 8)); // 1
120 println!("Min of [2, 5): {}", seg_tree.query(2, 5)); // min(9, 1, 6) = 1
121 println!("Min of [0, 3): {}", seg_tree.query(0, 3)); // min(7, 3, 9) = 3
122 println!("Min of [5, 8): {}", seg_tree.query(5, 8)); // min(2, 8, 4) = 2
123 println!();
124}
Sourcepub fn update(&mut self, index: usize, value: Spec::T)
pub fn update(&mut self, index: usize, value: Spec::T)
Performs a point update, replacing the value at index
with a new value
.
After updating the leaf node, the change is propagated up the tree by recalculating the parent nodes.
Time complexity: O(log n)
.
§Panics
Panics if index
is out of bounds (index >= self.size
).
Examples found in repository?
36fn custom_sum_example() {
37 println!("1. Custom SegTree for Sum Operations");
38 println!("-----------------------------------");
39
40 // Define a custom spec for sum operations
41 struct SumSpec;
42
43 impl SegTreeSpec for SumSpec {
44 type T = i64;
45 const ID: Self::T = 0; // Identity element for addition
46
47 fn op(a: &Self::T, b: &Self::T) -> Self::T {
48 a + b
49 }
50 }
51
52 let values = vec![1, 2, 3, 4, 5];
53 let mut seg_tree = SegTree::<SumSpec>::from_vec(&values);
54
55 println!("Initial values: {:?}", values);
56 println!("Sum of range [1, 4): {}", seg_tree.query(1, 4)); // 2 + 3 + 4 = 9
57 println!("Sum of entire array [0, 5): {}", seg_tree.query(0, 5)); // 15
58
59 // Update element at index 2 from 3 to 10
60 seg_tree.update(2, 10);
61 println!("\nAfter updating index 2 to 10:");
62 println!("Sum of range [1, 4): {}", seg_tree.query(1, 4)); // 2 + 10 + 4 = 16
63 println!("Sum of entire array [0, 5): {}", seg_tree.query(0, 5)); // 22
64 println!();
65}
66
67/// Example 2: Using helper types for common operations
68fn helper_types_example() {
69 println!("2. Helper Types for Common Operations");
70 println!("------------------------------------");
71
72 let values = vec![3, 1, 4, 1, 5, 9, 2, 6, 5, 3];
73 println!("Values: {:?}", values);
74
75 // Sum operations
76 let mut sum_tree = SegTreeSum::<i32>::from_vec(&values);
77 println!("Range sum [2, 6): {}", sum_tree.query(2, 6)); // 4 + 1 + 5 + 9 = 19
78
79 // Min operations
80 let mut min_tree = SegTreeMin::<i32>::from_vec(&values);
81 println!("Range min [2, 6): {}", min_tree.query(2, 6)); // min(4, 1, 5, 9) = 1
82
83 // Max operations
84 let mut max_tree = SegTreeMax::<i32>::from_vec(&values);
85 println!("Range max [2, 6): {}", max_tree.query(2, 6)); // max(4, 1, 5, 9) = 9
86
87 // Demonstrate updates
88 println!("\nAfter updating index 3 to 20:");
89 sum_tree.update(3, 20);
90 min_tree.update(3, 20);
91 max_tree.update(3, 20);
92
93 println!("Range sum [2, 6): {}", sum_tree.query(2, 6)); // 4 + 20 + 5 + 9 = 38
94 println!("Range min [2, 6): {}", min_tree.query(2, 6)); // min(4, 20, 5, 9) = 4
95 println!("Range max [2, 6): {}", max_tree.query(2, 6)); // max(4, 20, 5, 9) = 20
96 println!();
97}
Sourcepub fn query(&self, left: usize, right: usize) -> Spec::T
pub fn query(&self, left: usize, right: usize) -> Spec::T
Queries the segment tree for the aggregated value in the range [left, right)
.
The range is half-open, including left
and excluding right
.
Time complexity: O(log n)
.
§Panics
This function panics if left > right
or if right
is out of the bounds
of the original array size (right > self.size
).
Examples found in repository?
36fn custom_sum_example() {
37 println!("1. Custom SegTree for Sum Operations");
38 println!("-----------------------------------");
39
40 // Define a custom spec for sum operations
41 struct SumSpec;
42
43 impl SegTreeSpec for SumSpec {
44 type T = i64;
45 const ID: Self::T = 0; // Identity element for addition
46
47 fn op(a: &Self::T, b: &Self::T) -> Self::T {
48 a + b
49 }
50 }
51
52 let values = vec![1, 2, 3, 4, 5];
53 let mut seg_tree = SegTree::<SumSpec>::from_vec(&values);
54
55 println!("Initial values: {:?}", values);
56 println!("Sum of range [1, 4): {}", seg_tree.query(1, 4)); // 2 + 3 + 4 = 9
57 println!("Sum of entire array [0, 5): {}", seg_tree.query(0, 5)); // 15
58
59 // Update element at index 2 from 3 to 10
60 seg_tree.update(2, 10);
61 println!("\nAfter updating index 2 to 10:");
62 println!("Sum of range [1, 4): {}", seg_tree.query(1, 4)); // 2 + 10 + 4 = 16
63 println!("Sum of entire array [0, 5): {}", seg_tree.query(0, 5)); // 22
64 println!();
65}
66
67/// Example 2: Using helper types for common operations
68fn helper_types_example() {
69 println!("2. Helper Types for Common Operations");
70 println!("------------------------------------");
71
72 let values = vec![3, 1, 4, 1, 5, 9, 2, 6, 5, 3];
73 println!("Values: {:?}", values);
74
75 // Sum operations
76 let mut sum_tree = SegTreeSum::<i32>::from_vec(&values);
77 println!("Range sum [2, 6): {}", sum_tree.query(2, 6)); // 4 + 1 + 5 + 9 = 19
78
79 // Min operations
80 let mut min_tree = SegTreeMin::<i32>::from_vec(&values);
81 println!("Range min [2, 6): {}", min_tree.query(2, 6)); // min(4, 1, 5, 9) = 1
82
83 // Max operations
84 let mut max_tree = SegTreeMax::<i32>::from_vec(&values);
85 println!("Range max [2, 6): {}", max_tree.query(2, 6)); // max(4, 1, 5, 9) = 9
86
87 // Demonstrate updates
88 println!("\nAfter updating index 3 to 20:");
89 sum_tree.update(3, 20);
90 min_tree.update(3, 20);
91 max_tree.update(3, 20);
92
93 println!("Range sum [2, 6): {}", sum_tree.query(2, 6)); // 4 + 20 + 5 + 9 = 38
94 println!("Range min [2, 6): {}", min_tree.query(2, 6)); // min(4, 20, 5, 9) = 4
95 println!("Range max [2, 6): {}", max_tree.query(2, 6)); // max(4, 20, 5, 9) = 20
96 println!();
97}
98
99/// Example 3: Custom min operation with detailed explanation
100fn custom_min_example() {
101 println!("3. Custom Min Operation with Details");
102 println!("-----------------------------------");
103
104 struct MinSpec;
105
106 impl SegTreeSpec for MinSpec {
107 type T = i32;
108 const ID: Self::T = i32::MAX; // Identity for min is maximum possible value
109
110 fn op(a: &Self::T, b: &Self::T) -> Self::T {
111 (*a).min(*b)
112 }
113 }
114
115 let values = vec![7, 3, 9, 1, 6, 2, 8, 4];
116 let seg_tree = SegTree::<MinSpec>::from_vec(&values);
117
118 println!("Values: {:?}", values);
119 println!("Min of entire array: {}", seg_tree.query(0, 8)); // 1
120 println!("Min of [2, 5): {}", seg_tree.query(2, 5)); // min(9, 1, 6) = 1
121 println!("Min of [0, 3): {}", seg_tree.query(0, 3)); // min(7, 3, 9) = 3
122 println!("Min of [5, 8): {}", seg_tree.query(5, 8)); // min(2, 8, 4) = 2
123 println!();
124}