Skip to content

Commit 81f91f4

Browse files
committed
🐞 fix(NewWaveletMatrix): dataCopy
1 parent 8290d42 commit 81f91f4

File tree

8 files changed

+738
-182
lines changed

8 files changed

+738
-182
lines changed

18_哈希/BitSet/BitSet2.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// 一个list,长度固定为n,两种操作:
2+
// 1. set i, v 设置某个下标为0或1
3+
// 2. query i 查找从i下标开始的第一个1的位置
4+
5+
16
/**
27
* 01线段树,支持 flip/indexOf/onesCount/kth,可用于模拟Bitset
38
*/

18_哈希/BitSet/BitVector.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// WaveletMatrix 里用到的BitVector
2+
3+
package main
4+
5+
import "math/bits"
6+
7+
type BitVector struct {
8+
n int
9+
block []int
10+
sum []int // block ごとに立っている 1 の数の累積和
11+
}
12+
13+
func NewBitVector(n int) *BitVector {
14+
blockCount := (n + 63) >> 6
15+
return &BitVector{
16+
n: n,
17+
block: make([]int, blockCount),
18+
sum: make([]int, blockCount),
19+
}
20+
}
21+
22+
func (f *BitVector) Set(i int) {
23+
f.block[i>>6] |= 1 << uint(i&63)
24+
}
25+
26+
func (f *BitVector) Build() {
27+
for i := 0; i < len(f.block)-1; i++ {
28+
f.sum[i+1] = f.sum[i] + bits.OnesCount(uint(f.block[i]))
29+
}
30+
}
31+
32+
func (f *BitVector) Get(i int) int {
33+
return (f.block[i>>6] >> uint(i&63)) & 1
34+
}
35+
36+
// 统计 [0,end) 中 value 的个数
37+
func (f *BitVector) Count(value, end int) int {
38+
if value == 1 {
39+
return f.count1(end)
40+
}
41+
return end - f.count1(end)
42+
}
43+
44+
// 统计 [0,end) 中第 k(0-indexed) 个 value 的位置
45+
func (f *BitVector) Index(value, k int) int {
46+
if k < 0 || f.Count(value, f.n) <= k {
47+
return -1
48+
}
49+
50+
left, right := 0, f.n
51+
for right-left > 1 {
52+
mid := (left + right) >> 1
53+
if f.Count(value, mid) >= k+1 {
54+
right = mid
55+
} else {
56+
left = mid
57+
}
58+
}
59+
return right - 1
60+
}
61+
62+
func (f *BitVector) IndexWithStart(value, k, start int) int {
63+
return f.Index(value, k+f.Count(value, start))
64+
}
65+
66+
func (f *BitVector) count1(k int) int {
67+
mask := (1 << uint(k&63)) - 1
68+
return f.sum[k>>6] + bits.OnesCount(uint(f.block[k>>6]&mask))
69+
}

算法竞赛进阶指南/GoDS (Go Data Structures)/Lib/Maps/a.java

Lines changed: 0 additions & 7 deletions
This file was deleted.

算法竞赛进阶指南/GoDS (Go Data Structures)/a.md

Lines changed: 0 additions & 111 deletions
This file was deleted.

算法竞赛进阶指南/GoDS (Go Data Structures)/src/note.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ https://qiita.com/ktateish/items/ab2df3e0864d2e931bf2
2020
func main() {
2121
slice1 := []int{1, 2, 3, 4, 5}
2222
sliceCopy := append([]int{}, slice1...)
23+
// 不需要写类型拷贝
24+
// sliceCopy := append(slice1[:0:0], slice1...)
2325
sliceCopy[0] = 100
2426
fmt.Println(slice1, sliceCopy) // [1 2 3 4 5] [100 2 3 4 5]
2527
}

0 commit comments

Comments
 (0)