This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/3111"
#include "../../Src/Algebra/Monoid/SubarraySumMaxMonoid.hpp"
#include "../../Src/DataStructure/SegmentTree/SegmentTree.hpp"
#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<M::Element> A(N);
for (int i = 0 ; i < N ; i++) {
int a;
cin >> a;
A[i] = M::convert(a);
}
SegmentTree<M> seg{A};
auto prod = [&]() -> long long {
auto pd = seg.product(0, N);
return pd ? max(pd->ans(), 0LL) : 0LL;
};
cout << prod() << '\n';
while (Q--) {
int k, x;
cin >> k >> x;
k--;
seg.assign(k, M::convert(x));
cout << prod() << '\n';
}
}
#line 1 "Test/AOJ/3111.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/3111"
#line 2 "Src/Algebra/Monoid/SubarraySumMaxMonoid.hpp"
#include <algorithm>
#include <concepts>
#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/DataStructure/SegmentTree/SegmentTree.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"
#line 4 "Src/Algebra/Semigroup/SemigroupConcept.hpp"
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>
#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 5 "Test/AOJ/3111.test.cpp"
#include <iostream>
#line 8 "Test/AOJ/3111.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<M::Element> A(N);
for (int i = 0 ; i < N ; i++) {
int a;
cin >> a;
A[i] = M::convert(a);
}
SegmentTree<M> seg{A};
auto prod = [&]() -> long long {
auto pd = seg.product(0, N);
return pd ? max(pd->ans(), 0LL) : 0LL;
};
cout << prod() << '\n';
while (Q--) {
int k, x;
cin >> k >> x;
k--;
seg.assign(k, M::convert(x));
cout << prod() << '\n';
}
}