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
#pragma once


#include <cassert><--- 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 <iterator><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <utility>
#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 "internal/dev_env.hpp"
#include "internal/concepts.hpp"
#include "internal/types.hpp"
#include "internal/iterator.hpp"
#include "internal/point_reference.hpp"
#include "internal/range_reference.hpp"
#include "internal/unconstructible.hpp"

#include "snippet/iterations.hpp"
#include "numeric/bit.hpp"

#include "action/base.hpp"
#include "algebraic/internal/concepts.hpp"


namespace uni {

namespace internal {

namespace fenwick_tree_impl {


// Thanks to: atcoder::fenwick_tree
template<algebraic::internal::monoid Operand>
struct core {
    using operand = Operand;
    using size_type = internal::size_t;

  private:
    size_type _n = 0, _bit_ceil = 0;
    std::vector<operand> _data;

    inline void _init() noexcept(NO_EXCEPT) {
        FOR(i, 1, this->_n) {
            size_type j = i + (i & -i);
            if(j <= this->_n) this->_data[j-1] = this->_data[j-1] + this->_data[i-1];
        }
    }

  public:
    core() noexcept(NO_EXCEPT) {}

    explicit core(const size_type n) noexcept(NO_EXCEPT)
      : _n(n), _bit_ceil(std::bit_ceil<std::make_unsigned_t<size_type>>(n)), _data(n, operand{})
    {}

    inline size_type size() const noexcept(NO_EXCEPT) { return this->_n; }<--- Parent function 'core::size'<--- Parent function 'core::size'


    template<std::input_iterator I, std::sentinel_for<I> S>
    inline void assign(I first, S last) noexcept(NO_EXCEPT) {<--- Parent function 'core::assign'<--- Parent function 'core::assign'
        if constexpr(std::sized_sentinel_for<S, I>) {
            assert(std::ranges::distance(first, last) == this->size());
        }
        for(size_type i = 0; first < last; ++i, ++first) this->_data[i] = *first;
        this->_init();
    }


    inline void add(size_type p, const operand& x) noexcept(NO_EXCEPT) {
        for(p++; p<=this->_n; p += p & -p) this->_data[p-1] = this->_data[p-1] + x;
    }

    inline void set(const size_type p, const operand& x) noexcept(NO_EXCEPT) {
        assert(this->get(p) == this->fold(p, p+1));
        this->add(p, x + -this->get(p));
    }

    inline operand fold(size_type r) const noexcept(NO_EXCEPT) {<--- Parent function 'core::fold'<--- Parent function 'core::fold'
        operand s = operand{};
        for(; r>0; r -= r & -r) s = s + this->_data[r-1];
        return s;
    }
    inline operand fold(size_type l, size_type r) const noexcept(NO_EXCEPT) {<--- Parent function 'core::fold'<--- Parent function 'core::fold'
        operand s = operand{};
        for(; l < r; r -= r & -r) s = s + this->_data[r-1];
        for(; r < l; l -= l & -l) s = s + -this->_data[l-1];
        return s;
    }

    inline operand get(size_type p) const noexcept(NO_EXCEPT) {<--- Parent function 'core::get'<--- Parent function 'core::get'
        return this->fold(p, p+1);
    }

    template<class F>
    inline size_type max_right(size_type l, F&& f) const noexcept(NO_EXCEPT)
        requires algebraic::internal::invertible<operand>
    {
        assert(0 <= l && l <= this->_n);<--- Unsigned positive
        assert(f(operand{}));
        if(l == this->_n) return this->_n;
        operand inv = -this->fold(l);
        size_type p = 0, q = this->_bit_ceil;
        for(size_type k=q; k>0; k >>= 1) {
            if(p+k <= this->_n and f(this->_data[p+k-1] + inv)) {
                inv = inv + this->_data[(p+=k)-1];
            }
        }
        return p;
    }

