cp-documentation

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

View the Project on GitHub zawa-tin/cp-documentation

:heavy_check_mark: Test/AOJ/2450.test.cpp

Depends on

Code

#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/2450"

#include "../../Src/DataStructure/SegmentTree/AssignmentSegmentTree.hpp"
#include "../../Src/Graph/Tree/HeavyLightDecomposition.hpp"
#include "../../Src/Algebra/Monoid/SubarraySumMaxMonoid.hpp"
#include "../../Src/Algebra/Monoid/ReverseOrder.hpp"

#include <cassert>
#include <iostream>
#include <vector>
using namespace std;
using namespace zawa;
using M = SubarraySumMaxMonoid<long long>;

int main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
    int N, Q;
    cin >> N >> Q;
    vector<int> A(N);
    for (int i = 0 ; i < N ; i++) cin >> A[i];
    vector<vector<int>> g(N);
    for (int i = 0 ; i < N - 1 ; i++) {
        int u, v;
        cin >> u >> v;
        u--; v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    HeavyLightDecomposition hld{g};
    vector<M::Element> init(N);
    for (int i = 0 ; i < N ; i++) init[hld[i]] = M::convert(A[i]);
    AssignmentSegmentTree<M> seg{init};
    AssignmentSegmentTree<ReverseOrder<M>> ges{init};
    while (Q--) {
        int T;
        cin >> T;
        int a, b, c;
        cin >> a >> b >> c;
        a--; b--;
        if (T == 1) {
            for (auto [s, t] : hld(a, b)) {
                s = hld[s];
                t = hld[t];
                if (s > t) swap(s, t);
                seg.assign(s, t + 1, M::convert(c));
                ges.assign(s, t + 1, M::convert(c));
            }
        }
        else if (T == 2) {
            M::Element pd = M::identity();
            for (auto [s, t] : hld(a, b)) {
                s = hld[s];
                t = hld[t];
                if (s <= t) pd = M::operation(pd, seg.product(s, t + 1));
                else pd = M::operation(pd, ges.product(t, s + 1));
            }
            assert(pd);
            cout << pd->ans() << '\n';
        }
        else assert(false);
    }
}
#line 1 "Test/AOJ/2450.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/2450"

#line 2 "Src/DataStructure/SegmentTree/AssignmentSegmentTree.hpp"

#line 2 "Src/Template/TypeAlias.hpp"

#include <cstdint>
#include <cstddef>

namespace zawa {

using i16 = std::int16_t;
using i32 = std::int32_t;
using i64 = std::int64_t;
using i128 = __int128_t;

using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;

using usize = std::size_t;

} // namespace zawa
#line 2 "Src/Algebra/Monoid/MonoidConcept.hpp"

#line 2 "Src/Algebra/Semigroup/SemigroupConcept.hpp"

#include <concepts>

namespace zawa {

namespace concepts {

template <class T>
concept Semigroup = requires {
    typename T::Element;
    { T::operation(std::declval<typename T::Element>(), std::declval<typename T::Element>()) } -> std::same_as<typename T::Element>;
};

} // namespace concepts

} // namespace zawa
#line 4 "Src/Algebra/Monoid/MonoidConcept.hpp"

#line 6 "Src/Algebra/Monoid/MonoidConcept.hpp"

namespace zawa {

namespace concepts {

template <class T>
concept Identitiable = requires {
    typename T::Element;
    { T::identity() } -> std::same_as<typename T::Element>;
};

template <class T>
concept Monoid = Semigroup<T> and Identitiable<T>;

} // namespace

} // namespace zawa
#line 2 "Src/DataStructure/SegmentTree/SegmentTree.hpp"

#line 5 "Src/DataStructure/SegmentTree/SegmentTree.hpp"

#include <vector>
#include <cassert>
#include <functional>
#include <type_traits>
#include <ostream>

