Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_segment_tree.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
62# ifndef TPL_SEGMENT_TREE_H
63# define TPL_SEGMENT_TREE_H
64
65# include <cassert>
66# include <concepts>
67# include <algorithm>
68# include <initializer_list>
69# include <limits>
70# include <type_traits>
71# include <utility>
72# include <vector>
73# include <ah-concepts.H>
74# include <tpl_array.H>
75# include <tpl_dynList.H>
76# include <ahFunction.H>
77# include <ah-errors.H>
78# include <tpl_sparse_table.H>
79
80namespace Aleph
81{
98 template <typename F, typename T>
100
101 // ================================================================
102 // Gen_Segment_Tree — Point update + Range query
103 // ================================================================
104
141 template <typename T, class Op>
142 requires SegmentTreeOp<Op, T>
144 {
145 Array<T> tree; // 1-indexed implicit binary tree
146 size_t n = 0; // number of logical elements
147 T identity; // identity element of the monoid
148 Op op;
149
150 void build(size_t node, const size_t start, const size_t end)
151 {
152 if (start == end)
153 return;
154
155 const size_t mid = start + (end - start) / 2;
156 build(2 * node, start, mid);
157 build(2 * node + 1, mid + 1, end);
158 tree(node) = op(tree(2 * node), tree(2 * node + 1));
159 }
160
161 void update_impl(size_t node, const size_t start, const size_t end,
162 const size_t idx, const T & delta)
163 {
164 if (start == end)
165 {
166 tree(node) = op(tree(node), delta);
167 return;
168 }
169
170 if (const size_t mid = start + (end - start) / 2; idx <= mid)
171 update_impl(2 * node, start, mid, idx, delta);
172 else
173 update_impl(2 * node + 1, mid + 1, end, idx, delta);
174
175 tree(node) = op(tree(2 * node), tree(2 * node + 1));
176 }
177
178 void set_impl(size_t node, const size_t start, const size_t end,
179 const size_t idx, const T & val)
180 {
181 if (start == end)
182 {
183 tree(node) = val;
184 return;
185 }
186
187 if (const size_t mid = start + (end - start) / 2; idx <= mid)
188 set_impl(2 * node, start, mid, idx, val);
189 else
190 set_impl(2 * node + 1, mid + 1, end, idx, val);
191
192 tree(node) = op(tree(2 * node), tree(2 * node + 1));
193 }
194
195 T query_impl(size_t node, const size_t start, const size_t end,
196 const size_t l, const size_t r) const
197 {
198 if (r < start || end < l)
199 return identity;
200
201 if (l <= start and end <= r)
202 return tree(node);
203
204 const size_t mid = start + (end - start) / 2;
205 return op(query_impl(2 * node, start, mid, l, r),
206 query_impl(2 * node + 1, mid + 1, end, l, r));
207 }
208
209 template <class Getter>
210 void fill_and_build(Getter getter)
211 {
212 // Place values in leaves via recursive descent
213 fill_leaves_recursive(1, 0, n - 1, getter);
214
215 // Build internal nodes bottom-up
216 build(1, 0, n - 1);
217 }
218
219 template <class Getter>
220 void fill_leaves_recursive(size_t node, size_t start, size_t end,
221 Getter & getter)
222 {
223 if (start == end)
224 {
225 tree(node) = getter(start);
226 return;
227 }
228
229 const size_t mid = start + (end - start) / 2;
230 fill_leaves_recursive(2 * node, start, mid, getter);
231 fill_leaves_recursive(2 * node + 1, mid + 1, end, getter);
232 }
233
234 template <class AlephIt>
235 void fill_from_aleph_it(AlephIt it)
236 {
237 auto tmp = Array<T>::create(n);
238 size_t i = 0;
239 for (; it.has_curr(); it.next_ne())
240 tmp(i++) = it.get_curr();
241
242 auto getter = [&tmp](size_t j) { return tmp(j); };
243 fill_and_build(getter);
244 }
245
246 public:
247 using Item_Type = T;
248
257 Gen_Segment_Tree(const size_t num, const T & init_val,
258 const T & id, Op oper = Op())
259 : tree(num == 0 ? 1 : 4 * num, id),
260 n(num), identity(id), op(oper)
261 {
262 if (n > 0)
263 fill_and_build([&init_val](size_t) { return init_val; });
264 }
265
278 Gen_Segment_Tree(std::initializer_list<T> il,
279 const T & id, Op oper = Op())
280 : tree(il.size() == 0 ? 1 : 4 * il.size(), id),
281 n(il.size()), identity(id), op(oper)
282 {
283 if (n > 0)
284 {
285 auto it = il.begin();
286 fill_and_build([&it](size_t) { return *it++; });
287 }
288 }
289
297 const T & id, Op oper = Op())
298 : tree(values.size() == 0 ? 1 : 4 * values.size(), id),
299 n(values.size()), identity(id), op(oper)
300 {
301 if (n > 0)
302 fill_and_build([&values](size_t i) { return values(i); });
303 }
304
311 Gen_Segment_Tree(const std::vector<T> & values,
312 const T & id, Op oper = Op())
313 : tree(values.size() == 0 ? 1 : 4 * values.size(), id),
314 n(values.size()), identity(id), op(oper)
315 {
316 if (n > 0)
317 fill_and_build([&values](size_t i) { return values[i]; });
318 }
319
327 const T & id, Op oper = Op())
328 : tree(values.size() == 0 ? 1 : 4 * values.size(), id),
329 n(values.size()), identity(id), op(oper)
330 {
331 if (n > 0)
332 fill_from_aleph_it(values.get_it());
333 }
334
336
345 std::is_nothrow_move_constructible_v<Array<T>> and
346 std::is_nothrow_move_constructible_v<T> and
347 std::is_nothrow_move_constructible_v<Op>)
348 : tree(std::move(other.tree)), n(std::exchange(other.n, 0)),
349 identity(std::move(other.identity)), op(std::move(other.op))
350 {}
351
353
359 Gen_Segment_Tree & operator=(Gen_Segment_Tree && other) noexcept(noexcept(swap(other)))
360 {
361 swap(other);
362 return *this;
363 }
364
371 void update(const size_t i, const T & delta)
372 {
374 << "Gen_Segment_Tree::update: index " << i << " >= size " << n;
375
376 update_impl(1, 0, n - 1, i, delta);
377 }
378
385 void set(const size_t i, const T & val)
386 {
388 << "Gen_Segment_Tree::set: index " << i << " >= size " << n;
389
390 set_impl(1, 0, n - 1, i, val);
391 }
392
402 T query(const size_t l, const size_t r) const
403 {
405 << "Gen_Segment_Tree::query: r=" << r << " >= n=" << n;
407 << "Gen_Segment_Tree::query: l=" << l << " > r=" << r;
408
409 return query_impl(1, 0, n - 1, l, r);
410 }
411
419 T get(const size_t i) const
420 {
422 << "Gen_Segment_Tree::get: index " << i << " >= size " << n;
423
424 return query_impl(1, 0, n - 1, i, i);
425 }
426
428 [[nodiscard]] constexpr size_t size() const noexcept { return n; }
429
431 [[nodiscard]] constexpr bool is_empty() const noexcept { return n == 0; }
432
438 {
439 auto ret = Array<T>::create(n);
440 for (size_t i = 0; i < n; ++i)
441 ret(i) = get(i);
442 return ret;
443 }
444
454 void swap(Gen_Segment_Tree & other) noexcept(
455 std::is_nothrow_swappable_v<T> and std::is_nothrow_swappable_v<Op>)
456 {
457 // The identities and the operations go first when their swaps may
458 // throw, so that one that throws leaves no array with the identity
459 // or the operation of the other. When none may, the order cannot be
460 // seen, and they go last, as they always did: an identity that has
461 // state, an int, would reorder the code if it went first
462 constexpr bool may_throw =
463 not std::is_nothrow_swappable_v<T> or not std::is_nothrow_swappable_v<Op>;
464 if constexpr (may_throw)
466 tree.swap(other.tree);
467 std::swap(n, other.n);
468 if constexpr (not may_throw)
470 }
471
472 private:
473 // The identities and the operations, with the swaps that the
474 // specification of swap() evaluates (found by their arguments, else
475 // std::swap()), both or none: if that of the operations throws, the
476 // identities are exchanged back
478 {
479 using std::swap;
480 swap(identity, other.identity);
481 try
482 {
483 swap(op, other.op);
484 }
485 catch (...)
486 {
487 swap(identity, other.identity);
488 throw;
489 }
490 }
491 };
492
493
511 template <typename T>
513 : public Gen_Segment_Tree<T, Aleph::plus<T>>
514 {
516
523 Sum_Segment_Tree(const size_t num, const T & init_val = T())
524 : Base(num, init_val, T())
525 {}
526
532 Sum_Segment_Tree(std::initializer_list<T> il)
533 : Base(il, T())
534 {}
535
542 : Base(values, T())
543 {}
544
550 Sum_Segment_Tree(const std::vector<T> & values)
551 : Base(values, T())
552 {}
553
560 : Base(values, T())
561 {}
562 };
563
584 template <std::totally_ordered T>
586 : public Gen_Segment_Tree<T, Min_Op<T>>
587 {
589
596 Min_Segment_Tree(const size_t num,
597 const T & init_val = std::numeric_limits<T>::max())
598 : Base(num, init_val, std::numeric_limits<T>::max())
599 {}
600
606 Min_Segment_Tree(std::initializer_list<T> il)
607 : Base(il, std::numeric_limits<T>::max())
608 {}
609
616 : Base(values, std::numeric_limits<T>::max())
617 {}
618
624 Min_Segment_Tree(const std::vector<T> & values)
625 : Base(values, std::numeric_limits<T>::max())
626 {}
627
634 : Base(values, std::numeric_limits<T>::max())
635 {}
636 };
637
659 template <std::totally_ordered T>
661 : public Gen_Segment_Tree<T, Max_Op<T>>
662 {
664
671 Max_Segment_Tree(const size_t num,
672 const T & init_val = std::numeric_limits<T>::lowest())
673 : Base(num, init_val, std::numeric_limits<T>::lowest())
674 {}
675
681 Max_Segment_Tree(std::initializer_list<T> il)
682 : Base(il, std::numeric_limits<T>::lowest())
683 {}
684
691 : Base(values, std::numeric_limits<T>::lowest())
692 {}
693
699 Max_Segment_Tree(const std::vector<T> & values)
700 : Base(values, std::numeric_limits<T>::lowest())
701 {}
702
709 : Base(values, std::numeric_limits<T>::lowest())
710 {}
711 };
712
713
714 // ================================================================
715 // Gen_Lazy_Segment_Tree — Range update + Range query
716 // ================================================================
717
743 template <typename P>
745 std::equality_comparable<typename P::lazy_type> and
746 requires(const P& p,
747 const typename P::value_type& v1,
748 const typename P::value_type& v2,
749 const typename P::lazy_type& lz1,
750 const typename P::lazy_type& lz2,
751 size_t count)
752 {
753 typename P::value_type;
754 typename P::lazy_type;
755 { p.combine(v1, v2) } -> std::convertible_to<typename P::value_type>;
756 { p.apply(v1, lz1, count) } -> std::convertible_to<typename P::value_type>;
757 { p.compose(lz1, lz2) } -> std::convertible_to<typename P::lazy_type>;
758 { p.value_identity() } -> std::convertible_to<typename P::value_type>;
759 { p.lazy_identity() } -> std::convertible_to<typename P::lazy_type>;
760 };
761
775 template <typename T>
777 {
778 using value_type = T;
779 using lazy_type = T;
780
781 static constexpr T combine(const T & a, const T & b) noexcept
782 {
783 return a + b;
784 }
785
786 static constexpr T apply(const T & val, const T & delta,
787 size_t count) noexcept
788 {
789 return val + delta * static_cast<T>(count);
790 }
791
792 static constexpr T compose(const T & old_lazy,
793 const T & new_lazy) noexcept
794 {
795 return old_lazy + new_lazy;
796 }
797
798 static constexpr T value_identity() noexcept { return T(); }
799 static constexpr T lazy_identity() noexcept { return T(); }
800 };
801
815 template <std::totally_ordered T>
817 {
818 using value_type = T;
819 using lazy_type = T;
820
821 static constexpr T combine(const T & a, const T & b) noexcept
822 {
823 return a <= b ? a : b;
824 }
825
826 static constexpr T apply(const T & val, const T & delta,
827 size_t) noexcept
828 {
829 return val + delta;
830 }
831
832 static constexpr T compose(const T & old_lazy,
833 const T & new_lazy) noexcept
834 {
835 return old_lazy + new_lazy;
836 }
837
838 constexpr T value_identity() const noexcept
839 {
840 return std::numeric_limits<T>::max();
841 }
842
843 static constexpr T lazy_identity() noexcept { return T(); }
844 };
845
859 template <std::totally_ordered T>
861 {
862 using value_type = T;
863 using lazy_type = T;
864
865 static constexpr T combine(const T & a, const T & b) noexcept
866 {
867 return a >= b ? a : b;
868 }
869
870 static constexpr T apply(const T & val, const T & delta,
871 size_t) noexcept
872 {
873 return val + delta;
874 }
875
876 static constexpr T compose(const T & old_lazy,
877 const T & new_lazy) noexcept
878 {
879 return old_lazy + new_lazy;
880 }
881
882 constexpr T value_identity() const noexcept
883 {
884 return std::numeric_limits<T>::lowest();
885 }
886
887 static constexpr T lazy_identity() noexcept { return T(); }
888 };
889
909 template <typename T>
911 {
912 using value_type = T;
913 using lazy_type = std::pair<bool, T>;
914
915 static constexpr T combine(const T & a, const T & b) noexcept
916 {
917 return a + b;
918 }
919
920 static constexpr T apply(const T & val, const lazy_type & lz,
921 size_t count) noexcept
922 {
923 return lz.first ? lz.second * static_cast<T>(count) : val;
924 }
925
926 static constexpr lazy_type compose(const lazy_type & old_lz,
927 const lazy_type & new_lz) noexcept
928 {
929 return new_lz.first ? new_lz : old_lz;
930 }
931
932 static constexpr T value_identity() noexcept { return T(); }
933
934 static constexpr lazy_type lazy_identity() noexcept
935 {
936 return {false, T()};
937 }
938 };
939
940
969 template <typename Policy>
970 requires SegmentTreePolicy<Policy>
972 {
973 public:
974 using value_type = typename Policy::value_type;
975 using lazy_type = typename Policy::lazy_type;
977
978 private:
981 size_t n = 0;
982 Policy pol;
983
984 void push_down(size_t node, const size_t start, const size_t end) const
985 {
986 if (lazy(node) == pol.lazy_identity())
987 return;
988
989 const size_t mid = start + (end - start) / 2;
990 const size_t left = 2 * node;
991 const size_t right = 2 * node + 1;
992
993 tree(left) = pol.apply(tree(left), lazy(node), mid - start + 1);
994 lazy(left) = pol.compose(lazy(left), lazy(node));
995
996 tree(right) = pol.apply(tree(right), lazy(node), end - mid);
997 lazy(right) = pol.compose(lazy(right), lazy(node));
998
999 lazy(node) = pol.lazy_identity();
1000 }
1001
1002 void build(size_t node, const size_t start, const size_t end)
1003 {
1004 if (start == end)
1005 return;
1006
1007 const size_t mid = start + (end - start) / 2;
1008 build(2 * node, start, mid);
1009 build(2 * node + 1, mid + 1, end);
1010 tree(node) = pol.combine(tree(2 * node), tree(2 * node + 1));
1011 }
1012
1013 void update_impl(size_t node, const size_t start, const size_t end,
1014 const size_t l, const size_t r, const lazy_type & val)
1015 {
1016 if (r < start or end < l)
1017 return;
1018
1019 if (l <= start and end <= r)
1020 {
1021 tree(node) = pol.apply(tree(node), val, end - start + 1);
1022 lazy(node) = pol.compose(lazy(node), val);
1023 return;
1024 }
1025
1026 push_down(node, start, end);
1027
1028 const size_t mid = start + (end - start) / 2;
1029 update_impl(2 * node, start, mid, l, r, val);
1030 update_impl(2 * node + 1, mid + 1, end, l, r, val);
1031 tree(node) = pol.combine(tree(2 * node), tree(2 * node + 1));
1032 }
1033
1034 value_type query_impl(size_t node, const size_t start, const size_t end,
1035 const size_t l, const size_t r) const
1036 {
1037 if (r < start or end < l)
1038 return pol.value_identity();
1039
1040 if (l <= start and end <= r)
1041 return tree(node);
1042
1043 push_down(node, start, end);
1044
1045 const size_t mid = start + (end - start) / 2;
1046 return pol.combine(
1047 query_impl(2 * node, start, mid, l, r),
1048 query_impl(2 * node + 1, mid + 1, end, l, r));
1049 }
1050
1051 void set_impl(size_t node, const size_t start, const size_t end,
1052 const size_t idx, const value_type & val)
1053 {
1054 if (start == end)
1055 {
1056 tree(node) = val;
1057 lazy(node) = pol.lazy_identity();
1058 return;
1059 }
1060
1061 push_down(node, start, end);
1062
1063 if (const size_t mid = start + (end - start) / 2; idx <= mid)
1064 set_impl(2 * node, start, mid, idx, val);
1065 else
1066 set_impl(2 * node + 1, mid + 1, end, idx, val);
1067
1068 tree(node) = pol.combine(tree(2 * node), tree(2 * node + 1));
1069 }
1070
1071 template <class Getter>
1072 void fill_and_build(Getter getter)
1073 {
1074 fill_leaves_recursive(1, 0, n - 1, getter);
1075 build(1, 0, n - 1);
1076 }
1077
1078 template <class Getter>
1079 void fill_leaves_recursive(size_t node, size_t start, size_t end,
1080 Getter & getter)
1081 {
1082 if (start == end)
1083 {
1084 tree(node) = getter(start);
1085 return;
1086 }
1087
1088 const size_t mid = start + (end - start) / 2;
1089 fill_leaves_recursive(2 * node, start, mid, getter);
1090 fill_leaves_recursive(2 * node + 1, mid + 1, end, getter);
1091 }
1092
1093 template <class AlephIt>
1094 void fill_from_aleph_it(AlephIt it)
1095 {
1096 auto tmp = Array<value_type>::create(n);
1097 size_t i = 0;
1098 for (; it.has_curr(); it.next_ne())
1099 tmp(i++) = it.get_curr();
1100
1101 auto getter = [&tmp](size_t j) { return tmp(j); };
1102 fill_and_build(getter);
1103 }
1104
1105 size_t alloc_size() const
1106 {
1107 return n == 0 ? 1 : 4 * n;
1108 }
1109
1110 public:
1112 Gen_Lazy_Segment_Tree(const size_t num,
1113 const value_type & init_val = value_type(),
1114 Policy p = Policy())
1115 : tree(num == 0 ? 1 : 4 * num, p.value_identity()),
1116 lazy(num == 0 ? 1 : 4 * num, p.lazy_identity()),
1117 n(num), pol(p)
1118 {
1119 if (n > 0)
1120 fill_and_build([&init_val](size_t) { return init_val; });
1121 }
1122
1124 Gen_Lazy_Segment_Tree(std::initializer_list<value_type> il,
1125 Policy p = Policy())
1126 : tree(il.size() == 0 ? 1 : 4 * il.size(), p.value_identity()),
1127 lazy(il.size() == 0 ? 1 : 4 * il.size(), p.lazy_identity()),
1128 n(il.size()), pol(p)
1129 {
1130 if (n > 0)
1131 {
1132 auto it = il.begin();
1133 fill_and_build([&it](size_t) { return *it++; });
1134 }
1135 }
1136
1139 Policy p = Policy())
1140 : tree(values.size() == 0 ? 1 : 4 * values.size(), p.value_identity()),
1141 lazy(values.size() == 0 ? 1 : 4 * values.size(), p.lazy_identity()),
1142 n(values.size()), pol(p)
1143 {
1144 if (n > 0)
1145 fill_and_build([&values](size_t i) { return values(i); });
1146 }
1147
1149 Gen_Lazy_Segment_Tree(const std::vector<value_type> & values,
1150 Policy p = Policy())
1151 : tree(values.size() == 0 ? 1 : 4 * values.size(), p.value_identity()),
1152 lazy(values.size() == 0 ? 1 : 4 * values.size(), p.lazy_identity()),
1153 n(values.size()), pol(p)
1154 {
1155 if (n > 0)
1156 fill_and_build([&values](size_t i) { return values[i]; });
1157 }
1158
1161 Policy p = Policy())
1162 : tree(values.size() == 0 ? 1 : 4 * values.size(), p.value_identity()),
1163 lazy(values.size() == 0 ? 1 : 4 * values.size(), p.lazy_identity()),
1164 n(values.size()), pol(p)
1165 {
1166 if (n > 0)
1168 }
1169
1171
1180 std::is_nothrow_move_constructible_v<Array<value_type>> and
1181 std::is_nothrow_move_constructible_v<Array<lazy_type>> and
1182 std::is_nothrow_move_constructible_v<Policy>)
1183 : tree(std::move(other.tree)), lazy(std::move(other.lazy)),
1184 n(std::exchange(other.n, 0)), pol(std::move(other.pol))
1185 {}
1186
1188
1195 noexcept(noexcept(swap(other)))
1196 {
1197 swap(other);
1198 return *this;
1199 }
1200
1209 void update(const size_t l, const size_t r, const lazy_type & val)
1210 {
1212 << "Gen_Lazy_Segment_Tree::update: r=" << r << " >= n=" << n;
1214 << "Gen_Lazy_Segment_Tree::update: l=" << l << " > r=" << r;
1215
1216 update_impl(1, 0, n - 1, l, r, val);
1217 }
1218
1223 void point_update(const size_t i, const lazy_type & delta)
1224 {
1225 update(i, i, delta);
1226 }
1227
1236 void set(const size_t i, const value_type & val)
1237 {
1239 << "Gen_Lazy_Segment_Tree::set: index " << i << " >= size " << n;
1240
1241 set_impl(1, 0, n - 1, i, val);
1242 }
1243
1255 value_type query(const size_t l, const size_t r) const
1256 {
1258 << "Gen_Lazy_Segment_Tree::query: r=" << r << " >= n=" << n;
1260 << "Gen_Lazy_Segment_Tree::query: l=" << l << " > r=" << r;
1261
1262 return query_impl(1, 0, n - 1, l, r);
1263 }
1264
1269 value_type get(const size_t i) const
1270 {
1271 return query(i, i);
1272 }
1273
1275 [[nodiscard]] constexpr size_t size() const noexcept { return n; }
1276
1278 [[nodiscard]] constexpr bool is_empty() const noexcept { return n == 0; }
1279
1285 {
1286 auto ret = Array<value_type>::create(n);
1287 for (size_t i = 0; i < n; ++i)
1288 ret(i) = get(i);
1289 return ret;
1290 }
1291
1299 void swap(Gen_Lazy_Segment_Tree & other) noexcept(std::is_nothrow_swappable_v<Policy>)
1300 {
1301 // the swap that the specification evaluates: that of Policy found by
1302 // its arguments, else std::swap(); first, so that one that throws
1303 // leaves no aggregates or pending tags with the policy of the other
1304 using std::swap;
1305 swap(pol, other.pol);
1306 tree.swap(other.tree);
1307 lazy.swap(other.lazy);
1308 std::swap(n, other.n);
1309 }
1310 };
1311
1312
1317 template <typename T>
1319 : public Gen_Lazy_Segment_Tree<Add_Sum_Policy<T>>
1320 {
1322 using Base::Base;
1323 };
1324
1333 template <std::totally_ordered T>
1335 : public Gen_Lazy_Segment_Tree<Add_Min_Policy<T>>
1336 {
1338 using Base::Base;
1339 };
1340
1349 template <std::totally_ordered T>
1351 : public Gen_Lazy_Segment_Tree<Add_Max_Policy<T>>
1352 {
1354 using Base::Base;
1355 };
1356
1357
1358 // ================================================================
1359 // Segment_Tree_Beats — Ji Driver's Segment Tree
1360 // ================================================================
1361
1389 template <typename T>
1390 requires std::is_arithmetic_v<T> and std::is_signed_v<T>
1392 {
1399
1401 size_t n = 0;
1402
1403 static constexpr T INF = std::numeric_limits<T>::max();
1404 static constexpr T NEG_INF = std::numeric_limits<T>::lowest();
1405
1406 void init_leaf(size_t node, const T & val)
1407 {
1408 tree(node) = {val, NEG_INF, val, INF, val,
1409 1, 1, INF, NEG_INF};
1410 }
1411
1412 void pull_up(size_t node) const
1413 {
1414 const auto & l = tree(2 * node);
1415 const auto & r = tree(2 * node + 1);
1416
1417 tree(node).sum = l.sum + r.sum;
1418
1419 // max
1420 if (l.max_val > r.max_val)
1421 {
1422 tree(node).max_val = l.max_val;
1423 tree(node).count_max = l.count_max;
1424 tree(node).second_max = std::max(l.second_max, r.max_val);
1425 }
1426 else if (l.max_val < r.max_val)
1427 {
1428 tree(node).max_val = r.max_val;
1429 tree(node).count_max = r.count_max;
1430 tree(node).second_max = std::max(l.max_val, r.second_max);
1431 }
1432 else
1433 {
1434 tree(node).max_val = l.max_val;
1435 tree(node).count_max = l.count_max + r.count_max;
1436 tree(node).second_max = std::max(l.second_max, r.second_max);
1437 }
1438
1439 // min
1440 if (l.min_val < r.min_val)
1441 {
1442 tree(node).min_val = l.min_val;
1443 tree(node).count_min = l.count_min;
1444 tree(node).second_min = std::min(l.second_min, r.min_val);
1445 }
1446 else if (l.min_val > r.min_val)
1447 {
1448 tree(node).min_val = r.min_val;
1449 tree(node).count_min = r.count_min;
1450 tree(node).second_min = std::min(l.min_val, r.second_min);
1451 }
1452 else
1453 {
1454 tree(node).min_val = l.min_val;
1455 tree(node).count_min = l.count_min + r.count_min;
1456 tree(node).second_min = std::min(l.second_min, r.second_min);
1457 }
1458
1459 tree(node).lazy_chmin = INF;
1460 tree(node).lazy_chmax = NEG_INF;
1461 }
1462
1463 void push_chmin_to_child(size_t child, T v) const
1464 {
1465 auto & nd = tree(child);
1466 if (v >= nd.max_val)
1467 return;
1468
1469 nd.sum -= static_cast<T>(nd.count_max) * (nd.max_val - v);
1470
1471 // If all elements in this node are the same value (max == min),
1472 // then reducing max also reduces min
1473 if (nd.min_val == nd.max_val)
1474 nd.min_val = v;
1475 if (nd.second_min == nd.max_val)
1476 nd.second_min = v;
1477
1478 nd.max_val = v;
1479
1480 // Compose with existing lazy tags
1481 nd.lazy_chmin = std::min(nd.lazy_chmin, v);
1482 nd.lazy_chmax = std::min(nd.lazy_chmax, v);
1483 }
1484
1485 void push_chmax_to_child(size_t child, T v) const
1486 {
1487 auto & nd = tree(child);
1488 if (v <= nd.min_val)
1489 return;
1490
1491 nd.sum += static_cast<T>(nd.count_min) * (v - nd.min_val);
1492
1493 // If all elements are the same value, raising min also raises max
1494 if (nd.max_val == nd.min_val)
1495 nd.max_val = v;
1496 if (nd.second_max == nd.min_val)
1497 nd.second_max = v;
1498
1499 nd.min_val = v;
1500
1501 // Compose with existing lazy tags
1502 nd.lazy_chmax = std::max(nd.lazy_chmax, v);
1503 nd.lazy_chmin = std::max(nd.lazy_chmin, v);
1504 }
1505
1506 void push_down(size_t node) const
1507 {
1508 auto & nd = tree(node);
1509
1510 // Push chmax first (raise minimum), then chmin (lower maximum)
1511 // This order is critical for correctness
1512 if (nd.lazy_chmax != NEG_INF)
1513 {
1514 push_chmax_to_child(2 * node, nd.lazy_chmax);
1515 push_chmax_to_child(2 * node + 1, nd.lazy_chmax);
1516 nd.lazy_chmax = NEG_INF;
1517 }
1518
1519 if (nd.lazy_chmin != INF)
1520 {
1521 push_chmin_to_child(2 * node, nd.lazy_chmin);
1522 push_chmin_to_child(2 * node + 1, nd.lazy_chmin);
1523 nd.lazy_chmin = INF;
1524 }
1525 }
1526
1527 void build(size_t node, size_t start, size_t end)
1528 {
1529 if (start == end)
1530 return;
1531
1532 const size_t mid = start + (end - start) / 2;
1533 build(2 * node, start, mid);
1534 build(2 * node + 1, mid + 1, end);
1535 pull_up(node);
1536 }
1537
1538 void chmin_impl(size_t node, const size_t start, const size_t end,
1539 const size_t l, const size_t r, T v)
1540 {
1541 if (r < start or end < l or tree(node).max_val <= v)
1542 return;
1543
1544 if (l <= start and end <= r and tree(node).second_max < v)
1545 {
1546 push_chmin_to_child(node, v);
1547 return;
1548 }
1549
1550 push_down(node);
1551 const size_t mid = start + (end - start) / 2;
1552 chmin_impl(2 * node, start, mid, l, r, v);
1553 chmin_impl(2 * node + 1, mid + 1, end, l, r, v);
1554 pull_up(node);
1555 }
1556
1557 void chmax_impl(size_t node, const size_t start, const size_t end,
1558 const size_t l, const size_t r, T v)
1559 {
1560 if (r < start or end < l or tree(node).min_val >= v)
1561 return;
1562
1563 if (l <= start and end <= r and tree(node).second_min > v)
1564 {
1565 push_chmax_to_child(node, v);
1566 return;
1567 }
1568
1569 push_down(node);
1570 const size_t mid = start + (end - start) / 2;
1571 chmax_impl(2 * node, start, mid, l, r, v);
1572 chmax_impl(2 * node + 1, mid + 1, end, l, r, v);
1573 pull_up(node);
1574 }
1575
1576 T query_sum_impl(size_t node, const size_t start, const size_t end,
1577 const size_t l, const size_t r) const
1578 {
1579 if (r < start or end < l)
1580 return T();
1581
1582 if (l <= start and end <= r)
1583 return tree(node).sum;
1584
1585 push_down(node);
1586 const size_t mid = start + (end - start) / 2;
1587 return query_sum_impl(2 * node, start, mid, l, r) +
1588 query_sum_impl(2 * node + 1, mid + 1, end, l, r);
1589 }
1590
1591 T query_max_impl(size_t node, const size_t start, const size_t end,
1592 const size_t l, const size_t r) const
1593 {
1594 if (r < start or end < l)
1595 return NEG_INF;
1596
1597 if (l <= start and end <= r)
1598 return tree(node).max_val;
1599
1600 push_down(node);
1601 const size_t mid = start + (end - start) / 2;
1602 return std::max(query_max_impl(2 * node, start, mid, l, r),
1603 query_max_impl(2 * node + 1, mid + 1, end, l, r));
1604 }
1605
1606 T query_min_impl(size_t node, const size_t start, const size_t end,
1607 const size_t l, const size_t r) const
1608 {
1609 if (r < start or end < l)
1610 return INF;
1611
1612 if (l <= start and end <= r)
1613 return tree(node).min_val;
1614
1615 push_down(node);
1616 const size_t mid = start + (end - start) / 2;
1617 return std::min(query_min_impl(2 * node, start, mid, l, r),
1618 query_min_impl(2 * node + 1, mid + 1, end, l, r));
1619 }
1620
1621 template <class Getter>
1622 void fill_and_build(Getter getter)
1623 {
1624 fill_leaves_recursive(1, 0, n - 1, getter);
1625 build(1, 0, n - 1);
1626 }
1627
1628 template <class Getter>
1629 void fill_leaves_recursive(const size_t node, size_t start, size_t end,
1630 Getter & getter)
1631 {
1632 if (start == end)
1633 {
1634 init_leaf(node, getter(start));
1635 return;
1636 }
1637
1638 const size_t mid = start + (end - start) / 2;
1639 fill_leaves_recursive(2 * node, start, mid, getter);
1640 fill_leaves_recursive(2 * node + 1, mid + 1, end, getter);
1641 }
1642
1644 {
1645 Node def = {NEG_INF, NEG_INF, INF, INF, T(),
1646 0, 0, INF, NEG_INF};
1647 for (size_t i = 0; i < tree.size(); ++i)
1648 tree(i) = def;
1649 }
1650
1651 public:
1652 using Item_Type = T;
1653
1660 Segment_Tree_Beats(const size_t num, const T & init_val = T())
1661 : tree(Array<Node>::create(num == 0 ? 1 : 4 * num)), n(num)
1662 {
1664 if (n > 0)
1665 fill_and_build([&init_val](size_t) { return init_val; });
1666 }
1667
1673 Segment_Tree_Beats(std::initializer_list<T> il)
1674 : tree(Array<Node>::create(il.size() == 0 ? 1 : 4 * il.size())),
1675 n(il.size())
1676 {
1678 if (n > 0)
1679 {
1680 auto it = il.begin();
1681 fill_and_build([&it](size_t) { return *it++; });
1682 }
1683 }
1684
1691 : tree(Array<Node>::create(values.size() == 0 ? 1 : 4 * values.size())),
1692 n(values.size())
1693 {
1695 if (n > 0)
1696 fill_and_build([&values](size_t i) { return values(i); });
1697 }
1698
1704 Segment_Tree_Beats(const std::vector<T> & values)
1705 : tree(Array<Node>::create(values.size() == 0 ? 1 : 4 * values.size())),
1706 n(values.size())
1707 {
1709 if (n > 0)
1710 fill_and_build([&values](size_t i) { return values[i]; });
1711 }
1712
1719 : tree(Array<Node>::create(values.size() == 0 ? 1 : 4 * values.size())),
1720 n(values.size())
1721 {
1723 if (n > 0)
1724 {
1725 auto tmp = Array<T>::create(n);
1726 size_t i = 0;
1727 for (auto it = values.get_it(); it.has_curr(); it.next_ne())
1728 tmp(i++) = it.get_curr();
1729 fill_and_build([&tmp](size_t j) { return tmp(j); });
1730 }
1731 }
1732
1734
1743 std::is_nothrow_move_constructible_v<Array<Node>>)
1744 : tree(std::move(other.tree)), n(std::exchange(other.n, 0))
1745 {}
1746
1748
1755 std::is_nothrow_move_assignable_v<Array<Node>>)
1756 {
1757 swap(other);
1758 return *this;
1759 }
1760
1765 void chmin(const size_t l, const size_t r, const T & v)
1766 {
1768 << "Segment_Tree_Beats::chmin: r=" << r << " >= n=" << n;
1770 << "Segment_Tree_Beats::chmin: l=" << l << " > r=" << r;
1771
1772 chmin_impl(1, 0, n - 1, l, r, v);
1773 }
1774
1779 void chmax(const size_t l, const size_t r, const T & v)
1780 {
1782 << "Segment_Tree_Beats::chmax: r=" << r << " >= n=" << n;
1784 << "Segment_Tree_Beats::chmax: l=" << l << " > r=" << r;
1785
1786 chmax_impl(1, 0, n - 1, l, r, v);
1787 }
1788
1793 T query_sum(const size_t l, const size_t r) const
1794 {
1796 << "Segment_Tree_Beats::query_sum: r=" << r << " >= n=" << n;
1798 << "Segment_Tree_Beats::query_sum: l=" << l << " > r=" << r;
1799
1800 return query_sum_impl(1, 0, n - 1, l, r);
1801 }
1802
1807 T query_max(const size_t l, const size_t r) const
1808 {
1810 << "Segment_Tree_Beats::query_max: r=" << r << " >= n=" << n;
1812 << "Segment_Tree_Beats::query_max: l=" << l << " > r=" << r;
1813
1814 return query_max_impl(1, 0, n - 1, l, r);
1815 }
1816
1821 T query_min(const size_t l, const size_t r) const
1822 {
1824 << "Segment_Tree_Beats::query_min: r=" << r << " >= n=" << n;
1826 << "Segment_Tree_Beats::query_min: l=" << l << " > r=" << r;
1827
1828 return query_min_impl(1, 0, n - 1, l, r);
1829 }
1830
1832 T get(const size_t i) const
1833 {
1834 return query_sum(i, i);
1835 }
1836
1838 [[nodiscard]] constexpr size_t size() const noexcept { return n; }
1839
1841 [[nodiscard]] constexpr bool is_empty() const noexcept { return n == 0; }
1842
1845 {
1846 auto ret = Array<T>::create(n);
1847 for (size_t i = 0; i < n; ++i)
1848 ret(i) = get(i);
1849 return ret;
1850 }
1851
1853 void swap(Segment_Tree_Beats & other) noexcept
1854 {
1855 tree.swap(other.tree);
1856 std::swap(n, other.n);
1857 }
1858 };
1859
1860} // end namespace Aleph
1861
1862# endif /* TPL_SEGMENT_TREE_H */
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
Exception handling system with formatted messages for Aleph-w.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
Definition ah-errors.H:727
Standard functor implementations and comparison objects.
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
void swap(Array &s) noexcept
Swap this with s
Definition tpl_array.H:313
static Array create(const size_t n)
Create an array with n logical elements.
Definition tpl_array.H:245
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
Lazy segment tree with range update and range query.
void build(size_t node, const size_t start, const size_t end)
void point_update(const size_t i, const lazy_type &delta)
Point update: apply delta to a[i].
Gen_Lazy_Segment_Tree(const Array< value_type > &values, Policy p=Policy())
Construct from an Array<value_type>.
void set_impl(size_t node, const size_t start, const size_t end, const size_t idx, const value_type &val)
Gen_Lazy_Segment_Tree(const std::vector< value_type > &values, Policy p=Policy())
Construct from a std::vector.
Gen_Lazy_Segment_Tree & operator=(const Gen_Lazy_Segment_Tree &)=default
constexpr size_t size() const noexcept
Number of logical elements.
void update_impl(size_t node, const size_t start, const size_t end, const size_t l, const size_t r, const lazy_type &val)
void fill_and_build(Getter getter)
typename Policy::value_type value_type
Gen_Lazy_Segment_Tree & operator=(Gen_Lazy_Segment_Tree &&other) noexcept(noexcept(swap(other)))
Exchange this tree with another, which is left with the previous elements of this one.
value_type query(const size_t l, const size_t r) const
Range query over [l, r].
constexpr bool is_empty() const noexcept
True if the tree contains no elements.
Array< value_type > values() const
Reconstruct all original values into an Array.
Gen_Lazy_Segment_Tree(const DynList< value_type > &values, Policy p=Policy())
Construct from a DynList.
void update(const size_t l, const size_t r, const lazy_type &val)
Range update: apply val to every a[i] with l <= i <= r.
void set(const size_t i, const value_type &val)
Set a[i] = val.
value_type query_impl(size_t node, const size_t start, const size_t end, const size_t l, const size_t r) const
typename Policy::lazy_type lazy_type
Gen_Lazy_Segment_Tree(std::initializer_list< value_type > il, Policy p=Policy())
Construct from an initializer list.
Gen_Lazy_Segment_Tree(Gen_Lazy_Segment_Tree &&other) noexcept(std::is_nothrow_move_constructible_v< Array< value_type > > and std::is_nothrow_move_constructible_v< Array< lazy_type > > and std::is_nothrow_move_constructible_v< Policy >)
Take the elements of another tree, which is left empty.
Gen_Lazy_Segment_Tree(const size_t num, const value_type &init_val=value_type(), Policy p=Policy())
Construct with num elements, all equal to init_val.
Gen_Lazy_Segment_Tree(const Gen_Lazy_Segment_Tree &)=default
void fill_leaves_recursive(size_t node, size_t start, size_t end, Getter &getter)
void push_down(size_t node, const size_t start, const size_t end) const
value_type get(const size_t i) const
Retrieve the value a[i].
void swap(Gen_Lazy_Segment_Tree &other) noexcept(std::is_nothrow_swappable_v< Policy >)
Swap this tree with other in O(1).
Segment tree over an arbitrary associative binary operation.
void set(const size_t i, const T &val)
Set a[i] = val.
void update_impl(size_t node, const size_t start, const size_t end, const size_t idx, const T &delta)
T query(const size_t l, const size_t r) const
Range query over [l, r].
Gen_Segment_Tree(const size_t num, const T &init_val, const T &id, Op oper=Op())
Construct a segment tree with num elements, all equal to init_val.
Gen_Segment_Tree(const Array< T > &values, const T &id, Op oper=Op())
Construct from an Array<T>.
Gen_Segment_Tree & operator=(const Gen_Segment_Tree &)=default
void set_impl(size_t node, const size_t start, const size_t end, const size_t idx, const T &val)
Gen_Segment_Tree(const DynList< T > &values, const T &id, Op oper=Op())
Construct from a DynList<T>.
void swap_identity_and_op(Gen_Segment_Tree &other)
void fill_from_aleph_it(AlephIt it)
void fill_leaves_recursive(size_t node, size_t start, size_t end, Getter &getter)
T query_impl(size_t node, const size_t start, const size_t end, const size_t l, const size_t r) const
Gen_Segment_Tree(std::initializer_list< T > il, const T &id, Op oper=Op())
Construct from an initializer list.
Gen_Segment_Tree(const Gen_Segment_Tree &)=default
void fill_and_build(Getter getter)
constexpr bool is_empty() const noexcept
True if the tree contains no elements.
Gen_Segment_Tree & operator=(Gen_Segment_Tree &&other) noexcept(noexcept(swap(other)))
Exchange this tree with another, which is left with the previous elements of this one.
Array< T > values() const
Reconstruct all original values into an Array.
T get(const size_t i) const
Retrieve the value a[i].
void update(const size_t i, const T &delta)
Point update: a[i] = Op(a[i], delta).
void swap(Gen_Segment_Tree &other) noexcept(std::is_nothrow_swappable_v< T > and std::is_nothrow_swappable_v< Op >)
Swap this tree with other in O(1).
constexpr size_t size() const noexcept
Number of logical elements.
void build(size_t node, const size_t start, const size_t end)
Gen_Segment_Tree(const std::vector< T > &values, const T &id, Op oper=Op())
Construct from a std::vector<T>.
Gen_Segment_Tree(Gen_Segment_Tree &&other) noexcept(std::is_nothrow_move_constructible_v< Array< T > > and std::is_nothrow_move_constructible_v< T > and std::is_nothrow_move_constructible_v< Op >)
Take the elements of another tree, which is left empty.
Ji Driver's Segment Tree (Segment Tree Beats).
void chmax_impl(size_t node, const size_t start, const size_t end, const size_t l, const size_t r, T v)
void chmax(const size_t l, const size_t r, const T &v)
Range chmax: for each i in [l, r], set a[i] = max(a[i], v).
constexpr size_t size() const noexcept
Number of logical elements.
T query_min_impl(size_t node, const size_t start, const size_t end, const size_t l, const size_t r) const
T query_sum(const size_t l, const size_t r) const
Range sum query over [l, r].
T get(const size_t i) const
Retrieve the value at position i.
void build(size_t node, size_t start, size_t end)
T query_max_impl(size_t node, const size_t start, const size_t end, const size_t l, const size_t r) const
T query_max(const size_t l, const size_t r) const
Range max query over [l, r].
void push_chmin_to_child(size_t child, T v) const
Segment_Tree_Beats(const Array< T > &values)
Construct from an Array<T>.
void fill_and_build(Getter getter)
Segment_Tree_Beats(const size_t num, const T &init_val=T())
Construct with num elements, all equal to init_val.
Segment_Tree_Beats(const Segment_Tree_Beats &)=default
Segment_Tree_Beats(const std::vector< T > &values)
Construct from a std::vector<T>.
Segment_Tree_Beats & operator=(const Segment_Tree_Beats &)=default
void push_chmax_to_child(size_t child, T v) const
void chmin(const size_t l, const size_t r, const T &v)
Range chmin: for each i in [l, r], set a[i] = min(a[i], v).
void push_down(size_t node) const
void chmin_impl(size_t node, const size_t start, const size_t end, const size_t l, const size_t r, T v)
Array< T > values() const
Reconstruct all values into an Array.
T query_min(const size_t l, const size_t r) const
Range min query over [l, r].
void swap(Segment_Tree_Beats &other) noexcept
Swap this tree with other in O(1).
constexpr bool is_empty() const noexcept
True if the tree contains no elements.
void pull_up(size_t node) const
void fill_leaves_recursive(const size_t node, size_t start, size_t end, Getter &getter)
Segment_Tree_Beats & operator=(Segment_Tree_Beats &&other) noexcept(std::is_nothrow_move_assignable_v< Array< Node > >)
Exchange this tree with another, which is left with the previous elements of this one.
T query_sum_impl(size_t node, const size_t start, const size_t end, const size_t l, const size_t r) const
void init_leaf(size_t node, const T &val)
Segment_Tree_Beats(std::initializer_list< T > il)
Construct from an initializer list.
Segment_Tree_Beats(Segment_Tree_Beats &&other) noexcept(std::is_nothrow_move_constructible_v< Array< Node > >)
Take the elements of another tree, which is left empty.
Segment_Tree_Beats(const DynList< T > &values)
Construct from a DynList<T>.
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
A binary functor closed over T.
Binary operation compatible with Segment Tree queries.
Policy concept for lazy segment tree.
pair< size_t, string > P
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_max_function > > max(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4188
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
and
Check uniqueness with explicit hash + equality functors.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
STL namespace.
Range add + range max policy.
static constexpr T compose(const T &old_lazy, const T &new_lazy) noexcept
static constexpr T lazy_identity() noexcept
constexpr T value_identity() const noexcept
static constexpr T combine(const T &a, const T &b) noexcept
static constexpr T apply(const T &val, const T &delta, size_t) noexcept
Range add + range min policy.
static constexpr T lazy_identity() noexcept
static constexpr T compose(const T &old_lazy, const T &new_lazy) noexcept
constexpr T value_identity() const noexcept
static constexpr T apply(const T &val, const T &delta, size_t) noexcept
static constexpr T combine(const T &a, const T &b) noexcept
Range add + range sum policy.
static constexpr T value_identity() noexcept
static constexpr T combine(const T &a, const T &b) noexcept
static constexpr T apply(const T &val, const T &delta, size_t count) noexcept
static constexpr T lazy_identity() noexcept
static constexpr T compose(const T &old_lazy, const T &new_lazy) noexcept
Range assign + range sum policy.
static constexpr T combine(const T &a, const T &b) noexcept
static constexpr T value_identity() noexcept
static constexpr lazy_type lazy_identity() noexcept
std::pair< bool, T > lazy_type
static constexpr T apply(const T &val, const lazy_type &lz, size_t count) noexcept
static constexpr lazy_type compose(const lazy_type &old_lz, const lazy_type &new_lz) noexcept
Lazy segment tree for range add + range max.
Lazy segment tree for range add + range min.
Lazy segment tree for range add + range sum.
Max Segment Tree for range maximum queries with point updates.
Max_Segment_Tree(const size_t num, const T &init_val=std::numeric_limits< T >::lowest())
Construct with num elements, all equal to init_val.
Max_Segment_Tree(const DynList< T > &values)
Construct from a DynList<T>.
Max_Segment_Tree(const std::vector< T > &values)
Construct from a std::vector<T>.
Max_Segment_Tree(std::initializer_list< T > il)
Construct from an initializer list.
Max_Segment_Tree(const Array< T > &values)
Construct from an Array<T>.
Min Segment Tree for range minimum queries with point updates.
Min_Segment_Tree(const size_t num, const T &init_val=std::numeric_limits< T >::max())
Construct with num elements, all equal to init_val.
Min_Segment_Tree(const std::vector< T > &values)
Construct from a std::vector<T>.
Min_Segment_Tree(const DynList< T > &values)
Construct from a DynList<T>.
Min_Segment_Tree(const Array< T > &values)
Construct from an Array<T>.
Min_Segment_Tree(std::initializer_list< T > il)
Construct from an initializer list.
Sum Segment Tree for range sum queries with point updates.
Sum_Segment_Tree(const DynList< T > &values)
Construct from a DynList<T>.
Sum_Segment_Tree(const size_t num, const T &init_val=T())
Construct with num elements, all equal to init_val.
Sum_Segment_Tree(const std::vector< T > &values)
Construct from a std::vector<T>.
Sum_Segment_Tree(const Array< T > &values)
Construct from an Array<T>.
Sum_Segment_Tree(std::initializer_list< T > il)
Construct from an initializer list.
Gsl_Compatible_Rng r
Dynamic array container with automatic resizing.
Alias for htlist.H (DynList implementation).
Sparse Table for static range queries in O(1).
DynList< int > l