    template<class F> inline size_type min_left(size_type r, F&& f) const noexcept(NO_EXCEPT)
        requires algebraic::internal::invertible<operand>
    {
        assert(0 <= r && r <= this->_n);<--- Unsigned positive
        assert(f(operand{}));
        if(r == 0) return 0;
        operand acc = this->fold(r);
        size_type p = 0, q = std::bit_ceil<std::make_unsigned_t<size_type>>(r);
        for(size_type k=q; k>0; k >>= 1) {
            if(p+k < r and !f(acc + -this->_data[p+k-1])) {
                acc = acc + -this->_data[(p+=k)-1];
            }
        }
        if(p == 0 and f(acc)) return 0;
        return p + 1;
    }
};


} // namespace fenwick_tree_impl

} // namespace internal


template<class Value>
struct fenwick_tree : internal::unconstructible {};


template<algebraic::internal::monoid Monoid>
struct fenwick_tree<Monoid> : internal::fenwick_tree_impl::core<Monoid> {
    static_assert(algebraic::internal::commutative<Monoid>);

  private:
    using core = typename internal::fenwick_tree_impl::core<Monoid>;

    core _impl;

  public:
    using value_type = typename core::operand;
    using size_type = typename core::size_type;

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

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

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

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

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

    template<std::ranges::input_range R>
    explicit fenwick_tree(R&& range) noexcept(NO_EXCEPT) : fenwick_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) {<--- Derived function 'fenwick_tree::assign'<--- Derived function 'fenwick_tree < Monoid >::assign'
        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) {
        std::fill(this->data(), this->data() + this->_impl.size(), v);
        this->_init();
        return *this;
    }

    inline auto size() const noexcept(NO_EXCEPT) { return this->_impl.size(); }<--- Derived function 'fenwick_tree::size'<--- Derived function 'fenwick_tree < Monoid >::size'
    inline bool empty() const noexcept(NO_EXCEPT) { return this->_impl.size() == 0; }

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

        operator value_type() const noexcept(NO_EXCEPT) { return this->_super->get(this->_pos); }
        auto val() const 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;
        }
    };

    struct range_reference : internal::range_reference<fenwick_tree> {
        range_reference(fenwick_tree *const super, const size_type l, const size_type r) noexcept(NO_EXCEPT)
          : internal::range_reference<fenwick_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 fold() noexcept(NO_EXCEPT) {
            if(this->_begin == 0 and this->_end == this->_super->size()) return this->_super->fold();
            if(this->_begin == 0) return this->_super->fold(this->_end);
            return this->_super->fold(this->_begin, this->_end);
        }
    };


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

    inline auto& set(const size_type p, const value_type& x) noexcept(NO_EXCEPT)
        requires algebraic::internal::invertible<value_type>
    {
        assert(0 <= p && p < this->_impl.size());
        this->_impl.set(p, x);
         return *this;
    }

    inline value_type get(const size_type p) const noexcept(NO_EXCEPT)<--- Derived function 'fenwick_tree::get'<--- Derived function 'fenwick_tree < Monoid >::get'
        requires algebraic::internal::invertible<value_type>
    {
        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 const auto operator()(const size_type l, const size_type r) const noexcept(NO_EXCEPT) {
        return range_reference(this, l, r);
    }

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

    inline auto fold(const size_type l, const size_type r) const noexcept(NO_EXCEPT)<--- Derived function 'fenwick_tree::fold'<--- Derived function 'fenwick_tree < Monoid >::fold'
        requires algebraic::internal::invertible<value_type>
    {
        assert(0 <= l && l <= r && r <= this->_impl.size());
        return this->_impl.fold(l, r);
    }

    inline auto fold(const size_type r) const noexcept(NO_EXCEPT) {<--- Derived function 'fenwick_tree::fold'<--- Derived function 'fenwick_tree < Monoid >::fold'
        assert(0 <= r && r <= this->_impl.size());
        return this->_impl.fold(r);
    }

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

    struct iterator;

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

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

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

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


template<actions::internal::operatable_action Action>
struct fenwick_tree<Action> : fenwick_tree<typename Action::operand> {
    using fenwick_tree<typename Action::operand>::fenwick_tree;
};


} // namespace uni