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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#pragma once


#include <cassert><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <algorithm><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <iostream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <vector><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <type_traits><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <concepts><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <ranges><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <bit><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.


#include "snippet/aliases.hpp"

#include "internal/dev_env.hpp"
#include "internal/types.hpp"
#include "internal/iterator.hpp"
#include "internal/point_reference.hpp"
#include "internal/range_reference.hpp"

#include "numeric/bit.hpp"
#include "numeric/arithmetic.hpp"

#include "algebraic/internal/concepts.hpp"

#include "action/base.hpp"
#include "action/helpers.hpp"


namespace uni {

namespace internal {

namespace lazy_segment_tree_impl {

// Thanks to: atcoder::lazy_segtree
template<actions::internal::full_action Action>
    requires
        algebraic::internal::monoid<typename Action::operand> &&
        algebraic::internal::monoid<typename Action::operation>
struct core {
    using size_type = internal::size_t;

    using action = Action;
    using operand = typename Action::operand;
    using operation = typename Action::operation;

 private:
    size_type _n = 0, _size = 0, _depth = 0;
    std::valarray<size_type> _lengths;
    std::valarray<operand> _values;
    std::valarray<operation> _lazy;


    inline void _pull(const size_type p) noexcept(NO_EXCEPT) {
        this->_values[p] = this->_values[p << 1] + this->_values[p << 1 | 1];
    }

    inline void _all_apply(const size_type p, const operation& f) noexcept(NO_EXCEPT) {
        this->_values[p] = action::mapping(action::power(f, this->_lengths[p]), this->_values[p]);
        if(p < this->_size) this->_lazy[p] = f + this->_lazy[p];
    }

    inline void _push(const size_type p) noexcept(NO_EXCEPT) {
        this->_all_apply(p << 1, this->_lazy[p]);
        this->_all_apply(p << 1 | 1, this->_lazy[p]);
        this->_lazy[p] = operation{};
    }

    inline void _init() noexcept(NO_EXCEPT) {
        REPD(p, 1, this->_size) {
            this->_lengths[p] = this->_lengths[p << 1] + this->_lengths[p << 1 | 1];
            this->_pull(p);
        }
    }

  public:
    core() noexcept = default;

    explicit core(const size_type n) noexcept(NO_EXCEPT)
      : _n(n), _size(std::bit_ceil(uni::to_unsigned(n))), _depth(std::countr_zero(uni::to_unsigned(this->_size))),
        _lengths(this->_size << 1), _values(this->_size << 1), _lazy(this->_size)
    {}


    inline size_type size() const noexcept(NO_EXCEPT) { return this->_n; }
    inline size_type allocated() const noexcept(NO_EXCEPT) { return this->_values.size(); }
    inline size_type depth() const noexcept(NO_EXCEPT) { return this->_depth; }


    inline operand fold_all() const noexcept(NO_EXCEPT) { return this->_values[1]; }


    inline void fill( const operand& v = operand()) noexcept(NO_EXCEPT) {
        REP(p, 0, this->_n) {
            this->_lengths[this->_size + p] = 1, this->_values[this->_size + p] = v;
        }
        this->_init();
    }

    template<std::input_iterator I, std::sentinel_for<I> S>
    inline void assign(I first, S last) noexcept(NO_EXCEPT) {
        if constexpr(std::sized_sentinel_for<operand, I>) {
            assert(std::ranges::distance(first, last) == this->_n);
        }
        size_type p = 0;
        for(auto itr=first; itr!=last; ++itr, ++p) {
            this->_lengths[this->_size + p] = 1, this->_values[this->_size + p] = static_cast<operand>(*itr);
        }
        this->_init();
    }


    inline void set(size_type p, const operand& x) noexcept(NO_EXCEPT) {
        p += this->_size;
        FORD(i, 1, this->_depth) this->_push(p >> i);
        this->_values[p] = x;
        FOR(i, 1, this->_depth) this->_pull(p >> i);
    }

