CP-Algorithms Library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub cp-algorithms/cp-algorithms-aux

:heavy_check_mark: cp-algo/structures/fenwick_set.hpp

Depends on

Verified with

Code

#ifndef CP_ALGO_STRUCTURES_FENWICK_SET_HPP
#define CP_ALGO_STRUCTURES_FENWICK_SET_HPP
#include "fenwick.hpp"
#include "bit_array.hpp"
namespace cp_algo::structures {
    template<size_t maxc, typename Uint = uint64_t>
    using popcount_array = std::array<int, maxc / bit_width<Uint> + 1>;
    // fenwick-based set for [0, maxc)
    template<size_t maxc, typename Uint = uint64_t>
    struct fenwick_set: fenwick<int, popcount_array<maxc, Uint>> {
        using Base = fenwick<int, popcount_array<maxc, Uint>>;
        static constexpr size_t word = bit_width<Uint>;
        size_t sz = 0;
        bit_array<maxc, Uint> bits;

        fenwick_set(): Base(popcount_array<maxc, Uint>{}) {}
        fenwick_set(auto &&range): fenwick_set() {
            for(auto x: range) {
                Base::data[x / word + 1] += 1;
                if(!bits.test(x)) {
                    sz++;
                    bits.flip(x);
                }
            }
            Base::to_prefix_folds();
        }
        void insert(size_t x) {
            if(bits.test(x)) return;
            Base::update(x / word, 1);
            bits.flip(x);
            sz++;
        }
        void erase(size_t x) {
            if(!bits.test(x)) return;
            Base::update(x / word, -1);
            bits.flip(x);
            sz--;
        }
        size_t order_of_key(size_t x) const {
            return Base::prefix_fold(x / word) + order_of_bit(bits.word(x / word), x % word);
        }
        size_t find_by_order(size_t order) const {
            if(order >= sz) {
                return -1;
            }
            auto [x, pref] = Base::prefix_lower_bound((int)order);
            return x * word + kth_set_bit(bits.word(x), order - pref);
        }
        size_t lower_bound(size_t x) const {
            if(bits.test(x)) {return x;}
            auto order = order_of_key(x);
            return order < sz ? find_by_order(order) : -1;
        }
        size_t pre_upper_bound(size_t x) const {
            if(bits.test(x)) {return x;}
            auto order = order_of_key(x);
            return order ? find_by_order(order - 1) : -1;
        }
    };
}
#endif // CP_ALGO_STRUCTURES_FENWICK_SET_HPP
#line 1 "cp-algo/structures/fenwick_set.hpp"


#line 1 "cp-algo/structures/fenwick.hpp"


#include <cassert>
#include <vector>
namespace cp_algo::structures {
    template <typename Op>
    struct inverse_op {};

    template <typename T>
    struct inverse_op<std::plus<T>> {
        static T apply(T const& a, T const& b) {
            return a - b;
        }
    };

    template <typename T>
    struct inverse_op<std::multiplies<T>> {
        static T apply(T const& a, T const& b) {
            return a / b;
        }
    };

    template<typename T, std::ranges::range Container = std::vector<T>, typename Op = std::plus<T>>
    struct fenwick {
        Op op;
        size_t n;
        Container data;

        fenwick(auto &&range, Op &&op = Op{}): op(std::move(op)) {
            assign(std::move(range));
        }
        void to_prefix_folds() {
            for(size_t i = 1; i < n; i++) {
                if(i + (i & -i) <= n) {
                    data[i + (i & -i)] = op(data[i + (i & -i)], data[i]);
                }
            }
        }
        void assign(auto &&range) {
            n = size(range) - 1;
            data = std::move(range);
            to_prefix_folds();
        }
        void update(size_t x, T const& v) {
            for(++x; x <= n; x += x & -x) {
                data[x] = op(data[x], v);
            }
        }
        // fold of [0, r)
        T prefix_fold(size_t r) const {
            assert(r <= n);
            T res = {};
            for(; r; r -= r & -r) {
                res = op(res, data[r]);
            }
            return res;
        }
        // fold of [l, r)
        T range_fold(size_t l, size_t r) const {
            return inverse_op<Op>::apply(prefix_fold(r), prefix_fold(l));
        }
        // Last x s.t. prefix_fold(x) <= k
        // Assumes prefix_fold is monotonic
        // returns [x, prefix_fold(x)]
        auto prefix_lower_bound(T k) const {
            size_t x = 0;
            T pref = {};
            for(size_t i = std::bit_floor(n); i; i /= 2) {
                if(x + i <= n && op(pref, data[x + i]) <= k) {
                    pref = op(pref, data[x + i]);
                    x += i;
                }
            }
            return std::pair{x, pref};
        }
    };

