shogo-library

競技プログラミング用の C++ ライブラリ

View the Project on GitHub shogo314/shogo-library

:heavy_check_mark: Python の range のような整数列と、その直積の Grid
(src/utility/range.hpp)

Python の range のような、等差数列を表す Range です。要素を実際には持たないので、長い列でもメモリを使いません。

for (long long i : Range(2, 10, 3)) { /* 2, 5, 8 */ }
Range r(10, 0, -3);       // 10, 7, 4, 1
r.size();                 // 4
r.sum();                  // 22
r.contains(4);            // true
r.index(4);               // 2
Range(10).product_xor();  // 0 ^ 1 ^ ... ^ 9 = 1

sum、max などのメンバ関数を持つので、コンテナに対する関数 の sum(r)、max(r) などから呼ぶと $O(1)$ で計算されます。

C++20 以降では Range は random_access_range、Grid と近傍列挙は forward_range として、標準の ranges アルゴリズムで使えます。Range の iterator は値を返し、自分の走査状態を持つので元の Range の寿命に依存しません。Grid と近傍列挙の iterator は元のオブジェクトを参照します。

Range・Grid はメンバが不変で代入できないため、views に接続するときは変数に受けてから使います。

Range r(10);
auto even = r | std::views::filter([](long long x) { return x % 2 == 0; });

メンバ関数

関数 説明 計算量
Range(stop) 0, 1, ..., stop-1 $O(1)$
Range(start, stop) start, start+1, ..., stop-1 $O(1)$
Range(start, stop, step) start, start+step, ... (stop の手前まで)。step は負でもよいが 0 は不可 $O(1)$
begin() / end() ランダムアクセスイテレータ $O(1)$
size() / empty() 要素数 (long long) / 空か $O(1)$
r[i] / at(i) i 番目の要素 (i は long long) $O(1)$
count(x) / contains(x) x の個数 (0 か 1) / x を含むか (step が 1 なら割り算をしない) $O(1)$
index(x) x の位置 (long long。含まなければ assert で落ちる。step が 1 なら割り算をしない) $O(1)$
find(x) x を指すイテレータ (なければ end()) $O(1)$
lower_bound(x) / upper_bound(x) x 以上 / x より大きい最初の要素 (step > 0 のみ) $O(1)$
sum() 総和 $O(1)$
min() / max() 最小値 / 最大値 (空なら assert で落ちる) $O(1)$
product_xor() 総 xor (要素は非負) $O(\log^2 \max)$
reversed() 逆順の Range $O(1)$
sorted() 昇順の Range $O(1)$
clipped(l, r) 要素のうち l 以上 r 未満のものの Range $O(1)$
slice(s) 位置を Range s で選んだ Range (Python の r[a:b:c]) $O(1)$
to_vector() std::vector<long long> にする $O(n)$
==、!= 要素の列として等しいか (Range(0, 5, 2) == Range(0, 6, 2)) $O(1)$
repr() "Range(start, stop, step)" (repr 用) $O(1)$

メンバ変数

変数 説明
start / stop / step コンストラクタに渡した値 (const なので読むだけ)

Grid

セルの値は保持せず、整数の行・列の範囲を表します。幾何の長方形は Rectangle を使います。

2 つの Range の直積 rows × cols です。(i, j) を i の昇順、同じ i では j の昇順 (行優先) に並べた列として扱います。グリッドの走査や範囲内の判定、頂点番号への変換に使います。

for (auto [i, j] : Grid(h, w)) { /* (0, 0), (0, 1), ..., (h-1, w-1) */ }
const Grid grid(h, w);
grid.contains(ni, nj);    // 0 <= ni < h かつ 0 <= nj < w か (inrect と同じ)
grid.index(i, j);         // i * w + j
grid[k];                  // index の逆 (k / w, k % w)
for (auto [ni, nj] : grid.neighbors4(i, j)) { /* 範囲内の上下左右 */ }
Grid({1, 4}, {10, 0, -5}) // {1, 2, 3} × {10, 5}
関数 説明 計算量
Grid(h, w) $[0, h) \times [0, w)$ $O(1)$
Grid(rows, cols) Range 2 つの直積。Grid({1, 4}, {0, 10, 2}) のように Range の初期化子を直接書ける $O(1)$
begin() / end() 前方向イテレータ。要素は std::pair<long long, long long> で、auto [i, j] で受け取れる $O(1)$
size() / empty() 要素数 (long long) / 空か $O(1)$
contains(i, j) / contains(p) (i, j) を含むか $O(1)$
index(i, j) / index(p) 行優先での位置 (含まなければ assert で落ちる) $O(1)$
r[k] / at(k) 行優先で k 番目の要素 $O(1)$
neighbors4(i, j) / neighbors4(p) (i, j) の上下左右のうち Grid に含まれるもの ((i, j) は含まれていること) $O(1)$
neighbors8(i, j) / neighbors8(p) (i, j) を囲む 8 つのうち Grid に含まれるもの $O(1)$
==、!= 要素の列として等しいか $O(1)$
repr() "Grid(Range(...), Range(...))" (repr 用) $O(1)$
rows / cols 行と列の Range (const なので読むだけ)  

neighbors4 / neighbors8 は範囲 for で回せるもの (size()、empty() もある) を返し、要素は Grid の順 (行優先) に並びます。隣は rows・cols の中で隣り合う要素なので、step が 1 でなければ i ± rows.step などになります。

その場で作った Grid(h, w).contains(i, j) は inrect(i, j, h, w) と同じ速さです。変数に入れた Grid でも、step が 1 なら割り算をしないので、ほぼ同じ速さになります。for (auto [i, j] : Grid(h, w)) は、ループの中身がごく軽いと 2 重ループより 3 割ほど遅くなります。neighbors4 を使ったグリッドの BFS も、dx、dy の配列と inrect で書いたものより 3 割ほど遅くなります。C++20 では std::ranges::forward_range と sized_range を満たします。ただし Range と同じく代入できないので、views に渡すときは一時オブジェクトではなく変数を渡します。