    inline void add(size_type p, const operand& x) noexcept(NO_EXCEPT) {
        p += this->_size;
        FORD(i, 1, this->_depth) this->_push(p >> i);
        this->_values[p] = this->_values[p] + x;
        FOR(i, 1, this->_depth) this->_pull(p >> i);
    }

    inline operand get(size_type p) noexcept(NO_EXCEPT) {
        p += this->_size;
        FORD(i, 1, this->_depth) this->_push(p >> i);
        return this->_values[p];
    }

    inline operand fold(size_type l, size_type r) noexcept(NO_EXCEPT) {
        if(l == r) return {};

        l += this->_size;
        r += this->_size;

        FORD(i, 1, this->_depth) {
            if(((l >> i) << i) != l) this->_push(l >> i);
            if(((r >> i) << i) != r) this->_push((r - 1) >> i);
        }

        operand sml = operand{}, smr = operand{};
        while(l < r) {
            if(l & 1) sml = sml + this->_values[l++];
            if(r & 1) smr = this->_values[--r] + smr;
            l >>= 1;
            r >>= 1;
        }

        return sml + smr;
    }


    inline void apply(size_type p, const operation& f) noexcept(NO_EXCEPT) {
        p += this->_size;
        FORD(i, 1, this->_depth) this->_push(p >> i);
        this->_values[p] = action::mapping(action::power(f, this->_lengths[p]), this->_values[p]);
        FOR(i, 1, this->_depth) this->_pull(p >> i);
    }

    inline void apply(size_type l, size_type r, const operation& f) noexcept(NO_EXCEPT) {
        if(l == r) return;

        l += this->_size;
        r += this->_size;

        FORD(i, 1, this->_depth) {
            if(((l >> i) << i) != l) this->_push(l >> i);
            if(((r >> i) << i) != r) this->_push((r - 1) >> i);
        }

        {
            size_type l2 = l, r2 = r;
            while(l < r) {
                if(l & 1) this->_all_apply(l++, f);
                if(r & 1) this->_all_apply(--r, f);
                l >>= 1;
                r >>= 1;
            }
            l = l2;
            r = r2;
        }

        FOR(i, 1, this->_depth) {
            if(((l >> i) << i) != l) this->_pull(l >> i);
            if(((r >> i) << i) != r) this->_pull((r - 1) >> i);
        }
    }


    template<class F>
    inline size_type max_right(size_type l, F&& f) noexcept(NO_EXCEPT) {
        assert(0 <= l && l <= _n);<--- Unsigned positive
        assert(f(operand{}));
        if(l == _n) return _n;
        l += this->_size;
        FORD(i, 1, this->_depth) this->_push(l >> i);
        operand sm;
        do {
            while(l % 2 == 0) l >>= 1;
            if(!f(sm + this->_values[l])) {
                while(l < this->_size) {
                    this->_push(l);
                    l = (2 * l);
                    if(f(sm + this->_values[l])) {
                        sm = sm + this->_values[l];
                        l++;
                    }
                }
                return l - this->_size;
            }
            sm = sm + this->_values[l];
            l++;
        } while((l & -l) != l);
        return _n;
    }


    template<class F>
    inline size_type min_left(size_type r, F&& f) noexcept(NO_EXCEPT) {
        assert(0 <= r && r <= _n);<--- Unsigned positive
        assert(f(operand{}));
        if(r == 0) return 0;
        r += this->_size;
        FORD(i, 1, this->_depth) this->_push((r - 1) >> i);
        operand sm;
        do {
            r--;
            while(r > 1 && (r % 2)) r >>= 1;
            if(!f(this->_values[r] + sm)) {
                while(r < this->_size) {
                    this->_push(r);
                    r = (2 * r + 1);
                    if(f(this->_values[r] + sm)) {
                        sm = this->_values[r] + sm;
                        r--;
                    }
                }
                return r + 1 - this->_size;
            }
            sm = this->_values[r] + sm;
        } while((r & -r) != r);
        return 0;
    }
};


} // namespace lazy_segment_tree_impl

} // namespace internal


template<class T>
struct lazy_segment_tree : lazy_segment_tree<actions::make_full_t<T>> {
    using lazy_segment_tree<actions::make_full_t<T>>::lazy_segment_tree;
};


template<actions::internal::full_action Action>
    requires internal::available<internal::lazy_segment_tree_impl::core<Action>>
struct lazy_segment_tree<Action> {
    using action = Action;
    using operand = Action::operand;
    using operation = Action::operation;

