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/AtCoder/abc292_h.test.cpp

Depends on

Code

#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/1/ITP1_1_A"
// #define PROBLEM "https://atcoder.jp/contests/abc292/tasks/abc292_ex"
#define ERROR 1e-9

#include "../../Src/Template/IOSetting.hpp"
#include "../../Src/Algebra/Monoid/AdditionMonoid.hpp"
#include "../../Src/Algebra/Monoid/MaxMonoid.hpp"
#include "../../Src/Algebra/Monoid/PrefixProductMonoid.hpp"
#include "../../Src/DataStructure/SegmentTree/SegmentTree.hpp"

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

using namespace zawa; 
using D = std::optional<long long>;
using vM = MaxMonoid<long long>;
struct oM {
    using Element = D;
    static D identity() noexcept {
        return 0LL;
    }
    static D operation(const D& l, const D& r) noexcept {
        if (l and r) return l.value() + r.value();
        else return (l ? l : (r ? r : 0LL));
    }
};
using M = PrefixProductMonoid<oM, vM>;

int main() {
#ifdef ATCODER
    SetFastIO();
    SetPrecision(10);

    int n, q;
    long long b;
    std::cin >> n >> b >> q;
    std::vector<M::Element> a(n);
    for (int i{} ; i < n ; i++) {
        long long v; std::cin >> v;
        a[i] = M::Element{D{v - b}, D{v - b}};
    }
    SegmentTree<M> seg(a);
    for (int _{} ; _ < q ; _++) {
        int c; std::cin >> c;
        c--;
        long long x; std::cin >> x;
        x -= b;
        seg.set(c, M::Element{D{x}, D{x}});
        auto r{seg.maxRight(0, [](const M::Element& v) -> bool { return (!(bool)v.prefix() or v.prefix().value() < 0LL); })};
        r = std::min<int>(r + 1, n);
        assert(seg.product(0, r).product().has_value());
        long long sum{seg.product(0, r).product().value()};
        long double ans{(long double)(sum + b * (long long)r) / (long double)r};
        std::cout << ans << '\n';
    }
#else
    std::cout << "Hello World\n";
#endif
}
#line 1 "Test/AtCoder/abc292_h.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/1/ITP1_1_A"
// #define PROBLEM "https://atcoder.jp/contests/abc292/tasks/abc292_ex"
#define ERROR 1e-9

#line 2 "Src/Template/IOSetting.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 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 2 "Src/Algebra/Monoid/AdditionMonoid.hpp"

namespace zawa {

template <class T>
struct AdditionMonoid {
    using Element = T;
    static T identity() noexcept {
        return T{};
    }
    static T operation(const T& a, const T& b) noexcept {
        return a + b;
    }
};

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

#include <algorithm>
#include <limits>
#include <optional>

namespace zawa {

template <class T>
class MaxMonoid {
public:
    using Element = std::optional<T>;
    static constexpr Element identity() noexcept {
        return std::nullopt;
    }
    static constexpr Element operation(const Element& l, const Element& r) noexcept {
        if (l and r) {
            return std::max(l, r);
        }
        else if (l) {
            return l;
        }
        else if (r) {
            return r;
        }
        else {
            return std::nullopt;
        }
    }
};

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

#include <type_traits>

namespace zawa {

template <class Value>
class PrefixProductMonoidData {
    Value product_{}, prefix_{};
public:
    PrefixProductMonoidData() = default;
    PrefixProductMonoidData(const Value& product, const Value& prefix)
        : product_{product}, prefix_{prefix} {}

    inline const Value& product() const noexcept {
        return product_;
    }
    inline const Value& prefix() const noexcept {
        return prefix_;
    }
};

template <class O, class F>
class PrefixProductMonoid {
    static_assert(std::is_same_v<typename O::Element, typename F::Element>);
public:
    using Element = PrefixProductMonoidData<typename O::Element>;
    static Element identity() noexcept {
        return PrefixProductMonoidData{O::identity(), F::identity()};
    }
    static Element operation(const Element& l, const Element& r) noexcept {
        return PrefixProductMonoidData{
            O::operation(l.product(), r.product()),
            F::operation(l.prefix(), O::operation(l.product(), r.prefix()))
        };
    }
};

} // namespace zawa
#line 2 "Src/DataStructure/SegmentTree/SegmentTree.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 5 "Src/DataStructure/SegmentTree/SegmentTree.hpp"

#include <vector>
#include <cassert>
#include <functional>
#line 10 "Src/DataStructure/SegmentTree/SegmentTree.hpp"
#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-- ; 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 Function>
    [[nodiscard]] usize maxRight(usize l, const Function& 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 Function>
    [[nodiscard]] usize minLeft(usize r, const Function& 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 10 "Test/AtCoder/abc292_h.test.cpp"

#line 16 "Test/AtCoder/abc292_h.test.cpp"

using namespace zawa; 
using D = std::optional<long long>;
using vM = MaxMonoid<long long>;
struct oM {
    using Element = D;
    static D identity() noexcept {
        return 0LL;
    }
    static D operation(const D& l, const D& r) noexcept {
        if (l and r) return l.value() + r.value();
        else return (l ? l : (r ? r : 0LL));
    }
};
using M = PrefixProductMonoid<oM, vM>;

int main() {
#ifdef ATCODER
    SetFastIO();
    SetPrecision(10);

    int n, q;
    long long b;
    std::cin >> n >> b >> q;
    std::vector<M::Element> a(n);
    for (int i{} ; i < n ; i++) {
        long long v; std::cin >> v;
        a[i] = M::Element{D{v - b}, D{v - b}};
    }
    SegmentTree<M> seg(a);
    for (int _{} ; _ < q ; _++) {
        int c; std::cin >> c;
        c--;
        long long x; std::cin >> x;
        x -= b;
        seg.set(c, M::Element{D{x}, D{x}});
        auto r{seg.maxRight(0, [](const M::Element& v) -> bool { return (!(bool)v.prefix() or v.prefix().value() < 0LL); })};
        r = std::min<int>(r + 1, n);
        assert(seg.product(0, r).product().has_value());
        long long sum{seg.product(0, r).product().value()};
        long double ans{(long double)(sum + b * (long long)r) / (long double)r};
        std::cout << ans << '\n';
    }
#else
    std::cout << "Hello World\n";
#endif
}
Back to top page