関連

Required by

Verified with

Code

#pragma once

/**
 * @file range.hpp
 * @brief Python の range のような整数列と、その直積の Grid
 */

#include <cassert>
#include <cstddef>
#include <iterator>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

struct Range {
   public:
    using value_type = long long;
    using const_reference = value_type;
    using size_type = long long;
    using difference_type = std::ptrdiff_t;

    const value_type start;
    const value_type stop;
    const value_type step;

   private:
    const value_type norm_stop;

    constexpr static value_type normalize(value_type _start, value_type _stop, value_type _step) {
        assert(_step != 0);
        if (_step > 0) {
            if (_start >= _stop) {
                return _start;
            } else {
                return _start + (_stop - _start + _step - 1) / _step * _step;
            }
        } else {
            if (_start <= _stop) {
                return _start;
            } else {
                return _start - (_start - _stop - _step - 1) / _step * _step;
            }
        }
    }

    constexpr Range(value_type _start, value_type _stop, value_type _step, value_type _norm_stop)
        : start(_start), stop(_stop), step(_step), norm_stop(_norm_stop) {
    }

   public:
    /**
     * @brief start から stop の手前まで step ずつ進む整数列
     * @pre step != 0
     */
    constexpr Range(value_type _start, value_type _stop, value_type _step)
        : start(_start), stop(_stop), step(_step), norm_stop(normalize(_start, _stop, _step)) {
    }
    constexpr Range(value_type _start, value_type _stop)
        : start(_start), stop(_stop), step(1), norm_stop(normalize(_start, _stop, 1)) {}
    explicit constexpr Range(value_type _stop)
        : start(0), stop(_stop), step(1), norm_stop(normalize(0, _stop, 1)) {}
    constexpr Range()
        : start(0), stop(0), step(1), norm_stop(0) {
    }
    constexpr Range(const Range& o) = default;
    Range& operator=(const Range& o) = delete;

    struct const_iterator {
       public:
        using difference_type = Range::difference_type;
        using value_type = Range::value_type;
        using pointer = void;
        using reference = Range::const_reference;
        using iterator_category = std::random_access_iterator_tag;

       private:
        value_type first = 0, stop = 0, step = 1, value = 0;

        constexpr bool same_range(const const_iterator& o) const noexcept {
            return first == o.first && stop == o.stop && step == o.step;
        }

       public:
        constexpr const_iterator() noexcept = default;
        constexpr const_iterator(const Range& range, value_type value) noexcept
            : first(range.start), stop(range.stop), step(range.step), value(value) {}
        constexpr reference base() const noexcept {
            return value;
        }
        constexpr const_iterator& operator++() noexcept {
            value += step;
            return *this;
        }
        constexpr const_iterator operator++(int) noexcept {
            const_iterator ret(*this);
            value += step;
            return ret;
        }
        constexpr const_iterator& operator+=(difference_type _n) noexcept {
            value += _n * step;
            return *this;
        }
        constexpr friend const_iterator operator+(const const_iterator& i, difference_type _n) noexcept {
            const_iterator ret(i);
            ret += _n;
            return ret;
        }
        constexpr friend const_iterator operator+(difference_type _n, const const_iterator& i) noexcept {
            const_iterator ret(i);
            ret += _n;
            return ret;
        }
        constexpr const_iterator& operator--() noexcept {
            value -= step;
            return *this;
        }
        constexpr const_iterator operator--(int) noexcept {
            const_iterator ret(*this);
            value -= step;
            return ret;
        }
        constexpr const_iterator& operator-=(difference_type _n) noexcept {
            value -= _n * step;
            return *this;
        }
        constexpr friend const_iterator operator-(const const_iterator& i, difference_type _n) noexcept {
            const_iterator ret(i);
            ret -= _n;
            return ret;
        }
        constexpr reference operator[](difference_type i) const noexcept { return value + i * step; }
        constexpr reference operator*() const noexcept {
            return value;
        }
        constexpr friend bool operator!=(const const_iterator& a, const const_iterator& b) noexcept {
            return !a.same_range(b) || a.value != b.value;
        }
        constexpr friend bool operator==(const const_iterator& a, const const_iterator& b) noexcept {
            return !(a != b);
        }
        constexpr friend bool operator<(const const_iterator& a, const const_iterator& b) noexcept {
            assert(a.same_range(b));
            return a.step > 0 ? a.value < b.value : a.value > b.value;
        }
        constexpr friend bool operator>(const const_iterator& a, const const_iterator& b) noexcept { return b < a; }
        constexpr friend bool operator<=(const const_iterator& a, const const_iterator& b) noexcept { return !(b < a); }
        constexpr friend bool operator>=(const const_iterator& a, const const_iterator& b) noexcept { return !(a < b); }
        constexpr friend difference_type operator-(const const_iterator& a, const const_iterator& b) {
            assert(a.same_range(b));
            if (a.step > 0) {
                return (a.value - b.value) / a.step;
            } else {
                return (b.value - a.value) / (-a.step);
            }
        }
    };

