-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0307_range_sum_query_mutable.rs
More file actions
87 lines (74 loc) · 1.82 KB
/
s0307_range_sum_query_mutable.rs
File metadata and controls
87 lines (74 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#![allow(unused)]
struct NumArray {
tree: Vec<i32>,
n: usize,
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl NumArray {
fn new(nums: Vec<i32>) -> Self {
if nums.len() > 0 {
let n = nums.len();
let mut tree = vec![0; n * 2];
for i in n..2 * n {
tree[i] = nums[i - n];
}
for i in (0..n).rev() {
tree[i] = tree[i * 2] + tree[i * 2 + 1];
}
return Self {
tree,
n: nums.len(),
};
}
Self {
tree: Vec::new(),
n: 0,
}
}
fn update(&mut self, i: i32, val: i32) {
let mut i = i as usize;
i += self.n;
self.tree[i] = val;
while i > 0 {
let (mut left, mut right) = (i, i);
if i % 2 == 0 {
right = i + 1;
} else {
left = i - 1;
}
self.tree[i / 2] = self.tree[left] + self.tree[right];
i /= 2;
}
}
fn sum_range(&self, i: i32, j: i32) -> i32 {
let (mut l, mut r, mut sum) = (i as usize + self.n, j as usize + self.n, 0);
while l <= r {
if (l % 2) == 1 {
sum += self.tree[l];
l += 1;
}
if (r % 2) == 0 {
sum += self.tree[r];
r -= 1;
}
l /= 2;
r /= 2;
}
sum
}
}
/**
* Your NumArray object will be instantiated and called as such:
* let obj = NumArray::new(nums);
* obj.update(i, val);
* let ret_2: i32 = obj.sum_range(i, j);
*/
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_346() {}
}