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: ABC322-F Vacation Query
(Test/AtCoder/abc322_f.test.cpp)

遅延セグ木に乗せる構造を以下の様に設計する

product (値のマージ) では、この意味に沿うようによしなに頑張る。

operation(値の更新)では、01列のflipなのでい つ も ので良い

Depends on

Code

// #define PROBLEM "https://atcoder.jp/contests/abc322/tasks/abc322_f"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/1/ITP1_1_A"

/*
 * https://atcoder.jp/contests/abc322/submissions/67058865
 * AtCoder Beginner Contest 322 F - Vacation Query
 */

#include "../../Src/DataStructure/SegmentTree/LazySegmentTree.hpp"
#include "../../Src/Template/IOSetting.hpp"

#include <utility>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cassert>

using pii = std::pair<int, int>;

void chmax(int& a, int b) {
    a = std::max(a, b);
}

struct vD {
    int max[2]{ 0, 0 };
    pii left{}, right{};
    bool allSame{true};

    vD() = default;

    vD flip() const {
        vD res{};
        res.max[0] = max[1];
        res.max[1] = max[0];
        res.left = pii{left.first ^ 1, left.second};
        res.right = pii{right.first ^ 1, right.second};
        res.allSame = allSame;
        return res;
    }
};

struct vM {
    using Element = vD;
    static vD identity() {
        return vD{};
    }
    static vD operation(const vD& L, const vD& R) {
        vD res{};

        res.max[0] = std::max(L.max[0], R.max[0]);
        res.max[1] = std::max(L.max[1], R.max[1]);

        res.left.first = L.left.first;
        res.left.second = L.left.second;
        if (L.allSame and L.left.first == R.left.first) res.left.second += R.left.second;

        res.right.first = R.right.first;
        res.right.second = R.right.second;
        if (R.allSame and R.right.first == L.right.first) res.right.second += L.right.second;

        res.allSame = L.allSame and R.allSame and L.left.first == R.right.first;

        if (L.right.first == R.left.first) chmax(res.max[L.right.first], L.right.second + R.left.second);
        chmax(res.max[res.left.first], res.left.second);
        chmax(res.max[res.right.first], res.right.second);

        return res;
    }
};

struct oM {
    using Element = bool;
    static bool identity() {
        return false;
    }
    static bool operation(bool l, bool r) {
        return l ^ r;
    }
};

struct Structure {
    using ValueMonoid = vM;
    using OperatorMonoid = oM;
    static vD mapping(const vD& a, bool b) {
        return (b ? a.flip() : a);
    }
};

int main() {
#ifdef ATCODER
    using namespace zawa;
    SetFastIO();

    int n, q; std::cin >> n >> q;
    std::vector<vD> init(n);
    for (int i{} ; i < n ; i++) {
        char c; std::cin >> c;
        if (c == '0') {
            init[i].left = pii{0, 1};
            init[i].right = pii{0, 1};
            init[i].max[0] = 1;
            init[i].max[1] = 0;
            init[i].allSame = true;
        }
        else if (c == '1') {
            init[i].left = pii{1, 1};
            init[i].right = pii{1, 1};
            init[i].max[0] = 0;
            init[i].max[1] = 1;
            init[i].allSame = true;
        }
        else {
            assert(false);
        }
    }

    LazySegmentTree<Structure> seg(init);
    for (int _{} ; _ < q ; _++) {
        int c, l, r; std::cin >> c >> l >> r;
        l--;
        if (c == 1) {
            seg.operation(l, r, true);
        }
        else if (c == 2) {
            auto prod{seg.product(l, r)};
            int ans{prod.max[1]};
            std::cout << ans << '\n';
        }
        else {
            assert(false);
        }
    }
#else
    std::cout << "Hello World\n";
#endif
}
#line 1 "Test/AtCoder/abc322_f.test.cpp"
// #define PROBLEM "https://atcoder.jp/contests/abc322/tasks/abc322_f"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/1/ITP1_1_A"

/*
 * https://atcoder.jp/contests/abc322/submissions/67058865
 * AtCoder Beginner Contest 322 F - Vacation Query
 */