namespace zawa {

template <concepts::Monoid Monoid>
class SegmentTree {
public:

    using VM = Monoid;

    using V = typename VM::Element;

    using OM = Monoid;

    using O = typename OM::Element;

    SegmentTree() = default;

    explicit SegmentTree(usize n) : m_n{ n }, m_dat(n << 1, VM::identity()) {}

    explicit SegmentTree(const std::vector<V>& dat) : m_n{ dat.size() }, m_dat(dat.size() << 1, VM::identity()) {
        for (usize i{} ; i < m_n ; i++) {
            m_dat[i + m_n] = dat[i];
        }
        for (usize i{m_n} ; i-- ; ) {
            m_dat[i] = VM::operation(m_dat[left(i)], m_dat[right(i)]);
        }
    }

    [[nodiscard]] inline usize size() const noexcept {
        return m_n;
    }

    [[nodiscard]] V get(usize i) const {
        assert(i < size());
        return m_dat[i + m_n];
    }

    [[nodiscard]] V operator[](usize i) const {
        assert(i < size());
        return m_dat[i + m_n];
    }

    void operation(usize i, const O& value) {
        assert(i < size());
        i += size();
        m_dat[i] = OM::operation(m_dat[i], value);
        while (i = parent(i), i) {
            m_dat[i] = VM::operation(m_dat[left(i)], m_dat[right(i)]);
        }
    }

    void assign(usize i, const V& value) {
        assert(i < size());
        i += size();
        m_dat[i] = value;
        while (i = parent(i), i) {
            m_dat[i] = VM::operation(m_dat[left(i)], m_dat[right(i)]);
        }
    }

    [[nodiscard]] V product(u32 l, u32 r) const {
        assert(l <= r and r <= size());
        V L{ VM::identity() }, R{ VM::identity() };
        for (l += size(), r += size() ; l < r ; l = parent(l), r = parent(r)) {
            if (l & 1) {
                L = VM::operation(L, m_dat[l++]);
            }
            if (r & 1) {
                R = VM::operation(m_dat[--r], R);
            }
        }
        return VM::operation(L, R);
    }

    template <class F>
    requires std::predicate<F, V>
    [[nodiscard]] usize maxRight(usize l, const F& f) {
        assert(l < size());
        static_assert(std::is_convertible_v<decltype(f), std::function<bool(V)>>, "maxRight's argument f must be function bool(T)");
        assert(f(VM::identity()));
        usize res{l}, width{1};
        V prod{ VM::identity() };
        // 現在の見ている頂点の幅をwidthで持つ
        // 境界がある頂点を含む部分木の根を探す
        // (折り返す時は必要以上の幅を持つ根になるが、widthを持っているのでオーバーしない)
        for (l += size() ; res + width <= size() ; l = parent(l), width <<= 1) if (l & 1) {
            if (not f(VM::operation(prod, m_dat[l]))) break; 
            res += width;
            prod = VM::operation(prod, m_dat[l++]);
        }
        // 根から下って、境界を発見する
        while (l = left(l), width >>= 1) {
            if (res + width <= size() and f(VM::operation(prod, m_dat[l]))) {
                res += width;
                prod = VM::operation(prod, m_dat[l++]);
            } 
        }
        return res;
    }

    template <class F>
    requires std::predicate<F, V>
    [[nodiscard]] usize minLeft(usize r, const F& f) const {
        assert(r <= size());
        static_assert(std::is_convertible_v<decltype(f), std::function<bool(V)>>, "minLeft's argument f must be function bool(T)");
        assert(f(VM::identity()));
        usize res{r}, width{1};
        V prod{ VM::identity() };
        for (r += size() ; res >= width ; r = parent(r), width <<= 1) if (r & 1) {
            if (not f(VM::operation(m_dat[r - 1], prod))) break;
            res -= width;
            prod = VM::operation(prod, m_dat[--r]);
        }
        while (r = left(r), width >>= 1) {
            if (res >= width and f(VM::operation(m_dat[r - 1], prod))) {
                res -= width;
                prod = VM::operation(m_dat[--r], prod);
            }
        }
        return res;
    }

