307. 区域和检索 - 数组可修改


给你一个数组 nums ,请你完成两类查询。

  1. 其中一类查询要求 更新 数组 nums 下标对应的值
  2. 另一类查询要求返回数组 nums 中索引 left 和索引 right 之间( 包含 )的nums元素的 ,其中 left <= right

实现 NumArray 类:

  • NumArray(int[] nums) 用整数数组 nums 初始化对象
  • void update(int index, int val)nums[index] 的值 更新val
  • int sumRange(int left, int right) 返回数组 nums 中索引 left 和索引 right 之间( 包含 )的nums元素的 (即,nums[left] + nums[left + 1], ..., nums[right]

示例 1:

1
2
3
4
5
6
7
8
9
10
11
输入:
["NumArray", "sumRange", "update", "sumRange"]
[[[1, 3, 5]], [0, 2], [1, 2], [0, 2]]
输出:
[null, 9, null, 8]

解释:
NumArray numArray = new NumArray([1, 3, 5]);
numArray.sumRange(0, 2); // 返回 1 + 3 + 5 = 9
numArray.update(1, 2); // nums = [1,2,5]
numArray.sumRange(0, 2); // 返回 1 + 2 + 5 = 8

提示:

  • 1 <= nums.length <= 3 * 10^4
  • -100 <= nums[i] <= 100
  • 0 <= index < nums.length
  • -100 <= val <= 100
  • 0 <= left <= right < nums.length
  • 调用 updatesumRange 方法次数不大于 3 * 10^4

树状数组

树状数组维护前缀和

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
class FenwickTree {
int[] tr; // [1, n]
public FenwickTree(int[] nums) { // [0, n-1]
this.tr = new int[nums.length+1];
for (int i = 0; i < nums.length; i++) {
add(i+1, nums[i]);
}
}
public int lowbit(int x) {
return x & (-x);
}
public void add(int i, int val) {
while (i < tr.length) {
tr[i] += val;
i += lowbit(i);
}
}
public int query(int i) {
int res = 0;
while (i > 0) {
res += tr[i];
i -= lowbit(i);
}
return res;
}
}
class NumArray {
FenwickTree tree;
int nums[];
public NumArray(int[] nums) {
this.nums = nums;
tree = new FenwickTree(nums);
}
public void update(int index, int val) {
tree.add(index+1, val-nums[index]);
nums[index] = val;
}

public int sumRange(int left, int right) {
return tree.query(right+1) - tree.query(left);
}
}

线段树

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
class NumArray {
int[] nums;
int len;
int[] tree;

void upd(int idx, int l, int r, int tl, int tr, int val) {
if (tl <= l && r <= tr) {
tree[idx] = val;
if (l != r) { // 没有用lazy数组的情况,就需要给所有子区间更新
int m = l + (r - l) / 2;
upd(2 * idx, l, m, tl, tr, val);
upd(2 * idx + 1, m + 1, r, tl, tr, val);
}
} else {
int m = l + (r - l) / 2;
if (tl <= m)
upd(2 * idx, l, m, tl, tr, val);
if (m + 1 <= tr)
upd(2 * idx + 1, m + 1, r, tl, tr, val);
tree[idx] = tree[2 * idx] + tree[2 * idx + 1];
}
}

int query(int idx, int l, int r, int tl, int tr) {
if (tl <= l && r <= tr) {
return tree[idx];
} else {
int m = l + (r - l) / 2, res = 0;
if (tl <= m)
res += query(2 * idx, l, m, tl, tr);
if (m + 1 <= tr)
res += query(2 * idx + 1, m + 1, r, tl, tr);
return res;
}
}

void build(int idx, int l, int r) {
if (l == r) {
tree[idx] = nums[l];
} else {
int m = l + (r - l) / 2;
build(2 * idx, l, m);
build(2 * idx + 1, m + 1, r);
tree[idx] = tree[2 * idx] + tree[2 * idx + 1];
}
}

public NumArray(int[] nums) {
this.nums = nums;
this.len = nums.length;
tree = new int[4 * len];
build(1, 0, len - 1);
}

public void update(int index, int val) {
upd(1, 0, len - 1, index, index, val);
}

public int sumRange(int left, int right) {
return query(1, 0, len - 1, left, right);
}
}

/**
* 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);
*/