Skip to content

Commit 92234ea

Browse files
authored
Merge pull request #107 from hitonanode/adding_const_to_segtree_and_lazy_segtree
#106: make several methods of segtree and lazy_segtree const
2 parents 2b1437c + 060f9e6 commit 92234ea

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

atcoder/lazysegtree.hpp

+11-11
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ struct lazy_segtree {
4040
for (int i = 1; i <= log; i++) update(p >> i);
4141
}
4242

43-
S get(int p) {
43+
S get(int p) const {
4444
assert(0 <= p && p < _n);
4545
p += size;
4646
for (int i = log; i >= 1; i--) push(p >> i);
4747
return d[p];
4848
}
4949

50-
S prod(int l, int r) {
50+
S prod(int l, int r) const {
5151
assert(0 <= l && l <= r && r <= _n);
5252
if (l == r) return e();
5353

@@ -70,7 +70,7 @@ struct lazy_segtree {
7070
return op(sml, smr);
7171
}
7272

73-
S all_prod() { return d[1]; }
73+
S all_prod() const { return d[1]; }
7474

7575
void apply(int p, F f) {
7676
assert(0 <= p && p < _n);
@@ -109,10 +109,10 @@ struct lazy_segtree {
109109
}
110110
}
111111

112-
template <bool (*g)(S)> int max_right(int l) {
112+
template <bool (*g)(S)> int max_right(int l) const {
113113
return max_right(l, [](S x) { return g(x); });
114114
}
115-
template <class G> int max_right(int l, G g) {
115+
template <class G> int max_right(int l, G g) const {
116116
assert(0 <= l && l <= _n);
117117
assert(g(e()));
118118
if (l == _n) return _n;
@@ -138,10 +138,10 @@ struct lazy_segtree {
138138
return _n;
139139
}
140140

141-
template <bool (*g)(S)> int min_left(int r) {
141+
template <bool (*g)(S)> int min_left(int r) const {
142142
return min_left(r, [](S x) { return g(x); });
143143
}
144-
template <class G> int min_left(int r, G g) {
144+
template <class G> int min_left(int r, G g) const {
145145
assert(0 <= r && r <= _n);
146146
assert(g(e()));
147147
if (r == 0) return 0;
@@ -169,15 +169,15 @@ struct lazy_segtree {
169169

170170
private:
171171
int _n, size, log;
172-
std::vector<S> d;
173-
std::vector<F> lz;
172+
mutable std::vector<S> d;
173+
mutable std::vector<F> lz;
174174

175175
void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
176-
void all_apply(int k, F f) {
176+
void all_apply(int k, F f) const {
177177
d[k] = mapping(f, d[k]);
178178
if (k < size) lz[k] = composition(f, lz[k]);
179179
}
180-
void push(int k) {
180+
void push(int k) const {
181181
all_apply(2 * k, lz[k]);
182182
all_apply(2 * k + 1, lz[k]);
183183
lz[k] = id();

atcoder/segtree.hpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ template <class S, S (*op)(S, S), S (*e)()> struct segtree {
3030
for (int i = 1; i <= log; i++) update(p >> i);
3131
}
3232

33-
S get(int p) {
33+
S get(int p) const {
3434
assert(0 <= p && p < _n);
3535
return d[p + size];
3636
}
3737

38-
S prod(int l, int r) {
38+
S prod(int l, int r) const {
3939
assert(0 <= l && l <= r && r <= _n);
4040
S sml = e(), smr = e();
4141
l += size;
@@ -50,12 +50,12 @@ template <class S, S (*op)(S, S), S (*e)()> struct segtree {
5050
return op(sml, smr);
5151
}
5252

53-
S all_prod() { return d[1]; }
53+
S all_prod() const { return d[1]; }
5454

55-
template <bool (*f)(S)> int max_right(int l) {
55+
template <bool (*f)(S)> int max_right(int l) const {
5656
return max_right(l, [](S x) { return f(x); });
5757
}
58-
template <class F> int max_right(int l, F f) {
58+
template <class F> int max_right(int l, F f) const {
5959
assert(0 <= l && l <= _n);
6060
assert(f(e()));
6161
if (l == _n) return _n;
@@ -79,10 +79,10 @@ template <class S, S (*op)(S, S), S (*e)()> struct segtree {
7979
return _n;
8080
}
8181

82-
template <bool (*f)(S)> int min_left(int r) {
82+
template <bool (*f)(S)> int min_left(int r) const {
8383
return min_left(r, [](S x) { return f(x); });
8484
}
85-
template <class F> int min_left(int r, F f) {
85+
template <class F> int min_left(int r, F f) const {
8686
assert(0 <= r && r <= _n);
8787
assert(f(e()));
8888
if (r == 0) return 0;

0 commit comments

Comments
 (0)