    constexpr const_iterator begin() const noexcept {
        return {*this, start};
    }
    constexpr const_iterator end() const noexcept {
        return {*this, norm_stop};
    }
    /**
     * @brief x の個数 (0 か 1)
     */
    constexpr size_type count(value_type x) const noexcept {
        // step がコンパイル時に分からないときも、よく使う step = 1 では割り算をしない
        if (step == 1) return start <= x && x < stop;
        if (step > 0) {
            if (start <= x && x < stop) {
                if ((x - start) % step == 0) {
                    return 1;
                }
            }
        } else {
            if (stop < x && x <= start) {
                if ((start - x) % (-step) == 0) {
                    return 1;
                }
            }
        }
        return 0;
    }
    /**
     * @brief x が含まれるか
     */
    constexpr bool contains(value_type x) const noexcept {
        return count(x) != 0;
    }
    /**
     * @brief x のインデックス
     * x が含まれないとエラー
     */
    constexpr size_type index(value_type x) const {
        assert(contains(x));
        if (step == 1) return x - start;
        return (x - start) / step;
    }
    /**
     * @brief 要素が x のイテレータ
     * x が含まれないと end() を返す
     */
    constexpr const_iterator find(value_type x) const noexcept {
        if (contains(x)) {
            return {*this, x};
        } else {
            return {*this, norm_stop};
        }
    }
    /**
     * @brief 要素が x 以上の最小のイテレータ
     * step > 0 じゃないとエラー
     */
    constexpr const_iterator lower_bound(value_type x) const {
        assert(step > 0);
        if (norm_stop <= x) {
            return end();
        } else if (x < start) {
            return begin();
        } else {
            return {*this, start + (x - start + step - 1) / step * step};
        }
    }
    /**
     * @brief 要素が x より大きい最小のイテレータ
     * step > 0 じゃないとエラー
     */
    constexpr const_iterator upper_bound(value_type x) const {
        assert(step > 0);
        if (norm_stop <= x) {
            return end();
        } else if (x < start) {
            return begin();
        } else {
            return {*this, start + (x - start + step) / step * step};
        }
    }
    /**
     * @brief コンテナの要素数
     */
    constexpr size_type size() const noexcept {
        if (step > 0) {
            return (norm_stop - start) / step;
        } else {
            return (start - norm_stop) / (-step);
        }
    }
    constexpr value_type at(size_type i) const {
        assert(0 <= i && i < size());
        return start + i * step;
    }
    constexpr value_type operator[](size_type i) const {
        return at(i);
    }
    constexpr bool empty() const noexcept {
        return start == norm_stop;
    }
    constexpr value_type sum() const noexcept {
        return size() * (norm_stop - step + start) / 2;
    }
    /**
     * @brief 最小値
     * 空だとエラー
     */
    constexpr value_type min() const {
        assert(!empty());
        if (step > 0) {
            return start;
        } else {
            return norm_stop - step;
        }
    }
    /**
     * @brief 最大値
     * 空だとエラー
     */
    constexpr value_type max() const {
        assert(!empty());
        if (step > 0) {
            return norm_stop - step;
        } else {
            return start;
        }
    }
    /**
     * @brief 総 xor
     * 負を含むとエラー
     */
    constexpr value_type product_xor() const noexcept {
        if (empty()) return 0;
        assert(min() >= 0);
        Range sorted_range(sorted());
        value_type res = 0;
        std::make_unsigned_t<value_type> t = 1;
        while (t <= static_cast<std::make_unsigned_t<value_type>>(max())) {
            std::make_unsigned_t<value_type> tmp = 0, n = size(), m = t, a = sorted_range.step, b = sorted_range.start;
            while (true) {
                if (a >= m) {
                    tmp += n * (n - 1) / 2 * (a / m);
                    a %= m;
                }
                if (b >= m) {
                    tmp += n * (b / m);
                    b %= m;
                }
                std::make_unsigned_t<value_type> y_max = a * n + b;
                if (y_max < m) break;
                n = y_max / m;
                b = y_max % m;
                std::swap(m, a);
            }
            if (tmp % 2 == 1) {
                res += t;
            }
            t <<= 1;
        }
        return res;
    }
    /**
     * @brief 逆順にしたもの
     */
    constexpr Range reversed() const noexcept {
        return {norm_stop - step, start - step, -step, start - step};
    }
    /**
     * @brief step > 0 だとそのまま step < 0 だと逆順にしたもの
     */
    constexpr Range sorted() const noexcept {
        if (step > 0) {
            return {start, norm_stop, step, norm_stop};
        } else {
            return {norm_stop - step, start - step, -step, start - step};
        }
    }
    /**
     * @brief 要素が [l, r) である範囲を返す
     */
    constexpr Range clipped(value_type l, value_type r) const {
        if (l >= r) return {};
        r--;  // 以下では r を含む [l, r] として扱う
        if (step > 0) {
            value_type _start = [&] {
                if (norm_stop <= l) {
                    return norm_stop;
                } else if (l < start) {
                    return start;
                } else {
                    return start + (l - start + step - 1) / step * step;
                }
            }();
            value_type _stop = [&] {
                if (norm_stop <= r) {
                    return norm_stop;
                } else if (r < start) {
                    return start;
                } else {
                    return start + (r - start + step) / step * step;
                }
            }();
            return {_start, _stop, step, _stop};
        } else {
            value_type _start = [&] {
                if (start - step <= r) {
                    return start;
                } else if (r < norm_stop - step) {
                    return norm_stop;
                } else {
                    return norm_stop + (r - norm_stop) / step * step;
                }
            }();
            value_type _stop = [&] {
                if (start - step <= l) {
                    return start;
                } else if (l < norm_stop - step) {
                    return norm_stop;
                } else {
                    return norm_stop + (l - norm_stop - 1) / step * step;
                }
            }();
            return {_start, _stop, step, _stop};
        }
    }
    /**
     * @brief 要素を Range で指定したコンテナ
     */
    constexpr Range slice(const Range& r) const {
        if (r.empty()) return {};
        if (r.max() < 0) return {};
        if (r.min() >= size()) return {};
        Range trimmed_range(r.clipped(0, size()));
        if (trimmed_range.empty()) return {};
        value_type _step = step * trimmed_range.step;
        value_type _start = at(trimmed_range.start);
        value_type _stop = _start + _step * trimmed_range.size();
        return {_start, _stop, _step, _stop};
    }