    friend std::ostream& operator<<(std::ostream& os, const SegmentTree& st) {
        for (usize i{1} ; i < 2 * st.size() ; i++) {
            os << st.m_dat[i] << (i + 1 == 2 * st.size() ? "" : " ");
        }
        return os;
    }

private:

    constexpr u32 left(u32 v) const {
        return v << 1;
    }

    constexpr u32 right(u32 v) const {
        return v << 1 | 1;
    }

    constexpr u32 parent(u32 v) const {
        return v >> 1;
    }

    usize m_n;

    std::vector<V> m_dat;
};

} // namespace zawa
#line 6 "Src/DataStructure/SegmentTree/AssignmentSegmentTree.hpp"

#line 9 "Src/DataStructure/SegmentTree/AssignmentSegmentTree.hpp"
#include <set>

namespace zawa {

namespace concepts {

template <class T, class U>
concept Powerable = requires {
    typename T::Element;
    { T::power(std::declval<typename T::Element>(), std::declval<U>()) }
        -> std::same_as<typename T::Element>;
};

template <class T>
concept EqualCompare = requires(T a, T b) {
    { a == b } -> std::convertible_to<bool>;
};

template <class T>
concept FastPowerableMonoid = Monoid<T> and Powerable<T, u64>;

} // namespace concepts

template <concepts::Monoid Monoid>
class AssignmentSegmentTree {
public:

    using VM = Monoid;

    using V = typename VM::Element;

    AssignmentSegmentTree() = default;

    explicit AssignmentSegmentTree(usize n) : m_seg{n}, m_dat(n, VM::identity()), m_ls{} {
        m_dat.shrink_to_fit();
        assert(n);
        m_ls.insert(0u);
        m_ls.insert(n);
    }

    explicit AssignmentSegmentTree(std::vector<V> dat) : m_seg{}, m_dat{dat}, m_ls{} {
        // dat: 区間の左端lにa_{l}^{r-l}, それ以外のiはidentity()にする -> セグ木にこれをのせる
        // m_dat: 区間の左端lにa_{l}, それ以外のiはidentity()にする
        m_dat.shrink_to_fit();
        if constexpr (concepts::EqualCompare<V>) {
            for (usize i{}, j{} ; i < m_dat.size() ; ) {
                while (j < dat.size() and dat[i] == dat[j]) j++;
                m_ls.insert(i);
                dat[i] = power(m_dat[i], j - i);
                for ( ; ++i < j ; dat[i] = m_dat[i] = VM::identity()) ;
            }
        }
        else {
            for (usize i{} ; i < m_dat.size() ; i++) m_ls.insert(i);
        }
        m_ls.insert(dat.size());
        m_seg = decltype(m_seg){dat};
    }

    [[nodiscard]] inline usize size() const noexcept {
        return m_dat.size();
    }

    [[nodiscard]] V product(usize l, usize r) const {
        assert(l <= r and r <= size());
        if (l == r) return VM::identity();
        const auto second_l = m_ls.upper_bound(l);
        const auto first_l = std::prev(second_l);
        if (second_l != m_ls.end() and r <= *second_l) { // 一つの区間に含まれている
            return power(m_dat[*first_l], r - l);
        }
        const auto last_l = std::prev(m_ls.upper_bound(r));
        V res = VM::operation(
                power(m_dat[*first_l], *second_l - l),
                m_seg.product(*second_l, *last_l)
                );
        if (r == *last_l) return res;
        return VM::operation(res, power(m_dat[*last_l], r - *last_l));
    }