#line 2 "Src/DataStructure/SegmentTree/LazySegmentTree.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/DataStructure/SegmentTree/SegmentTreeConcept.hpp"

#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 4 "Src/DataStructure/SegmentTree/SegmentTreeConcept.hpp"

namespace zawa {

namespace concepts {

template <class T>
concept MonoidWithAction = requires {
    requires Monoid<typename T::ValueMonoid>;
    requires Monoid<typename T::OperatorMonoid>;
    { T::mapping(
            std::declval<typename T::ValueMonoid::Element>(),
            std::declval<typename T::OperatorMonoid::Element>()
            ) } -> std::same_as<typename T::ValueMonoid::Element>; 
};

} // namespace concepts

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

#include <algorithm>
#include <bit>
#include <cassert>
#include <ranges>
#include <tuple>
#include <vector>

namespace zawa {

template <concepts::MonoidWithAction S>
class LazySegmentTree {
public:

    using VM = S::ValueMonoid;

    using V = typename VM::Element;

    using OM = S::OperatorMonoid;

    using O = typename OM::Element;

    LazySegmentTree() = default;

    explicit LazySegmentTree(usize n) 
        : m_n{n}, m_sz{1u << (std::bit_width(n))}, m_dat(m_sz << 1, VM::identity()), m_lazy(m_sz << 1, OM::identity()) {}

    explicit LazySegmentTree(const std::vector<V>& a)
        : m_n{a.size()}, m_sz{1u << (std::bit_width(a.size()))}, m_dat(m_sz << 1, VM::identity()), m_lazy(m_sz << 1, OM::identity()) {
        std::ranges::copy(a, m_dat.begin() + inner_size());
        for (usize i = inner_size() ; --i ; ) recalc(i);
    }

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

    [[nodiscard]] V operator[](usize i) {
        assert(i < size());
        return get(i, 1, 0, inner_size());
    }

    [[nodiscard]] V get(usize i) {
        return (*this)[i];
    }

    [[nodiscard]] V product(usize l, usize r) {
        assert(l <= r and r <= size());
        return product(l, r, 1, 0, inner_size());
    }

    void operation(usize l, usize r, const O& o) {
        assert(l <= r and r <= size());
        return operation(l, r, o, 1, 0, inner_size());
    }

    void assign(usize i, const V& v) {
        assert(i < size());
        assign(i, v, 1, 0, inner_size());
    }

    void operation(usize i, const O& o) {
        assert(i < size());
        operation(i, o, 1, 0, inner_size());
    }

private:

    using NodeInfo = std::tuple<usize, usize, usize>;

public:

    template <class F>
    requires std::predicate<F, V>
    usize maxRight(usize l, F f) {
        assert(l <= size());
        if (!f(VM::identity())) return l;
        if (l == size()) return size();
        std::vector<NodeInfo> ranges;
        partition_range(l, size(), ranges, 1, 0, inner_size());
        V prod = VM::identity();
        for (auto [nd, nl, nr] : ranges) {
            if (!f(VM::operation(prod, m_dat[nd]))) {
                return maxRight(f, prod, nd, nl, nr);
            }
            else {
                prod = VM::operation(prod, m_dat[nd]);
            }
        }
        return size();
    }

    template <class F>
    requires std::predicate<F, V>
    usize minLeft(usize r, F f) {
        assert(r <= size());
        if (!f(VM::identity())) return r;
        if (!r) return 0;
        std::vector<NodeInfo> ranges;
        partition_range(0, r, ranges, 1, 0, inner_size());
        V prod = VM::identity();
        for (auto [nd, nl, nr] : ranges | std::views::reverse) {
            if (!f(VM::operation(m_dat[nd], prod))) {
                return minLeft(f, prod, nd, nl, nr);
            }
            else {
                prod = VM::operation(prod, m_dat[nd]);
            }
        }
        return 0;
    }

private:

    usize m_n{}, m_sz{};

    std::vector<V> m_dat;

    std::vector<O> m_lazy;

