-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbit.go
More file actions
43 lines (38 loc) · 696 Bytes
/
bit.go
File metadata and controls
43 lines (38 loc) · 696 Bytes
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
package bit
func lowbit(x int) int {
return x & (-x)
}
// BIT is Binary Indexed Tree.
type BIT struct {
c []int
len int
}
// New func is constructor of BIT struct.
func New(nums []int) *BIT {
len := len(nums) + 1
c := make([]int, len, len)
for i := 1; i < len; i++ {
c[i] = nums[i-1]
for j := i - 2; j >= i-lowbit(i); j-- {
c[i] += nums[j]
}
}
return &BIT{c: c, len: len}
}
// Update nums[i], add delta.
func (b *BIT) Update(i int, delta int) {
for j := i + 1; j < b.len; j += lowbit(j) {
b.c[j] += delta
if lowbit(j) == 0 {
break
}
}
}
// Sum 1--i.
func (b *BIT) Sum(i int) int {
res := 0
for j := i; j > 0; j -= lowbit(j) {
res += b.c[j]
}
return res
}