    void assign(usize l, usize r, V v) {
        assert(l <= r and r <= m_dat.size());
        if (l == r) return;
        // assert(*it_l < n);
        auto it_l = std::prev(m_ls.upper_bound(l)), it_r = std::prev(m_ls.upper_bound(r)); 
        if (*it_l < l) m_seg.assign(*it_l, power(m_dat[*it_l], l - *it_l));
        m_seg.assign(l, power(v, r - l));
        if (*it_r < r and r < m_dat.size()) {
            m_dat[r] = m_dat[*it_r];
            m_seg.assign(r, power(m_dat[r], *std::next(it_r) - r));
            m_ls.insert(r);
        }
        m_dat[l] = v;
        for (it_l++ ; *it_l < r ; it_l = m_ls.erase(it_l)) {
            m_seg.assign(*it_l, VM::identity());
            m_dat[*it_l] = VM::identity();
        }
        m_ls.insert(l);
    }

private:

    SegmentTree<VM> m_seg;

    std::vector<V> m_dat;

    std::set<usize> m_ls; 

    static V power(V v, u32 p) requires concepts::FastPowerableMonoid<VM> {
        return VM::power(v, p);
    }

    static V power(V v, u32 p) {
        V res{VM::identity()};
        while (p) {
            if (p & 1) res = VM::operation(res, v);
            v = VM::operation(v, v);
            p >>= 1; 
        }
        return res;
    }
};

} // namespace zawa
#line 2 "Src/Graph/Tree/HeavyLightDecomposition.hpp"

#line 4 "Src/Graph/Tree/HeavyLightDecomposition.hpp"

#include <algorithm>
#line 7 "Src/Graph/Tree/HeavyLightDecomposition.hpp"
#include <cmath>
#include <limits>
#include <utility>
#line 11 "Src/Graph/Tree/HeavyLightDecomposition.hpp"

namespace zawa {

template <class V>
class HeavyLightDecomposition {
public:

    static constexpr V Invalid() noexcept {
        return INVALID;
    }

    HeavyLightDecomposition() = default;

    HeavyLightDecomposition(std::vector<std::vector<V>> T, V root = 0u) 
        : n_{T.size()}, par_(n_), top_(n_), idx_(n_), 
        inv_(n_), size_(n_, usize{1}), dep_(n_) {

            auto dfs1{[&](auto dfs, V v, V p, usize d) -> usize {
                par_[v] = p;
                dep_[v] = d;
                if (p != INVALID) {
                    for (u32 i{} ; i + 1 < T[v].size() ; i++) if (T[v][i] == p) {
                        std::swap(T[v][i], T[v].back());
                        break;
                    }
                    assert(T[v].back() == p);
                    T[v].pop_back();
                }
                for (V x : T[v]) {
                    size_[v] += dfs(dfs, x, v, d + 1);
                }
                for (u32 i{1} ; i < T[v].size() ; i++) if (size_[T[v][0]] < size_[T[v][i]]) {
                    std::swap(T[v][0], T[v][i]);
                }
                return size_[v];
            }};

            auto dfs2{[&](auto dfs, V v, V idx, V top) -> V {
                idx_[v] = idx++;
                inv_[idx_[v]] = v;
                top_[v] = top;
                if (T[v].size()) {
                    idx = dfs(dfs, T[v][0], idx, top);
                    for (u32 i{1} ; i < T[v].size() ; i++) {
                        idx = dfs(dfs, T[v][i], idx, T[v][i]);
                    }
                }
                return idx;
            }};

            dfs1(dfs1, root, INVALID, 0u);
            dfs2(dfs2, root, 0u, root);
        }

    inline usize size() const noexcept {
        return n_;
    }

    usize size(V v) const noexcept {
        assert(v < (V)size());
        return size_[v];
    }

    usize depth(V v) const noexcept {
        assert(v < (V)size());
        return dep_[v];
    }

    V parent(V v) const noexcept {
        assert(v < (V)size());
        return par_[v];
    }