    static std::string type_str() {
        return "Range";
    }
    std::string initializer_str() const {
        return "{" + std::to_string(start) + ", " + std::to_string(stop) + ", " + std::to_string(step) + "}";
    }
    std::string repr() const {
        return "Range(" + std::to_string(start) + ", " + std::to_string(stop) + ", " + std::to_string(step) + ")";
    }

    std::vector<value_type> to_vector() const {
        std::vector<value_type> ret;
        ret.reserve(size());
        if (step > 0) {
            for (value_type x = start; x < stop; x += step) {
                ret.push_back(x);
            }
        } else {
            for (value_type x = start; x > stop; x += step) {
                ret.push_back(x);
            }
        }
        return ret;
    }
    constexpr friend bool operator==(const Range& a, const Range& b) noexcept {
        if (a.empty() && b.empty()) return true;
        if (a.size() != b.size()) return false;
        if (a.start != b.start) return false;
        if (a.size() == 1) return true;
        if (a.step != b.step) return false;
        return true;
    }
    constexpr friend bool operator!=(const Range& a, const Range& b) noexcept {
        return !(a == b);
    }
};

/**
 * @brief 2 つの Range の直積 rows × cols
 * (i, j) を i の昇順、同じ i では j の昇順 (行優先) に並べたものとして扱う
 */
struct Grid {
   public:
    using value_type = std::pair<long long, long long>;
    using size_type = long long;
    using difference_type = std::ptrdiff_t;

    const Range rows;
    const Range cols;

   private:
    const long long width;  // cols.size() (index や at で毎回割り算しないように持っておく)

   public:
    /**
     * @brief [0, h) × [0, w)
     */
    constexpr Grid(long long h, long long w) : rows(h), cols(w), width(cols.size()) {}
    constexpr Grid(const Range& _rows, const Range& _cols) : rows(_rows), cols(_cols), width(cols.size()) {}
    constexpr Grid(const Grid& o) = default;
    Grid& operator=(const Grid& o) = delete;

    struct const_iterator {
       public:
        using difference_type = Grid::difference_type;
        using value_type = Grid::value_type;
        using pointer = void;
        using reference = value_type;
        using iterator_category = std::forward_iterator_tag;

       private:
        const Grid* rect = nullptr;
        long long w = 0;   // cols.size()
        long long ic = 0;  // rows の何番目か
        long long jc = 0;  // cols の何番目か

       public:
        constexpr const_iterator() noexcept = default;
        constexpr const_iterator(const Grid* _rect, long long _w, long long _ic, long long _jc) noexcept
            : rect(_rect), w(_w), ic(_ic), jc(_jc) {}
        constexpr value_type operator*() const noexcept {
            return {rect->rows.start + ic * rect->rows.step, rect->cols.start + jc * rect->cols.step};
        }
        constexpr const_iterator& operator++() noexcept {
            if (++jc == w) {
                jc = 0;
                ++ic;
            }
            return *this;
        }
        constexpr const_iterator operator++(int) noexcept {
            const_iterator ret(*this);
            ++*this;
            return ret;
        }
        constexpr friend bool operator==(const const_iterator& a, const const_iterator& b) noexcept {
            return a.ic == b.ic && a.jc == b.jc;
        }
        constexpr friend bool operator!=(const const_iterator& a, const const_iterator& b) noexcept {
            return !(a == b);
        }
    };

    constexpr const_iterator begin() const noexcept {
        return {this, width, width == 0 ? rows.size() : 0, 0};
    }
    constexpr const_iterator end() const noexcept {
        return {this, width, rows.size(), 0};
    }
    /**
     * @brief 要素数 rows.size() * cols.size()
     */
    constexpr size_type size() const noexcept {
        return rows.size() * width;
    }
    constexpr bool empty() const noexcept {
        return rows.empty() || cols.empty();
    }
    /**
     * @brief (i, j) が含まれるか
     */
    constexpr bool contains(long long i, long long j) const noexcept {
        return rows.contains(i) && cols.contains(j);
    }
    constexpr bool contains(const value_type& p) const noexcept {
        return contains(p.first, p.second);
    }
    /**
     * @brief (i, j) の行優先での位置 (グリッドの頂点番号に使う)
     * (i, j) が含まれないとエラー
     */
    constexpr size_type index(long long i, long long j) const {
        assert(contains(i, j));
        return rows.index(i) * width + cols.index(j);
    }
    constexpr size_type index(const value_type& p) const {
        return index(p.first, p.second);
    }
    /**
     * @brief 行優先で k 番目の要素 (index の逆)
     */
    constexpr value_type at(size_type k) const {
        assert(0 <= k && k < size());
        return {rows[k / width], cols[k % width]};
    }
    constexpr value_type operator[](size_type k) const {
        return at(k);
    }

