-
Notifications
You must be signed in to change notification settings - Fork 481
/
0307.js
49 lines (45 loc) · 975 Bytes
/
0307.js
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
/**
* @param {number[]} nums
*/
var NumArray = function(data) {
this.nums = data;
this.tree = new Array(data.length + 1);
this.tree.fill(0);
for (let i = 0; i < data.length; i++) {
this._update(i+1, data[i]);
}
};
NumArray.prototype._update = function(i, val) {
while (i < this.tree.length) {
this.tree[i] += val;
i += this.lowbit(i);
}
};
NumArray.prototype.query = function(i) {
let res = 0;
while (i) {
res += this.tree[i];
i -= this.lowbit(i);
}
return res;
};
NumArray.prototype.lowbit = function(x) {
return x & (-x);
};
/**
* @param {number} i
* @param {number} val
* @return {void}
*/
NumArray.prototype.update = function(i, val) {
this._update(i+1, val - this.nums[i]);
this.nums[i] = val;
};
/**
* @param {number} i
* @param {number} j
* @return {number}
*/
NumArray.prototype.sumRange = function(i, j) {
return this.query(j+1) - this.query(i);
};