cplib-cpp

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub hitonanode/cplib-cpp

:heavy_check_mark: Binary lifting / doubling (ダブリング)
(other_algorithms/binary_lifting.hpp)

Functional graph 上のダブリングライブラリ.

使用方法

binary_lifting(const std::vector<int> &g, const std::vector<S> &w)

コンストラクタ.引数として $g(0), \ldots, g(n - 1)$ および $w(0), \ldots, w(n - 1)$ を与える.型 S は演算 S op(S, S) が結合法則を満たせばなんでもよい.

直感的には,各頂点 $i = 0, \ldots, n - 1$ について $i$ から頂点 $g(i)$ への重み $w(i)$ の有向辺が張られている functional graph に相当する. $g(i)$ の値は $0$ 未満や $n$ 以上でも構わない(下記の各関数は, $[0, n)$ の範囲外の頂点 $i$ からは $i$ 自身への重み $e$ の自己ループが生えている,と解釈するとつじつまが合うように設計されている).

int kth_next(int s, Int k)

$g^k (s)$ の値(途中で $[0, n)$ の範囲外に出る場合はその値)を前計算 $O(n \log k)$ ・クエリ $O(\log k)$ で返す.

int pow_next(int s, int d)

特に $g^{2^d}(s)$ の値を返す.前計算が済んでいればクエリ $O(1)$.

S prod(int s, Int len)

列 $(s, g(s), \ldots, g^{\mathrm{len} - 1} (s))$ の各要素 $x$ に関する値 $w(x)$ をとり,これら全ての積をとる(途中で $[0, n)$ の範囲外に出る場合,それ以降の要素は無視される).

const S &pow_prod(int s, int d)

特に $w(s) \cdot w(g(s)) \cdot \ldots \cdot w(g^{2^d - 1}(s))$ を返す.前計算が済んでいればクエリ $O(1)$.

int distance_monotone(int start, int left_goal, int right_goal)

$g^k (s)$ の値が初めて left_goal 以下または right_goal 以上になる $k$ を計算する.この条件が満たされることはない場合は -1 を返す.

この条件に関する単調性が必要. $O(n \log n)$ の前計算が済んでいればクエリ $O(\log n)$.

long long max_length(int s, F f, int maxd = 60)

f(prod(s, len))true と評価される $2^{\mathrm{maxd}}$ 以下の最大の len を返す. f(prod(s, len)) の単調性が必要.

問題例

Verified with

Code

#pragma once
#include <cassert>
#include <vector>

// Binary lifting (Doubling) on functional graphs
template <class S, S (*op)(S, S)> class binary_lifting {
    int n = 0;
    std::vector<std::vector<int>> _nexts;
    std::vector<std::vector<S>> _prods;

    void build_next() {
        std::vector<int> _next(n);
        std::vector<S> _prod(n);

        for (int i = 0; i < n; ++i) {
            if (int j = _nexts.back().at(i); isin(j)) {
                _next.at(i) = _nexts.back().at(j);
                _prod.at(i) = op(_prods.back().at(i), _prods.back().at(j));
            } else {
                _next.at(i) = j;
                _prod.at(i) = _prods.back().at(i);
            }
        }

        _nexts.emplace_back(std::move(_next));
        _prods.emplace_back(std::move(_prod));
    }

    inline bool isin(int i) const noexcept { return 0 <= i and i < n; }

public:
    // (up to) 2^d steps from `s`
    // Complexity: O(d) (Already precalculated) / O(nd) (First time)
    int pow_next(int s, int d) {
        assert(isin(s));
        while (int(_nexts.size()) <= d) build_next();
        return _nexts.at(d).at(s);
    }

    // Product of (up to) 2^d elements from `s`
    const S &pow_prod(int s, int d) {
        assert(isin(s));
        while (int(_nexts.size()) <= d) build_next();
        return _prods.at(d).at(s);
    }

    binary_lifting() = default;
    binary_lifting(const std::vector<int> &g, const std::vector<S> &w)
        : n(g.size()), _nexts(1, g), _prods(1, w) {
        assert(g.size() == w.size());
    }

    // (up to) k steps from `s`
    template <class Int> int kth_next(int s, Int k) {
        for (int d = 0; k > 0 and isin(s); ++d, k >>= 1) {
            if (k & 1) s = pow_next(s, d);
        }
        return s;
    }

    // Product of (up to) `len` elements from `s`
    template <class Int> S prod(int s, Int len) {
        assert(isin(s));
        assert(len > 0);
        int d = 0;
        while (!(len & 1)) ++d, len /= 2;

        S ret = pow_prod(s, d);
        s = pow_next(s, d);
        for (++d, len /= 2; len and isin(s); ++d, len /= 2) {
            if (len & 1) {
                ret = op(ret, pow_prod(s, d));
                s = pow_next(s, d);
            }
        }
        return ret;
    }

    // `start` から出発して「`left_goal` 以下または `right_goal` 以上」に到達するまでのステップ数
    // 単調性が必要
    int distance_monotone(int start, int left_goal, int right_goal) {
        assert(isin(start));

        if (start <= left_goal or right_goal <= start) return 0;

        int d = 0;
        while (left_goal < pow_next(start, d) and pow_next(start, d) < right_goal) {
            if ((1 << d) >= n) return -1;
            ++d;
        }

        int ret = 0, cur = start;
        for (--d; d >= 0; --d) {
            if (int nxt = pow_next(cur, d); left_goal < nxt and nxt < right_goal) {
                ret += 1 << d, cur = nxt;
            }
        }

        return ret + 1;
    }

    template <class F> long long max_length(const int s, F f, const int maxd = 60) {
        assert(isin(s));
        int d = 0;
        while (d <= maxd and f(pow_prod(s, d))) {
            if (!isin(pow_next(s, d))) return 1LL << maxd;
            ++d;
        }
        if (d > maxd) return 1LL << maxd;

        --d;

        int cur = pow_next(s, d);
        long long len = 1LL << d;
        S p = pow_prod(s, d);

        for (int e = d - 1; e >= 0; --e) {
            if (S nextp = op(p, pow_prod(cur, e)); f(nextp)) {
                std::swap(p, nextp);
                cur = pow_next(cur, e);
                len += 1LL << e;
            }
        }

        return len;
    }
};
#line 2 "other_algorithms/binary_lifting.hpp"
#include <cassert>
#include <vector>