   private:
    // 近傍の方向 (rows・cols の中での位置の差) を Grid の順に並べたもの。先頭 4 つは上下左右だけを並べた別の表
    // 4 近傍と 8 近傍の境目が分かるように 2 行に分けているので整形しない
    // clang-format off
    static constexpr int neighbor_dirs[12][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0},
                                                 {-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
    // clang-format on

   public:
    /**
     * @brief neighbors4 / neighbors8 の戻り値 (範囲 for で使う)
     * 作るときに Grid に含まれる方向をビットで持っておき、立っているビットを順に返す
     */
    struct Neighbors {
        long long i, j, row_step, col_step;
        int first;      // neighbor_dirs[first + k] がビット k の方向
        unsigned mask;  // Grid に含まれる方向

        struct iterator {
            using difference_type = std::ptrdiff_t;
            using value_type = Grid::value_type;
            using pointer = void;
            using reference = value_type;
            using iterator_category = std::forward_iterator_tag;

            const Neighbors* nb = nullptr;
            unsigned rest = 0;  // まだ返していない方向

            constexpr value_type operator*() const noexcept {
                const int k = nb->first + __builtin_ctz(rest);
                return {nb->i + neighbor_dirs[k][0] * nb->row_step, nb->j + neighbor_dirs[k][1] * nb->col_step};
            }
            constexpr iterator& operator++() noexcept {
                rest &= rest - 1;
                return *this;
            }
            constexpr iterator operator++(int) noexcept {
                iterator ret(*this);
                ++*this;
                return ret;
            }
            constexpr friend bool operator==(const iterator& a, const iterator& b) noexcept { return a.rest == b.rest; }
            constexpr friend bool operator!=(const iterator& a, const iterator& b) noexcept { return a.rest != b.rest; }
        };
        constexpr iterator begin() const noexcept { return {this, mask}; }
        constexpr iterator end() const noexcept { return {this, 0}; }
        constexpr long long size() const noexcept { return __builtin_popcount(mask); }
        constexpr bool empty() const noexcept { return mask == 0; }
    };

   private:
    constexpr Neighbors neighbors(long long i, long long j, int first, int last) const {
        assert(contains(i, j));
        unsigned mask = 0;
        for (int k = first; k < last; k++) {
            if (contains(i + neighbor_dirs[k][0] * rows.step, j + neighbor_dirs[k][1] * cols.step)) mask |= 1u << (k - first);
        }
        return {i, j, rows.step, cols.step, first, mask};
    }

   public:
    /**
     * @brief (i, j) の上下左右のうち Grid に含まれるものを、Grid の順に返す
     * 隣は rows・cols の中で隣り合う要素 (i ± rows.step など)。(i, j) が含まれないとエラー
     */
    constexpr Neighbors neighbors4(long long i, long long j) const {
        return neighbors(i, j, 0, 4);
    }
    constexpr Neighbors neighbors4(const value_type& p) const {
        return neighbors4(p.first, p.second);
    }
    /**
     * @brief (i, j) を囲む 8 つのうち Grid に含まれるものを、Grid の順に返す
     */
    constexpr Neighbors neighbors8(long long i, long long j) const {
        return neighbors(i, j, 4, 12);
    }
    constexpr Neighbors neighbors8(const value_type& p) const {
        return neighbors8(p.first, p.second);
    }

    static std::string type_str() {
        return "Grid";
    }
    std::string initializer_str() const {
        return "{" + rows.initializer_str() + ", " + cols.initializer_str() + "}";
    }
    std::string repr() const {
        return "Grid(" + rows.repr() + ", " + cols.repr() + ")";
    }

    constexpr friend bool operator==(const Grid& a, const Grid& b) noexcept {
        if (a.empty() && b.empty()) return true;
        return a.rows == b.rows && a.cols == b.cols;
    }
    constexpr friend bool operator!=(const Grid& a, const Grid& b) noexcept {
        return !(a == b);
    }
};
#line 2 "src/utility/range.hpp"

/**
 * @file range.hpp
 * @brief Python の range のような整数列と、その直積の Grid
 */

#include <cassert>
#include <cstddef>
#include <iterator>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

struct Range {
   public:
    using value_type = long long;
    using const_reference = value_type;
    using size_type = long long;
    using difference_type = std::ptrdiff_t;

    const value_type start;
    const value_type stop;
    const value_type step;

   private:
    const value_type norm_stop;

    constexpr static value_type normalize(value_type _start, value_type _stop, value_type _step) {
        assert(_step != 0);
        if (_step > 0) {
            if (_start >= _stop) {
                return _start;
            } else {
                return _start + (_stop - _start + _step - 1) / _step * _step;
            }
        } else {
            if (_start <= _stop) {
                return _start;
            } else {
                return _start - (_start - _stop - _step - 1) / _step * _step;
            }
        }
    }

    constexpr Range(value_type _start, value_type _stop, value_type _step, value_type _norm_stop)
        : start(_start), stop(_stop), step(_step), norm_stop(_norm_stop) {
    }

   public:
    /**
     * @brief start から stop の手前まで step ずつ進む整数列
     * @pre step != 0
     */
    constexpr Range(value_type _start, value_type _stop, value_type _step)
        : start(_start), stop(_stop), step(_step), norm_stop(normalize(_start, _stop, _step)) {
    }
    constexpr Range(value_type _start, value_type _stop)
        : start(_start), stop(_stop), step(1), norm_stop(normalize(_start, _stop, 1)) {}
    explicit constexpr Range(value_type _stop)
        : start(0), stop(_stop), step(1), norm_stop(normalize(0, _stop, 1)) {}
    constexpr Range()
        : start(0), stop(0), step(1), norm_stop(0) {
    }
    constexpr Range(const Range& o) = default;
    Range& operator=(const Range& o) = delete;

    struct const_iterator {
       public:
        using difference_type = Range::difference_type;
        using value_type = Range::value_type;
        using pointer = void;
        using reference = Range::const_reference;
        using iterator_category = std::random_access_iterator_tag;

       private:
        value_type first = 0, stop = 0, step = 1, value = 0;

        constexpr bool same_range(const const_iterator& o) const noexcept {
            return first == o.first && stop == o.stop && step == o.step;
        }