    template<std::ranges::range Container, typename Op>
    fenwick(Container&&, Op&&) -> fenwick<std::ranges::range_value_t<Container>, Container, Op>;
    template<std::ranges::range Container>
    fenwick(Container&&) -> fenwick<std::ranges::range_value_t<Container>, Container>;

    auto maxer = [](auto const& a, auto const& b) {
        return std::max(a, b);
    };
    template<typename T, std::ranges::range Container = std::vector<T>>
    struct fenwick_max: fenwick<T, Container, decltype(maxer)> {
        using fenwick<T, Container, decltype(maxer)>::fenwick;
    };
    template<std::ranges::range Container>
    fenwick_max(Container&&) -> fenwick_max<std::ranges::range_value_t<Container>, Container>;

}

#line 1 "cp-algo/structures/bit_array.hpp"


#line 1 "cp-algo/util/bit.hpp"


#include <immintrin.h>
#include <cstdint>
#include <array>
#include <bit>
namespace cp_algo {
    template<typename Uint>
    constexpr size_t bit_width = sizeof(Uint) * 8;

    size_t order_of_bit(auto x, size_t k) {
        return k ? std::popcount(x << (bit_width<decltype(x)> - k)) : 0;
    }
    [[gnu::target("bmi2")]]
    size_t kth_set_bit(uint64_t x, size_t k) {
        return std::countr_zero(_pdep_u64(1ULL << k, x));
    }
    template<int fl = 0>
    void with_bit_floor(size_t n, auto &&callback) {
        if constexpr (fl >= 63) {
            return;
        } else if (n >> (fl + 1)) {
            with_bit_floor<fl + 1>(n, callback);
        } else {
            callback.template operator()<1ULL << fl>();
        }
    }
}

#line 4 "cp-algo/structures/bit_array.hpp"
namespace cp_algo::structures {
    template<size_t N, typename Uint = uint64_t>
    struct bit_array {
        static constexpr size_t width = bit_width<Uint>;
        static constexpr size_t blocks = N / width + 1;
        std::array<Uint, blocks> data = {};

        uint64_t word(size_t x) const {
            return data[x];
        }
        void set(size_t x) {
            data[x / width] |= 1ULL << (x % width);
        }
        void flip(size_t x) {
            data[x / width] ^= 1ULL << (x % width);
        }
        bool test(size_t x) const {
            return (data[x / width] >> (x % width)) & 1;
        }
        bool operator[](size_t x) const {
            return test(x);
        }
    };
}

#line 5 "cp-algo/structures/fenwick_set.hpp"
namespace cp_algo::structures {
    template<size_t maxc, typename Uint = uint64_t>
    using popcount_array = std::array<int, maxc / bit_width<Uint> + 1>;
    // fenwick-based set for [0, maxc)
    template<size_t maxc, typename Uint = uint64_t>
    struct fenwick_set: fenwick<int, popcount_array<maxc, Uint>> {
        using Base = fenwick<int, popcount_array<maxc, Uint>>;
        static constexpr size_t word = bit_width<Uint>;
        size_t sz = 0;
        bit_array<maxc, Uint> bits;

        fenwick_set(): Base(popcount_array<maxc, Uint>{}) {}
        fenwick_set(auto &&range): fenwick_set() {
            for(auto x: range) {
                Base::data[x / word + 1] += 1;
                if(!bits.test(x)) {
                    sz++;
                    bits.flip(x);
                }
            }
            Base::to_prefix_folds();
        }
        void insert(size_t x) {
            if(bits.test(x)) return;
            Base::update(x / word, 1);
            bits.flip(x);
            sz++;
        }
        void erase(size_t x) {
            if(!bits.test(x)) return;
            Base::update(x / word, -1);
            bits.flip(x);
            sz--;
        }
        size_t order_of_key(size_t x) const {
            return Base::prefix_fold(x / word) + order_of_bit(bits.word(x / word), x % word);
        }
        size_t find_by_order(size_t order) const {
            if(order >= sz) {
                return -1;
            }
            auto [x, pref] = Base::prefix_lower_bound((int)order);
            return x * word + kth_set_bit(bits.word(x), order - pref);
        }
        size_t lower_bound(size_t x) const {
            if(bits.test(x)) {return x;}
            auto order = order_of_key(x);
            return order < sz ? find_by_order(order) : -1;
        }
        size_t pre_upper_bound(size_t x) const {
            if(bits.test(x)) {return x;}
            auto order = order_of_key(x);
            return order ? find_by_order(order - 1) : -1;
        }
    };
}

Back to top page