@@ -87,6 +87,7 @@ They will be covered in [platinum](/plat/seg-ext).
87
87
``` cpp
88
88
#include < algorithm>
89
89
#include < iostream>
90
+ #include < limits>
90
91
#include < vector>
91
92
92
93
using std::cout;
@@ -96,7 +97,7 @@ using std::vector;
96
97
/* * A data structure that can answer point update & range minimum queries. */
97
98
template <class T > class MinSegmentTree {
98
99
private :
99
- const T DEFAULT = 1e18 ; // Will overflow if T is an int
100
+ const T DEFAULT = std :: numeric_limits < T >(). max ();
100
101
101
102
vector < T > segtree ;
102
103
int len ;
@@ -115,12 +116,12 @@ template <class T> class MinSegmentTree {
115
116
116
117
/* * @return the minimum element in the range [start, end) */
117
118
T range_min (int start, int end) {
118
- T sum = DEFAULT ;
119
+ T min = DEFAULT ;
119
120
for (start += len , end += len ; start < end ; start /= 2 , end /= 2 ) {
120
- if (start % 2 == 1) { sum = std ::min (sum , segtree [start ++ ]); }
121
- if (end % 2 == 1) { sum = std ::min (sum , segtree [-- end ]); }
121
+ if (start % 2 == 1) { min = std ::min (min , segtree [start ++ ]); }
122
+ if (end % 2 == 1) { min = std ::min (min , segtree [-- end ]); }
122
123
}
123
- return sum ;
124
+ return min ;
124
125
}
125
126
};
126
127
@@ -283,6 +284,7 @@ using std::cout;
283
284
using std ::endl ;
284
285
using std ::vector ;
285
286
287
+ // BeginCodeSnip{Segment Tree}
286
288
template < class T > class SumSegmentTree {
287
289
private:
288
290
const T DEFAULT = 0 ;
@@ -310,6 +312,7 @@ template <class T> class SumSegmentTree {
310
312
return sum ;
311
313
}
312
314
};
315
+ // EndCodeSnip
313
316
314
317
int main () {
315
318
int arr_len;
@@ -406,6 +409,7 @@ Compared to the previous problem, all we need to change is the way we aggregate
406
409
values (from ` min()` to '+'), and change the initial value to 0.
407
410
408
411
` ` ` py
412
+ # BeginCodeSnip{Segment Tree}
409
413
class SumSegmentTree:
410
414
def __init__ (self , len_ : int ):
411
415
self .len = len_
@@ -436,6 +440,9 @@ class SumSegmentTree:
436
440
return total
437
441
438
442
443
+ # EndCodeSnip
444
+
445
+
439
446
arr_len , query_num = map (int , input ().split ())
440
447
arr = list (map (int , input ().split ()))
441
448
0 commit comments