       public:
        constexpr const_iterator() noexcept = default;
        constexpr const_iterator(const Range& range, value_type value) noexcept
            : first(range.start), stop(range.stop), step(range.step), value(value) {}
        constexpr reference base() const noexcept {
            return value;
        }
        constexpr const_iterator& operator++() noexcept {
            value += step;
            return *this;
        }
        constexpr const_iterator operator++(int) noexcept {
            const_iterator ret(*this);
            value += step;
            return ret;
        }
        constexpr const_iterator& operator+=(difference_type _n) noexcept {
            value += _n * step;
            return *this;
        }
        constexpr friend const_iterator operator+(const const_iterator& i, difference_type _n) noexcept {
            const_iterator ret(i);
            ret += _n;
            return ret;
        }
        constexpr friend const_iterator operator+(difference_type _n, const const_iterator& i) noexcept {
            const_iterator ret(i);
            ret += _n;
            return ret;
        }
        constexpr const_iterator& operator--() noexcept {
            value -= step;
            return *this;
        }
        constexpr const_iterator operator--(int) noexcept {
            const_iterator ret(*this);
            value -= step;
            return ret;
        }
        constexpr const_iterator& operator-=(difference_type _n) noexcept {
            value -= _n * step;
            return *this;
        }
        constexpr friend const_iterator operator-(const const_iterator& i, difference_type _n) noexcept {
            const_iterator ret(i);
            ret -= _n;
            return ret;
        }
        constexpr reference operator[](difference_type i) const noexcept { return value + i * step; }
        constexpr reference operator*() const noexcept {
            return value;
        }
        constexpr friend bool operator!=(const const_iterator& a, const const_iterator& b) noexcept {
            return !a.same_range(b) || a.value != b.value;
        }
        constexpr friend bool operator==(const const_iterator& a, const const_iterator& b) noexcept {
            return !(a != b);
        }
        constexpr friend bool operator<(const const_iterator& a, const const_iterator& b) noexcept {
            assert(a.same_range(b));
            return a.step > 0 ? a.value < b.value : a.value > b.value;
        }
        constexpr friend bool operator>(const const_iterator& a, const const_iterator& b) noexcept { return b < a; }
        constexpr friend bool operator<=(const const_iterator& a, const const_iterator& b) noexcept { return !(b < a); }
        constexpr friend bool operator>=(const const_iterator& a, const const_iterator& b) noexcept { return !(a < b); }
        constexpr friend difference_type operator-(const const_iterator& a, const const_iterator& b) {
            assert(a.same_range(b));
            if (a.step > 0) {
                return (a.value - b.value) / a.step;
            } else {
                return (b.value - a.value) / (-a.step);
            }
        }
    };

    constexpr const_iterator begin() const noexcept {
        return {*this, start};
    }
    constexpr const_iterator end() const noexcept {
        return {*this, norm_stop};
    }
    /**
     * @brief x の個数 (0 か 1)
     */
    constexpr size_type count(value_type x) const noexcept {
        // step がコンパイル時に分からないときも、よく使う step = 1 では割り算をしない
        if (step == 1) return start <= x && x < stop;
        if (step > 0) {
            if (start <= x && x < stop) {
                if ((x - start) % step == 0) {
                    return 1;
                }
            }
        } else {
            if (stop < x && x <= start) {
                if ((start - x) % (-step) == 0) {
                    return 1;
                }
            }
        }
        return 0;
    }
    /**
     * @brief x が含まれるか
     */
    constexpr bool contains(value_type x) const noexcept {
        return count(x) != 0;
    }
    /**
     * @brief x のインデックス
     * x が含まれないとエラー
     */
    constexpr size_type index(value_type x) const {
        assert(contains(x));
        if (step == 1) return x - start;
        return (x - start) / step;
    }
    /**
     * @brief 要素が x のイテレータ
     * x が含まれないと end() を返す
     */
    constexpr const_iterator find(value_type x) const noexcept {
        if (contains(x)) {
            return {*this, x};
        } else {
            return {*this, norm_stop};
        }
    }
    /**
     * @brief 要素が x 以上の最小のイテレータ
     * step > 0 じゃないとエラー
     */
    constexpr const_iterator lower_bound(value_type x) const {
        assert(step > 0);
        if (norm_stop <= x) {
            return end();
        } else if (x < start) {
            return begin();
        } else {
            return {*this, start + (x - start + step - 1) / step * step};
        }
    }
    /**
     * @brief 要素が x より大きい最小のイテレータ
     * step > 0 じゃないとエラー
     */
    constexpr const_iterator upper_bound(value_type x) const {
        assert(step > 0);
        if (norm_stop <= x) {
            return end();
        } else if (x < start) {
            return begin();
        } else {
            return {*this, start + (x - start + step) / step * step};
        }
    }
    /**
     * @brief コンテナの要素数
     */
    constexpr size_type size() const noexcept {
        if (step > 0) {
            return (norm_stop - start) / step;
        } else {
            return (start - norm_stop) / (-step);
        }
    }
    constexpr value_type at(size_type i) const {
        assert(0 <= i && i < size());
        return start + i * step;
    }
    constexpr value_type operator[](size_type i) const {
        return at(i);
    }
    constexpr bool empty() const noexcept {
        return start == norm_stop;
    }
    constexpr value_type sum() const noexcept {
        return size() * (norm_stop - step + start) / 2;
    }
    /**
     * @brief 最小値
     * 空だとエラー
     */
    constexpr value_type min() const {
        assert(!empty());
        if (step > 0) {
            return start;
        } else {
            return norm_stop - step;
        }
    }
    /**
     * @brief 最大値
     * 空だとエラー
     */
    constexpr value_type max() const {
        assert(!empty());
        if (step > 0) {
            return norm_stop - step;
        } else {
            return start;
        }
    }
    /**
     * @brief 総 xor
     * 負を含むとエラー
     */
    constexpr value_type product_xor() const noexcept {
        if (empty()) return 0;
        assert(min() >= 0);
        Range sorted_range(sorted());
        value_type res = 0;
        std::make_unsigned_t<value_type> t = 1;
        while (t <= static_cast<std::make_unsigned_t<value_type>>(max())) {
            std::make_unsigned_t<value_type> tmp = 0, n = size(), m = t, a = sorted_range.step, b = sorted_range.start;
            while (true) {
                if (a >= m) {
                    tmp += n * (n - 1) / 2 * (a / m);
                    a %= m;
                }
                if (b >= m) {
                    tmp += n * (b / m);
                    b %= m;
                }
                std::make_unsigned_t<value_type> y_max = a * n + b;
                if (y_max < m) break;
                n = y_max / m;
                b = y_max % m;
                std::swap(m, a);
            }
            if (tmp % 2 == 1) {
                res += t;
            }
            t <<= 1;
        }
        return res;
    }
    /**
     * @brief 逆順にしたもの
     */
    constexpr Range reversed() const noexcept {
        return {norm_stop - step, start - step, -step, start - step};
    }
    /**
     * @brief step > 0 だとそのまま step < 0 だと逆順にしたもの
     */
    constexpr Range sorted() const noexcept {
        if (step > 0) {
            return {start, norm_stop, step, norm_stop};
        } else {
            return {norm_stop - step, start - step, -step, start - step};
        }
    }
    /**
     * @brief 要素が [l, r) である範囲を返す
     */
    constexpr Range clipped(value_type l, value_type r) const {
        if (l >= r) return {};
        r--;  // 以下では r を含む [l, r] として扱う
        if (step > 0) {
            value_type _start = [&] {
                if (norm_stop <= l) {
                    return norm_stop;
                } else if (l < start) {
                    return start;
                } else {
                    return start + (l - start + step - 1) / step * step;
                }
            }();
            value_type _stop = [&] {
                if (norm_stop <= r) {
                    return norm_stop;
                } else if (r < start) {
                    return start;
                } else {
                    return start + (r - start + step) / step * step;
                }
            }();
            return {_start, _stop, step, _stop};
        } else {
            value_type _start = [&] {
                if (start - step <= r) {
                    return start;
                } else if (r < norm_stop - step) {
                    return norm_stop;
                } else {
                    return norm_stop + (r - norm_stop) / step * step;
                }
            }();
            value_type _stop = [&] {
                if (start - step <= l) {
                    return start;
                } else if (l < norm_stop - step) {
                    return norm_stop;
                } else {
                    return norm_stop + (l - norm_stop - 1) / step * step;
                }
            }();
            return {_start, _stop, step, _stop};
        }
    }
    /**
     * @brief 要素を Range で指定したコンテナ
     */
    constexpr Range slice(const Range& r) const {
        if (r.empty()) return {};
        if (r.max() < 0) return {};
        if (r.min() >= size()) return {};
        Range trimmed_range(r.clipped(0, size()));
        if (trimmed_range.empty()) return {};
        value_type _step = step * trimmed_range.step;
        value_type _start = at(trimmed_range.start);
        value_type _stop = _start + _step * trimmed_range.size();
        return {_start, _stop, _step, _stop};
    }