// Binary lifting (Doubling) on functional graphs
template <class S, S (*op)(S, S)> class binary_lifting {
    int n = 0;
    std::vector<std::vector<int>> _nexts;
    std::vector<std::vector<S>> _prods;

    void build_next() {
        std::vector<int> _next(n);
        std::vector<S> _prod(n);

        for (int i = 0; i < n; ++i) {
            if (int j = _nexts.back().at(i); isin(j)) {
                _next.at(i) = _nexts.back().at(j);
                _prod.at(i) = op(_prods.back().at(i), _prods.back().at(j));
            } else {
                _next.at(i) = j;
                _prod.at(i) = _prods.back().at(i);
            }
        }

        _nexts.emplace_back(std::move(_next));
        _prods.emplace_back(std::move(_prod));
    }

    inline bool isin(int i) const noexcept { return 0 <= i and i < n; }

public:
    // (up to) 2^d steps from `s`
    // Complexity: O(d) (Already precalculated) / O(nd) (First time)
    int pow_next(int s, int d) {
        assert(isin(s));
        while (int(_nexts.size()) <= d) build_next();
        return _nexts.at(d).at(s);
    }

    // Product of (up to) 2^d elements from `s`
    const S &pow_prod(int s, int d) {
        assert(isin(s));
        while (int(_nexts.size()) <= d) build_next();
        return _prods.at(d).at(s);
    }

    binary_lifting() = default;
    binary_lifting(const std::vector<int> &g, const std::vector<S> &w)
        : n(g.size()), _nexts(1, g), _prods(1, w) {
        assert(g.size() == w.size());
    }

    // (up to) k steps from `s`
    template <class Int> int kth_next(int s, Int k) {
        for (int d = 0; k > 0 and isin(s); ++d, k >>= 1) {
            if (k & 1) s = pow_next(s, d);
        }
        return s;
    }

    // Product of (up to) `len` elements from `s`
    template <class Int> S prod(int s, Int len) {
        assert(isin(s));
        assert(len > 0);
        int d = 0;
        while (!(len & 1)) ++d, len /= 2;

        S ret = pow_prod(s, d);
        s = pow_next(s, d);
        for (++d, len /= 2; len and isin(s); ++d, len /= 2) {
            if (len & 1) {
                ret = op(ret, pow_prod(s, d));
                s = pow_next(s, d);
            }
        }
        return ret;
    }

    // `start` から出発して「`left_goal` 以下または `right_goal` 以上」に到達するまでのステップ数
    // 単調性が必要
    int distance_monotone(int start, int left_goal, int right_goal) {
        assert(isin(start));

        if (start <= left_goal or right_goal <= start) return 0;

        int d = 0;
        while (left_goal < pow_next(start, d) and pow_next(start, d) < right_goal) {
            if ((1 << d) >= n) return -1;
            ++d;
        }

        int ret = 0, cur = start;
        for (--d; d >= 0; --d) {
            if (int nxt = pow_next(cur, d); left_goal < nxt and nxt < right_goal) {
                ret += 1 << d, cur = nxt;
            }
        }

        return ret + 1;
    }

    template <class F> long long max_length(const int s, F f, const int maxd = 60) {
        assert(isin(s));
        int d = 0;
        while (d <= maxd and f(pow_prod(s, d))) {
            if (!isin(pow_next(s, d))) return 1LL << maxd;
            ++d;
        }
        if (d > maxd) return 1LL << maxd;

        --d;

        int cur = pow_next(s, d);
        long long len = 1LL << d;
        S p = pow_prod(s, d);

        for (int e = d - 1; e >= 0; --e) {
            if (S nextp = op(p, pow_prod(cur, e)); f(nextp)) {
                std::swap(p, nextp);
                cur = pow_next(cur, e);
                len += 1LL << e;
            }
        }

        return len;
    }
};
Back to top page
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy