-
Notifications
You must be signed in to change notification settings - Fork 20
/
segment-tree.lisp
209 lines (197 loc) · 9.42 KB
/
segment-tree.lisp
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
(defpackage :cp/segment-tree
(:use :cl)
(:export #:define-segtree)
(:documentation "Provides 1-dimensional segment tree over arbitrary monoid,
using bottom-up implementation."))
(in-package :cp/segment-tree)
(deftype index () '(mod #.(floor array-dimension-limit 2)))
(defmacro define-segtree (name &key (operator '#'+) (identity 0) element-type)
"OPERATOR := binary operator (comprising a monoid)
IDENTITY := object (identity element of the monoid)
ELEMENT-TYPE := type specifier
This macro defines five functions:
- <NAME>-REF: accessor and setter
- <NAME>-FOLD: function for querying range sum
- MAKE-<NAME>: linear-time constructor
- <NAME>-MAX-RIGHT: binary search w.r.t. range sum in normal order
- <NAME>-MIN-LEFT: binary search w.r.t. range sum in reverse order"
(let* ((fname-ref (intern (format nil "~A-REF" (symbol-name name))))
(fname-fold (intern (format nil "~A-FOLD" (symbol-name name))))
(fname-max-right (intern (format nil "~A-MAX-RIGHT" (symbol-name name))))
(fname-min-left (intern (format nil "~A-MIN-LEFT" (symbol-name name))))
(fname-make (intern (format nil "MAKE-~A" (symbol-name name))))
(fname-%make (intern (format nil "%MAKE-~A" (symbol-name name))))
(fname-n (intern (format nil "%~A-N" (symbol-name name))))
(fname-vector (intern (format nil "%~A-VECTOR" (symbol-name name))))
(conc-name (intern (format nil "%~A-" (symbol-name name)))))
`(progn
(defstruct (,name (:constructor ,fname-%make
(vector &aux (n (ash (+ 1 (length vector)) -1))))
(:conc-name ,conc-name))
(n nil :type index) ; length of original vector
(vector nil :type (simple-array ,element-type (*))))
(declaim (inline ,fname-make))
(defun ,fname-make (size &key (initial-element ,identity) initial-contents)
(declare (index size)
((or null sequence) initial-contents))
(let ((res (make-array (max 0 (- (* 2 size) 1))
:element-type ',element-type
:initial-element initial-element)))
(when initial-contents
(replace res initial-contents :start1 (max 0 (- size 1))))
(loop for i from (- size 2) downto 0
do (setf (aref res i)
(funcall ,operator
(aref res (+ (* 2 i) 1)) (aref res (+ (* 2 i) 2)))))
(,fname-%make res)))
(declaim (inline ,fname-ref))
(defun ,fname-ref (,name index)
"Returns the element at INDEX."
(declare (index index))
(aref (,fname-vector ,name)
(+ index (,fname-n ,name) -1)))
(declaim (inline (setf ,fname-ref)))
(defun (setf ,fname-ref) (new-value ,name index)
(declare (index index)
(,element-type new-value))
(let* ((vector (,fname-vector ,name))
(i (+ index (- (,fname-n ,name) 1))))
(declare (index i))
(setf (aref vector i) new-value)
(loop while (> i 0)
do (setq i (ash (- i 1) -1))
(setf (aref vector i)
(funcall ,operator
(aref vector (+ (* 2 i) 1))
(aref vector (+ (* 2 i) 2)))))
new-value))
(declaim (ftype (function * (values ,element-type &optional)) ,fname-fold)
(inline ,fname-fold))
(defun ,fname-fold (,name left right)
"Folds the given half-open range [LEFT, RIGHT)."
(declare (index left right))
(let* ((vector (,fname-vector ,name))
(l (max 0 (+ left (,fname-n ,name) -1)))
(r (max 0 (+ right (,fname-n ,name) -1)))
(lvalue ,identity)
(rvalue ,identity))
(declare (index l r)
(,element-type lvalue rvalue))
(loop while (< l r)
when (evenp l)
do (setq lvalue (funcall ,operator lvalue (aref vector l)))
(incf l)
when (evenp r)
do (decf r)
(setq rvalue (funcall ,operator (aref vector r) rvalue))
do (setq l (ash (- l 1) -1)
r (ash (- r 1) -1)))
(funcall ,operator lvalue rvalue)))
(declaim (ftype (function * (values index &optional)) ,fname-max-right)
(inline ,fname-max-right))
(defun ,fname-max-right (,name test &optional (start 0))
"Returns the rightmost index i that satisfies (FUNCALL TEST <fold of
range [START, i)>).
Note:
- (FUNCALL TEST <identity>) must be true.
- TEST must be monotone in the target range."
(declare (index start))
(assert (funcall test ,identity))
(assert (<= start (,fname-n ,name)))
(let* ((n (,fname-n ,name))
(vector (,fname-vector ,name))
(n* (length vector))
(l (max 0 (+ start (- n 1))))
(r n*)
(value ,identity)
(total-shift 0))
(declare (index l r total-shift)
(,element-type value))
(labels ((recur (index)
(declare (index index))
(loop while (< index (- n 1))
for new-value of-type ,element-type =
(funcall ,operator
value
(aref vector (+ 1 (* 2 index))))
when (funcall test new-value)
do (setq value new-value
index (+ 2 (* 2 index)))
else
do (setq index (+ 1 (* 2 index))))
(return-from ,fname-max-right (- index (- n 1)))))
(loop until (= l r)
when (evenp l)
do (let ((new-value (funcall ,operator value (aref vector l))))
(declare (,element-type new-value))
(unless (funcall test new-value)
(recur l))
(setq value new-value))
do (setq l (ash l -1) ; move to right block if L is even
r (ash (- r 1) -1)
total-shift (+ total-shift 1)))
(loop for shift from (- total-shift 1) above 0
for r = (- (ash (+ n* 1) (- shift)) 1)
when (evenp r)
do (let ((new-value (funcall ,operator value (aref vector (- r 1)))))
(declare (,element-type new-value))
(unless (funcall test new-value)
(recur (- r 1)))
(setq value new-value)))
n)))
(declaim (ftype (function * (values index &optional)) ,fname-min-left)
(inline ,fname-min-left))
(defun ,fname-min-left (,name test &optional end)
"Returns the largest index i that satisfies (FUNCALL TEST <fold of range [i,
END)>).
Note:
- (FUNCALL TEST <identity>) must be true.
- TEST must be monotone in the target range."
(declare ((or null index) end))
(assert (funcall test ,identity))
(assert (or (null end) (<= end (,fname-n ,name))))
(let* ((n (,fname-n ,name))
(vector (,fname-vector ,name))
(l (max 0 (- n 1)))
(r (max 0 (+ (or end n) (- n 1))))
(value ,identity)
(total-shift 0))
(declare (index l r total-shift)
(,element-type value))
(labels ((recur (index)
(declare (index index))
(loop while (< index (- n 1))
for new-value of-type ,element-type =
(funcall ,operator
(aref vector (+ 2 (* 2 index)))
value)
when (funcall test new-value)
do (setq value new-value
index (+ 1 (* 2 index)))
else
do (setq index (+ 2 (* 2 index))))
(return-from ,fname-min-left (+ 1 (- index (- n 1))))))
(loop until (= l r)
when (evenp r)
do (let ((new-value (funcall ,operator (aref vector (- r 1)) value)))
(declare (,element-type new-value))
(unless (funcall test new-value)
(recur (- r 1)))
(setq value new-value))
do (setq l (ash l -1) ; move to right block if L is even
r (ash (- r 1) -1)
total-shift (+ total-shift 1)))
(loop for shift from (- total-shift 1) downto 0
for l = (ash (- n 1) (- shift))
when (evenp l)
do (let ((new-value (funcall ,operator (aref vector l) value)))
(declare (,element-type new-value))
(unless (funcall test new-value)
(recur l))
(setq value new-value)))
0))))))
#+(or)
(define-segtree segtree
:operator #'+
:identity 0
:element-type fixnum)