    V index(V v) const noexcept {
        assert(v < (V)size());
        return idx_[v];
    }

    V operator[](V v) const noexcept {
        assert(v < (V)size());
        return idx_[v];
    }

    std::vector<std::pair<V, V>> decomp(V s, V t) const {
        assert(s < (V)size());
        assert(t < (V)size());
        std::vector<std::pair<V, V>> res, ser;
        while (top_[s] != top_[t]) {
            if (dep_[top_[s]] >= dep_[top_[t]]) {
                res.emplace_back(s, top_[s]);
                s = top_[s];
                if (par_[s] != INVALID) s = par_[s];
            }
            else {
                ser.emplace_back(top_[t], t);
                t = top_[t];
                if (par_[t] != INVALID) t = par_[t];
            }
        }
        res.emplace_back(s, t);
        std::reverse(ser.begin(), ser.end());
        res.insert(res.end(), ser.begin(), ser.end()); 
        return res;
    }

    std::vector<std::pair<V, V>> operator()(V s, V t) const {
        return decomp(s, t);
    }

    V lca(V u, V v) const {
        assert(u < (V)size());
        assert(v < (V)size());
        while (top_[u] != top_[v]) {
            if (dep_[top_[u]] >= dep_[top_[v]]) {
                u = top_[u];
                if (par_[u] != INVALID) u = par_[u];
            }
            else {
                v = top_[v];
                if (par_[v] != INVALID) v = par_[v];
            }
        }
        return (dep_[u] <= dep_[v] ? u : v);
    }

    // pはvの祖先か?
    bool isAncestor(V v, V p) {
        assert(v < size());
        assert(p < size());
        if (dep_[v] < dep_[p]) return false;
        while (v != INVALID and top_[v] != top_[p]) {
            v = par_[top_[v]];
        }
        return v != INVALID;
    }

    V levelAncestor(V v, usize step) const {
        assert(v < (V)size());
        if (step > dep_[v]) return INVALID;
        while (true) {
            usize dist{dep_[v] - dep_[top_[v]]};
            if (dist >= step) break;
            step -= dist + 1;
            v = par_[top_[v]];
        }
        step = (dep_[v] - dep_[top_[v]]) - step;
        return inv_[idx_[top_[v]] + step];
    }

    V jump(V s, V t, usize step) const {
        assert(s < (V)size());
        assert(t < (V)size());
        V uu{INVALID}, vv{INVALID};
        usize d{};
        for (auto [u, v] : decomp(s, t)) {
            usize dist{std::max(dep_[u], dep_[v]) - std::min(dep_[u], dep_[v])};
            if (dist >= step) {
                uu = u;
                vv = v;
                d = dist;
                break;
            }
            step -= dist + 1;
        }
        if (uu == INVALID) return INVALID;
        if (dep_[uu] <= dep_[vv]) {
            return inv_[idx_[uu] + step];
        }
        else {
            return inv_[idx_[vv] + (d - step)];
        }
    }

    usize distance(V s, V t) const {
        assert(s < (V)size());
        assert(t < (V)size());
        usize res{};
        for (auto [u, v] : decomp(s, t)) {
            if (dep_[u] > dep_[v]) std::swap(u, v);
            res += dep_[v] - dep_[u];
        }
        return res;
    }

private:
    static constexpr V INVALID{static_cast<V>(-1)};
    usize n_{};
    std::vector<V> par_{}, top_{}, idx_{}, inv_{};
    std::vector<usize> size_{}, dep_{};
};

} // namespace zawa
#line 2 "Src/Algebra/Monoid/SubarraySumMaxMonoid.hpp"

#line 5 "Src/Algebra/Monoid/SubarraySumMaxMonoid.hpp"
#include <optional>

namespace zawa {

template <std::totally_ordered T>
class SubarraySumMax {
public:

    SubarraySumMax() = default;