    static std::string type_str() {
        return "Range";
    }
    std::string initializer_str() const {
        return "{" + std::to_string(start) + ", " + std::to_string(stop) + ", " + std::to_string(step) + "}";
    }
    std::string repr() const {
        return "Range(" + std::to_string(start) + ", " + std::to_string(stop) + ", " + std::to_string(step) + ")";
    }

    std::vector<value_type> to_vector() const {
        std::vector<value_type> ret;
        ret.reserve(size());
        if (step > 0) {
            for (value_type x = start; x < stop; x += step) {
                ret.push_back(x);
            }
        } else {
            for (value_type x = start; x > stop; x += step) {
                ret.push_back(x);
            }
        }
        return ret;
    }
    constexpr friend bool operator==(const Range& a, const Range& b) noexcept {
        if (a.empty() && b.empty()) return true;
        if (a.size() != b.size()) return false;
        if (a.start != b.start) return false;
        if (a.size() == 1) return true;
        if (a.step != b.step) return false;
        return true;
    }
    constexpr friend bool operator!=(const Range& a, const Range& b) noexcept {
        return !(a == b);
    }
};

/**
 * @brief 2 つの Range の直積 rows × cols
 * (i, j) を i の昇順、同じ i では j の昇順 (行優先) に並べたものとして扱う
 */
struct Grid {
   public:
    using value_type = std::pair<long long, long long>;
    using size_type = long long;
    using difference_type = std::ptrdiff_t;

    const Range rows;
    const Range cols;

   private:
    const long long width;  // cols.size() (index や at で毎回割り算しないように持っておく)

   public:
    /**
     * @brief [0, h) × [0, w)
     */
    constexpr Grid(long long h, long long w) : rows(h), cols(w), width(cols.size()) {}
    constexpr Grid(const Range& _rows, const Range& _cols) : rows(_rows), cols(_cols), width(cols.size()) {}
    constexpr Grid(const Grid& o) = default;
    Grid& operator=(const Grid& o) = delete;

    struct const_iterator {
       public:
        using difference_type = Grid::difference_type;
        using value_type = Grid::value_type;
        using pointer = void;
        using reference = value_type;
        using iterator_category = std::forward_iterator_tag;

       private:
        const Grid* rect = nullptr;
        long long w = 0;   // cols.size()
        long long ic = 0;  // rows の何番目か
        long long jc = 0;  // cols の何番目か

       public:
        constexpr const_iterator() noexcept = default;
        constexpr const_iterator(const Grid* _rect, long long _w, long long _ic, long long _jc) noexcept
            : rect(_rect), w(_w), ic(_ic), jc(_jc) {}
        constexpr value_type operator*() const noexcept {
            return {rect->rows.start + ic * rect->rows.step, rect->cols.start + jc * rect->cols.step};
        }
        constexpr const_iterator& operator++() noexcept {
            if (++jc == w) {
                jc = 0;
                ++ic;
            }
            return *this;
        }
        constexpr const_iterator operator++(int) noexcept {
            const_iterator ret(*this);
            ++*this;
            return ret;
        }
        constexpr friend bool operator==(const const_iterator& a, const const_iterator& b) noexcept {
            return a.ic == b.ic && a.jc == b.jc;
        }
        constexpr friend bool operator!=(const const_iterator& a, const const_iterator& b) noexcept {
            return !(a == b);
        }
    };