    inline usize inner_size() const noexcept {
        return m_sz;
    }
    
    void recalc(usize nd) {
        // assert(nd < inner_size());
        m_dat[nd] = VM::operation(m_dat[nd << 1 | 0], m_dat[nd << 1 | 1]);
    }

    void propagate(usize nd) {
        // assert(nd < inner_size());
        for (usize ch : {nd << 1 | 0, nd << 1 | 1}) {
            m_dat[ch] = S::mapping(m_dat[ch], m_lazy[nd]);
            m_lazy[ch] = OM::operation(m_lazy[ch], m_lazy[nd]);
        }
        m_lazy[nd] = OM::identity();
    }

    V product(usize ql, usize qr, usize nd, usize nl, usize nr) {
        if (qr <= nl or nr <= ql) return VM::identity();
        if (ql <= nl and nr <= qr) return m_dat[nd];
        propagate(nd);
        const usize m = (nl + nr) >> 1;
        return VM::operation(
                product(ql, qr, nd << 1 | 0, nl, m),
                product(ql, qr, nd << 1 | 1, m, nr)
                );
    }

    V get(usize i, usize nd, usize nl, usize nr) {
        if (nd >= inner_size()) return m_dat[nd];
        propagate(nd);
        const usize m = (nl + nr) >> 1;
        return i < m ? get(i, nd << 1 | 0, nl, m) : get(i, nd << 1 | 1, m, nr);
    }

    void operation(usize ql, usize qr, const O& o, usize nd, usize nl, usize nr) {
        if (qr <= nl or nr <= ql) return;
        if (ql <= nl and nr <= qr) {
            m_dat[nd] = S::mapping(m_dat[nd], o);
            m_lazy[nd] = OM::operation(m_lazy[nd], o);
            return;
        }
        propagate(nd);
        const usize m = (nl + nr) >> 1;
        operation(ql, qr, o, nd << 1 | 0, nl, m);
        operation(ql, qr, o, nd << 1 | 1, m, nr);
        recalc(nd);
    }

    void operation(usize i, const O& o, usize nd, usize nl, usize nr) {
        if (nl == i and i + 1 == nr) {
            m_dat[nd] = S::mapping(m_dat[nd], o);
            // 葉頂点なので、lazyへのopは不要
            return;
        }
        propagate(nd); 
        const usize m = (nl + nr) >> 1;
        i < m ? operation(i, o, nd << 1 | 0, nl, m) : operation(i, o, nd << 1 | 1, m, nr);
        recalc(nd);
    }

    void assign(usize i, const V& v, usize nd, usize nl, usize nr) {
        if (nl == i and i + 1 == nr) {
            m_dat[nd] = v;
            return;
        }
        propagate(nd); 
        const usize m = (nl + nr) >> 1;
        i < m ? assign(i, v, nd << 1 | 0, nl, m) : assign(i, v, nd << 1 | 1, m, nr);
        recalc(nd);
    }

    void partition_range(usize ql, usize qr, std::vector<NodeInfo>& res, usize nd, usize nl, usize nr) {
        if (qr <= nl or nr <= ql) return;
        if (ql <= nl and nr <= qr) {
            res.emplace_back(nd, nl, nr);
            return;
        }
        propagate(nd);
        const usize m = (nl + nr) >> 1;
        partition_range(ql, qr, res, nd << 1 | 0, nl, m);
        partition_range(ql, qr, res, nd << 1 | 1, m, nr);
    }

    template <class F>
    requires std::predicate<F, V>
    usize maxRight(F f, const V& prod, usize nd, usize nl, usize nr) {
        if (nd >= inner_size()) return nl;
        propagate(nd);
        const usize m = (nl + nr) >> 1, lch = nd << 1 | 0, rch = nd << 1 | 1;
        return f(VM::operation(prod, m_dat[lch])) ? 
            maxRight(f, VM::operation(prod, m_dat[lch]), rch, m, nr) : maxRight(f, prod, lch, nl, m);
    }

