forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cs
53 lines (45 loc) · 1.12 KB
/
Solution.cs
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
class BinaryIndexedTree {
private int n;
private int[] c;
public BinaryIndexedTree(int n) {
this.n = n;
c = new int[n + 1];
}
public void Update(int x, int delta) {
while (x <= n) {
c[x] += delta;
x += x & -x;
}
}
public int Query(int x) {
int s = 0;
while (x > 0) {
s += c[x];
x -= x & -x;
}
return s;
}
}
public class NumArray {
private BinaryIndexedTree tree;
public NumArray(int[] nums) {
int n = nums.Length;
tree = new BinaryIndexedTree(n);
for (int i = 0; i < n; ++i) {
tree.Update(i + 1, nums[i]);
}
}
public void Update(int index, int val) {
int prev = SumRange(index, index);
tree.Update(index + 1, val - prev);
}
public int SumRange(int left, int right) {
return tree.Query(right + 1) - tree.Query(left);
}
}
/**
* Your NumArray object will be instantiated and called as such:
* NumArray obj = new NumArray(nums);
* obj.Update(index,val);
* int param_2 = obj.SumRange(left,right);
*/