    constexpr const_iterator begin() const noexcept {
        return {this, width, width == 0 ? rows.size() : 0, 0};
    }
    constexpr const_iterator end() const noexcept {
        return {this, width, rows.size(), 0};
    }
    /**
     * @brief 要素数 rows.size() * cols.size()
     */
    constexpr size_type size() const noexcept {
        return rows.size() * width;
    }
    constexpr bool empty() const noexcept {
        return rows.empty() || cols.empty();
    }
    /**
     * @brief (i, j) が含まれるか
     */
    constexpr bool contains(long long i, long long j) const noexcept {
        return rows.contains(i) && cols.contains(j);
    }
    constexpr bool contains(const value_type& p) const noexcept {
        return contains(p.first, p.second);
    }
    /**
     * @brief (i, j) の行優先での位置 (グリッドの頂点番号に使う)
     * (i, j) が含まれないとエラー
     */
    constexpr size_type index(long long i, long long j) const {
        assert(contains(i, j));
        return rows.index(i) * width + cols.index(j);
    }
    constexpr size_type index(const value_type& p) const {
        return index(p.first, p.second);
    }
    /**
     * @brief 行優先で k 番目の要素 (index の逆)
     */
    constexpr value_type at(size_type k) const {
        assert(0 <= k && k < size());
        return {rows[k / width], cols[k % width]};
    }
    constexpr value_type operator[](size_type k) const {
        return at(k);
    }

   private:
    // 近傍の方向 (rows・cols の中での位置の差) を Grid の順に並べたもの。先頭 4 つは上下左右だけを並べた別の表
    // 4 近傍と 8 近傍の境目が分かるように 2 行に分けているので整形しない
    // clang-format off
    static constexpr int neighbor_dirs[12][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0},
                                                 {-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
    // clang-format on

   public:
    /**
     * @brief neighbors4 / neighbors8 の戻り値 (範囲 for で使う)
     * 作るときに Grid に含まれる方向をビットで持っておき、立っているビットを順に返す
     */
    struct Neighbors {
        long long i, j, row_step, col_step;
        int first;      // neighbor_dirs[first + k] がビット k の方向
        unsigned mask;  // Grid に含まれる方向

        struct iterator {
            using difference_type = std::ptrdiff_t;
            using value_type = Grid::value_type;
            using pointer = void;
            using reference = value_type;
            using iterator_category = std::forward_iterator_tag;

            const Neighbors* nb = nullptr;
            unsigned rest = 0;  // まだ返していない方向

            constexpr value_type operator*() const noexcept {
                const int k = nb->first + __builtin_ctz(rest);
                return {nb->i + neighbor_dirs[k][0] * nb->row_step, nb->j + neighbor_dirs[k][1] * nb->col_step};
            }
            constexpr iterator& operator++() noexcept {
                rest &= rest - 1;
                return *this;
            }
            constexpr iterator operator++(int) noexcept {
                iterator ret(*this);
                ++*this;
                return ret;
            }
            constexpr friend bool operator==(const iterator& a, const iterator& b) noexcept { return a.rest == b.rest; }
            constexpr friend bool operator!=(const iterator& a, const iterator& b) noexcept { return a.rest != b.rest; }
        };
        constexpr iterator begin() const noexcept { return {this, mask}; }
        constexpr iterator end() const noexcept { return {this, 0}; }
        constexpr long long size() const noexcept { return __builtin_popcount(mask); }
        constexpr bool empty() const noexcept { return mask == 0; }
    };

   private:
    constexpr Neighbors neighbors(long long i, long long j, int first, int last) const {
        assert(contains(i, j));
        unsigned mask = 0;
        for (int k = first; k < last; k++) {
            if (contains(i + neighbor_dirs[k][0] * rows.step, j + neighbor_dirs[k][1] * cols.step)) mask |= 1u << (k - first);
        }
        return {i, j, rows.step, cols.step, first, mask};
    }

   public:
    /**
     * @brief (i, j) の上下左右のうち Grid に含まれるものを、Grid の順に返す
     * 隣は rows・cols の中で隣り合う要素 (i ± rows.step など)。(i, j) が含まれないとエラー
     */
    constexpr Neighbors neighbors4(long long i, long long j) const {
        return neighbors(i, j, 0, 4);
    }
    constexpr Neighbors neighbors4(const value_type& p) const {
        return neighbors4(p.first, p.second);
    }
    /**
     * @brief (i, j) を囲む 8 つのうち Grid に含まれるものを、Grid の順に返す
     */
    constexpr Neighbors neighbors8(long long i, long long j) const {
        return neighbors(i, j, 4, 12);
    }
    constexpr Neighbors neighbors8(const value_type& p) const {
        return neighbors8(p.first, p.second);
    }

    static std::string type_str() {
        return "Grid";
    }
    std::string initializer_str() const {
        return "{" + rows.initializer_str() + ", " + cols.initializer_str() + "}";
    }
    std::string repr() const {
        return "Grid(" + rows.repr() + ", " + cols.repr() + ")";
    }

    constexpr friend bool operator==(const Grid& a, const Grid& b) noexcept {
        if (a.empty() && b.empty()) return true;
        return a.rows == b.rows && a.cols == b.cols;
    }
    constexpr friend bool operator!=(const Grid& a, const Grid& b) noexcept {
        return !(a == b);
    }
};
Back to top page