    template <class F>
    requires std::predicate<F, V>
    usize minLeft(F f, const V& prod, usize nd, usize nl, usize nr) {
        if (nd >= inner_size()) return nr;
        propagate(nd);
        const usize m = (nl + nr) >> 1, lch = nd << 1 | 0, rch = nd << 1 | 1;
        return f(VM::operation(m_dat[rch], prod)) ? 
            minLeft(f, VM::operation(m_dat[rch], prod), lch, nl, m) : minLeft(f, prod, rch, m, nr);
    }
};

} // namespace zawa
#line 2 "Src/Template/IOSetting.hpp"

#line 4 "Src/Template/IOSetting.hpp"

#include <iostream>
#include <iomanip>

namespace zawa {

void SetFastIO() {
    std::cin.tie(nullptr)->sync_with_stdio(false);
}

void SetPrecision(u32 dig) {
    std::cout << std::fixed << std::setprecision(dig);
}

} // namespace zawa
#line 11 "Test/AtCoder/abc322_f.test.cpp"

#include <utility>
#line 17 "Test/AtCoder/abc322_f.test.cpp"

using pii = std::pair<int, int>;

void chmax(int& a, int b) {
    a = std::max(a, b);
}

struct vD {
    int max[2]{ 0, 0 };
    pii left{}, right{};
    bool allSame{true};

    vD() = default;

    vD flip() const {
        vD res{};
        res.max[0] = max[1];
        res.max[1] = max[0];
        res.left = pii{left.first ^ 1, left.second};
        res.right = pii{right.first ^ 1, right.second};
        res.allSame = allSame;
        return res;
    }
};

struct vM {
    using Element = vD;
    static vD identity() {
        return vD{};
    }
    static vD operation(const vD& L, const vD& R) {
        vD res{};

        res.max[0] = std::max(L.max[0], R.max[0]);
        res.max[1] = std::max(L.max[1], R.max[1]);

        res.left.first = L.left.first;
        res.left.second = L.left.second;
        if (L.allSame and L.left.first == R.left.first) res.left.second += R.left.second;

        res.right.first = R.right.first;
        res.right.second = R.right.second;
        if (R.allSame and R.right.first == L.right.first) res.right.second += L.right.second;

        res.allSame = L.allSame and R.allSame and L.left.first == R.right.first;

        if (L.right.first == R.left.first) chmax(res.max[L.right.first], L.right.second + R.left.second);
        chmax(res.max[res.left.first], res.left.second);
        chmax(res.max[res.right.first], res.right.second);

        return res;
    }
};

struct oM {
    using Element = bool;
    static bool identity() {
        return false;
    }
    static bool operation(bool l, bool r) {
        return l ^ r;
    }
};

struct Structure {
    using ValueMonoid = vM;
    using OperatorMonoid = oM;
    static vD mapping(const vD& a, bool b) {
        return (b ? a.flip() : a);
    }
};

int main() {
#ifdef ATCODER
    using namespace zawa;
    SetFastIO();

    int n, q; std::cin >> n >> q;
    std::vector<vD> init(n);
    for (int i{} ; i < n ; i++) {
        char c; std::cin >> c;
        if (c == '0') {
            init[i].left = pii{0, 1};
            init[i].right = pii{0, 1};
            init[i].max[0] = 1;
            init[i].max[1] = 0;
            init[i].allSame = true;
        }
        else if (c == '1') {
            init[i].left = pii{1, 1};
            init[i].right = pii{1, 1};
            init[i].max[0] = 0;
            init[i].max[1] = 1;
            init[i].allSame = true;
        }
        else {
            assert(false);
        }
    }

    LazySegmentTree<Structure> seg(init);
    for (int _{} ; _ < q ; _++) {
        int c, l, r; std::cin >> c >> l >> r;
        l--;
        if (c == 1) {
            seg.operation(l, r, true);
        }
        else if (c == 2) {
            auto prod{seg.product(l, r)};
            int ans{prod.max[1]};
            std::cout << ans << '\n';
        }
        else {
            assert(false);
        }
    }
#else
    std::cout << "Hello World\n";
#endif
}
Back to top page