  private:
    using core = internal::lazy_segment_tree_impl::core<action>;

    core _impl;

  public:
    using value_type = operand;
    using action_type = operation::value_type;

    using size_type = core::size_type;

    inline auto size() const noexcept(NO_EXCEPT) { return this->_impl.size(); }
    inline auto allocated() const noexcept(NO_EXCEPT) { return this->_impl.allocated(); }
    inline auto depth() const noexcept(NO_EXCEPT) { return this->_impl.depth(); }

  protected:
    inline size_type _positivize_index(const size_type p) const noexcept(NO_EXCEPT) {
        return p < 0 ? this->_impl.size() + p : p;
    }

  public:
    lazy_segment_tree() noexcept(NO_EXCEPT) : _impl() {}

    explicit lazy_segment_tree(const size_type n, const value_type& v = value_type()) noexcept(NO_EXCEPT) : _impl(n) { this->_impl.fill(v); }

    template<std::convertible_to<value_type> T>
    lazy_segment_tree(const std::initializer_list<T>& init_list) noexcept(NO_EXCEPT) : lazy_segment_tree(ALL(init_list)) {}

    template<std::input_iterator I, std::sized_sentinel_for<I> S>
    lazy_segment_tree(I first, S last) noexcept(NO_EXCEPT)
      : _impl(static_cast<size_type>(std::ranges::distance(first, last)))
    { this->assign(first, last); }

    template<std::ranges::input_range R>
    explicit lazy_segment_tree(R&& range) noexcept(NO_EXCEPT) : lazy_segment_tree(ALL(range)) {}

    template<std::convertible_to<value_type> T>
    inline auto& assign(const std::initializer_list<T>& init_list) noexcept(NO_EXCEPT)
    {
        return this->assign(ALL(init_list));
    }

    template<std::input_iterator I, std::sentinel_for<I> S>
    inline auto& assign(I first, S last) noexcept(NO_EXCEPT) {
        this->_impl.assign(first, last);
        return *this;
    }

    template<std::ranges::input_range R>
    inline auto& assign(R&& range) noexcept(NO_EXCEPT) { return this->assign(ALL(range)); }

    inline auto& fill( const value_type& v = value_type()) noexcept(NO_EXCEPT) {
        this->impl.fill(v);
        return *this;
    }

    bool empty() const noexcept(NO_EXCEPT) { return this->_impl.size() == 0; }

    struct point_reference : internal::point_reference<lazy_segment_tree> {
        point_reference(lazy_segment_tree *const super, const size_type p) noexcept(NO_EXCEPT)
          : internal::point_reference<lazy_segment_tree>(super, super->_positivize_index(p))
        {
            assert(0 <= this->_pos && this->_pos < this->_super->size());
        }

        operator value_type() noexcept(NO_EXCEPT) { return this->_super->get(this->_pos); }
        auto val() noexcept(NO_EXCEPT) { return this->_super->get(this->_pos); }

        inline auto& operator=(const value_type& v) noexcept(NO_EXCEPT) {
            this->_super->set(this->_pos, v);
            return *this;
        }

        inline auto& operator+=(const value_type& v) noexcept(NO_EXCEPT) {
            this->_super->add(this->_pos, v);
            return *this;
        }

        inline auto& operator*=(const action_type& v) noexcept(NO_EXCEPT) {
            this->_super->apply(this->_pos, v);
            return *this;
        }
    };