    explicit SubarraySumMax(T v) : m_ans{v}, m_sum{v}, m_pref{v}, m_suf{v}, m_entire{true} {}

    SubarraySumMax(T ans, T sum, T pref, T suf, bool entire) : m_ans{ans}, m_sum{sum}, m_pref{pref}, m_suf{suf}, m_entire{entire} {}

    inline T ans() const {
        return m_ans;
    }

    inline T sum() const {
        return m_sum;
    }

    inline T pref() const {
        return m_pref;
    }

    inline T suf() const {
        return m_suf;
    }

    inline bool entire() const {
        return m_entire;
    }

    static SubarraySumMax<T> merge(const SubarraySumMax<T>& lhs, const SubarraySumMax<T>& rhs) {
        T sum = lhs.sum() + rhs.sum();
        T pref = std::max(lhs.pref(), lhs.sum() + rhs.pref());
        T suf = std::max(rhs.suf(), lhs.suf() + rhs.sum());
        T ans = std::max({lhs.ans(), rhs.ans(), lhs.suf() + rhs.pref(), sum});
        bool entire = (ans == sum);
        return {ans, sum, pref, suf, entire};
    }

private:

    T m_ans{}, m_sum{}, m_pref{}, m_suf{};

    bool m_entire{};
};

template <std::totally_ordered T>
struct SubarraySumMaxMonoid {

    using Element = std::optional<SubarraySumMax<T>>;

    static Element identity() {
        return std::nullopt;
    }

    static Element operation(const Element& L, const Element& R) {
        if (!L) return R;
        if (!R) return L;
        return Element::value_type::merge(L.value(), R.value());
    }

    static Element convert(T v) {
        return Element{v};
    }
};

} // namespace zawa
#line 2 "Src/Algebra/Monoid/ReverseOrder.hpp"

#line 4 "Src/Algebra/Monoid/ReverseOrder.hpp"

namespace zawa {

template <concepts::Monoid M>
struct ReverseOrder {

    using Element = M::Element;
    
    static Element identity() {
        return M::identity();
    }

    static Element operation(const Element& L, const Element& R) {
        return M::operation(R, L);
    }

};

} // namespace zawa
#line 7 "Test/AOJ/2450.test.cpp"

#line 9 "Test/AOJ/2450.test.cpp"
#include <iostream>
#line 11 "Test/AOJ/2450.test.cpp"
using namespace std;
using namespace zawa;
using M = SubarraySumMaxMonoid<long long>;

int main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
    int N, Q;
    cin >> N >> Q;
    vector<int> A(N);
    for (int i = 0 ; i < N ; i++) cin >> A[i];
    vector<vector<int>> g(N);
    for (int i = 0 ; i < N - 1 ; i++) {
        int u, v;
        cin >> u >> v;
        u--; v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    HeavyLightDecomposition hld{g};
    vector<M::Element> init(N);
    for (int i = 0 ; i < N ; i++) init[hld[i]] = M::convert(A[i]);
    AssignmentSegmentTree<M> seg{init};
    AssignmentSegmentTree<ReverseOrder<M>> ges{init};
    while (Q--) {
        int T;
        cin >> T;
        int a, b, c;
        cin >> a >> b >> c;
        a--; b--;
        if (T == 1) {
            for (auto [s, t] : hld(a, b)) {
                s = hld[s];
                t = hld[t];
                if (s > t) swap(s, t);
                seg.assign(s, t + 1, M::convert(c));
                ges.assign(s, t + 1, M::convert(c));
            }
        }
        else if (T == 2) {
            M::Element pd = M::identity();
            for (auto [s, t] : hld(a, b)) {
                s = hld[s];
                t = hld[t];
                if (s <= t) pd = M::operation(pd, seg.product(s, t + 1));
                else pd = M::operation(pd, ges.product(t, s + 1));
            }
            assert(pd);
            cout << pd->ans() << '\n';
        }
        else assert(false);
    }
}
Back to top page