-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
sqrtdecomposition.go
102 lines (94 loc) · 3.23 KB
/
sqrtdecomposition.go
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Package sqrt contains algorithms and data structures that contains a √n in their complexity
package sqrt
import "math"
// Sqrt (or Square Root) Decomposition is a technique used for query an array and perform updates
// Inside this package is described its most simple data structure, you can find more at: https://cp-algorithms.com/data_structures/sqrt_decomposition.html
//
// Formally, You can use SqrtDecomposition only if:
//
// Given a function $Query:E_1,...,E_n\rightarrow Q$
//
// if $\exist unionQ:Q,Q\rightarrow Q$
//
// s.t.
//
// - $\forall n\in \N > 1, 1\le i<n, E_1,..., E_n\in E \\ query(E_1,..., E_n)=unionQ(query(E_1,..., E_i), query(E_{i+1},...,E_n))$
//
// - (Only if you want use $update$ function)
// $\forall n\in \N > 0, E_1,..., E_n\in E \\ query(E_1,...,E_{new},..., E_n)=updateQ(query(E_1,...,E_{old},...,E_n), indexof(E_{old}), E_{new})$
type SqrtDecomposition[E any, Q any] struct {
querySingleElement func(element E) Q
unionQ func(q1 Q, q2 Q) Q
updateQ func(oldQ Q, oldE E, newE E) (newQ Q)
elements []E
blocks []Q
blockSize uint64
}
// Create a new SqrtDecomposition instance with the parameters as specified by SqrtDecomposition comment
// Assumptions:
// - len(elements) > 0
func NewSqrtDecomposition[E any, Q any](
elements []E,
querySingleElement func(element E) Q,
unionQ func(q1 Q, q2 Q) Q,
updateQ func(oldQ Q, oldE E, newE E) (newQ Q),
) *SqrtDecomposition[E, Q] {
sqrtDec := &SqrtDecomposition[E, Q]{
querySingleElement: querySingleElement,
unionQ: unionQ,
updateQ: updateQ,
elements: elements,
}
sqrt := math.Sqrt(float64(len(sqrtDec.elements)))
blockSize := uint64(sqrt)
numBlocks := uint64(math.Ceil(float64(len(elements)) / float64(blockSize)))
sqrtDec.blocks = make([]Q, numBlocks)
for i := uint64(0); i < uint64(len(elements)); i++ {
if i%blockSize == 0 {
sqrtDec.blocks[i/blockSize] = sqrtDec.querySingleElement(elements[i])
} else {
sqrtDec.blocks[i/blockSize] = sqrtDec.unionQ(sqrtDec.blocks[i/blockSize], sqrtDec.querySingleElement(elements[i]))
}
}
sqrtDec.blockSize = blockSize
return sqrtDec
}
// Performs a query from index start to index end (non included)
// Assumptions:
// - start < end
// - start and end are valid
func (s *SqrtDecomposition[E, Q]) Query(start uint64, end uint64) Q {
firstIndexNextBlock := ((start / s.blockSize) + 1) * s.blockSize
q := s.querySingleElement(s.elements[start])
if firstIndexNextBlock > end { // if in same block
start++
for start < end {
q = s.unionQ(q, s.querySingleElement(s.elements[start]))
start++
}
} else {
// left side
start++
for start < firstIndexNextBlock {
q = s.unionQ(q, s.querySingleElement(s.elements[start]))
start++
}
//middle part
endBlock := end / s.blockSize
for i := firstIndexNextBlock / s.blockSize; i < endBlock; i++ {
q = s.unionQ(q, s.blocks[i])
}
// right part
for i := endBlock * s.blockSize; i < end; i++ {
q = s.unionQ(q, s.querySingleElement(s.elements[i]))
}
}
return q
}
// Assumptions:
// - index is valid
func (s *SqrtDecomposition[E, Q]) Update(index uint64, newElement E) {
i := index / s.blockSize
s.blocks[i] = s.updateQ(s.blocks[i], s.elements[index], newElement)
s.elements[index] = newElement
}