    struct range_reference : internal::range_reference<lazy_segment_tree> {
        range_reference(lazy_segment_tree *const super, const size_type l, const size_type r) noexcept(NO_EXCEPT)
          : internal::range_reference<lazy_segment_tree>(super, super->_positivize_index(l), super->_positivize_index(r))
        {
            assert(0 <= this->_begin && this->_begin <= this->_end && this->_end <= this->_super->size());
        }

        inline auto& operator*=(const action_type& v) noexcept(NO_EXCEPT) {
            this->_super->apply(this->_begin, this->_end, v);
            return *this;
        }

        inline auto fold() noexcept(NO_EXCEPT) {
            if(this->_begin == 0 && this->_end == this->_super->size()) return this->_super->fold();
            return this->_super->fold(this->_begin, this->_end);
        }
    };


    inline auto& set(size_type p, const value_type& v) noexcept(NO_EXCEPT) {
        p = this->_positivize_index(p), assert(0 <= p && p < this->_impl.size());
        this->_impl.set(p, v);
         return *this;
    }

    inline auto& add(size_type p, const value_type& v) noexcept(NO_EXCEPT) {
        p = this->_positivize_index(p), assert(0 <= p && p < this->_impl.size());
        this->_impl.add(p, v);
         return *this;
    }


    inline auto& apply(size_type l, size_type r, const action_type& v) noexcept(NO_EXCEPT) {
        l = this->_positivize_index(l), r = this->_positivize_index(r);
        assert(0 <= l && l <= r && r <= this->_impl.size());
        this->_impl.apply(l, r, v);
        return *this;
    }

    inline auto& apply(const size_type p, const action_type& v) noexcept(NO_EXCEPT) {
        this->apply(p, p + 1, v);
        return *this;
    }

    inline auto& apply(const action_type& v) noexcept(NO_EXCEPT) { this->apply(0, this->_impl.size(), v);  return *this; }


    inline auto get(size_type p) noexcept(NO_EXCEPT) {
        p = this->_positivize_index(p), assert(0 <= p && p < this->_impl.size());
        return this->_impl.get(p);
    }

    inline auto operator[](const size_type p) noexcept(NO_EXCEPT) { return point_reference(this, p); }
    inline auto operator()(const size_type l, const size_type r) noexcept(NO_EXCEPT) { return range_reference(this, l, r); }


    inline auto fold(size_type l, size_type r) noexcept(NO_EXCEPT) {
        l = this->_positivize_index(l), r = this->_positivize_index(r);
        assert(0 <= l && l <= r && r <= this->_impl.size());
        return this->_impl.fold(l, r);
    }
    inline auto fold() noexcept(NO_EXCEPT) { return this->_impl.fold_all(); }



    template<bool (*f)(value_type)>
    inline auto max_right(const size_type l) noexcept(NO_EXCEPT) {
        return this->max_right(l, [](operand x) { return f(x); });
    }

    template<class F>
    inline auto max_right(const size_type l, F&& f) noexcept(NO_EXCEPT) {
        return this->_impl.max_right(l, std::forward<F>(f));
    }


    template<bool (*f)(value_type)>
    inline auto min_left(const size_type r) noexcept(NO_EXCEPT) {
        return min_left(r, [](operand x) { return f(x); });
    }

    template<class F>
    inline auto min_left(const size_type r, F&& f) noexcept(NO_EXCEPT) {
        return this->_impl.min_left(r, std::forward<F>(f));
    }

    struct iterator;

  protected:
    using iterator_interface = internal::container_iterator_interface<value_type, lazy_segment_tree, iterator>;

  public:
    struct iterator : iterator_interface {
        using iterator_interface::iterator_interface;
    };

    inline auto begin() noexcept(NO_EXCEPT) { return iterator(this, 0); }
    inline auto end() noexcept(NO_EXCEPT) { return iterator(this, this->_impl.size()); }

    inline auto rbegin() noexcept(NO_EXCEPT) { return std::make_reverse_iterator(this->end()); }
    inline auto rend() noexcept(NO_EXCEPT) { return std::make_reverse_iterator(this->begin()); }
};


} // namespace uni