|
| 1 | +#pragma once |
| 2 | +#include "../acl_beats.hpp" |
| 3 | +#include <algorithm> |
| 4 | +#include <type_traits> |
| 5 | + |
| 6 | +// Verified: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0427 |
| 7 | +template <typename Num, Num INF> struct RangeChmaxAddRangeSum { |
| 8 | + static_assert(std::is_signed<Num>::value, "Num must be signed"); |
| 9 | + |
| 10 | + static Num second_lowest(Num a, Num a2, Num c, Num c2) noexcept { // a < a2, c < c2 |
| 11 | + return a == c ? std::min(a2, c2) : a2 <= c ? a2 : c2 <= a ? c2 : std::max(a, c); |
| 12 | + } |
| 13 | + |
| 14 | + struct S { |
| 15 | + Num lo, lo2, sum; |
| 16 | + unsigned sz, nlo; |
| 17 | + bool fail; |
| 18 | + S() : lo(INF), lo2(INF), sum(0), sz(0), nlo(0), fail(false) {} |
| 19 | + S(Num x, unsigned sz_ = 1) |
| 20 | + : lo(x), lo2(INF), sum(Num(x) * sz_), sz(sz_), nlo(sz_), fail(false) {} |
| 21 | + }; |
| 22 | + |
| 23 | +private: |
| 24 | + static S e() { return S(); } |
| 25 | + |
| 26 | + static S op(S l, S r) { |
| 27 | + S ret; |
| 28 | + ret.lo = std::min(l.lo, r.lo); |
| 29 | + ret.lo2 = second_lowest(l.lo, l.lo2, r.lo, r.lo2); |
| 30 | + ret.sum = l.sum + r.sum, ret.sz = l.sz + r.sz; |
| 31 | + ret.nlo = l.nlo * (l.lo <= r.lo) + r.nlo * (r.lo <= l.lo); |
| 32 | + return ret; |
| 33 | + } |
| 34 | + |
| 35 | + struct F { |
| 36 | + Num lb, bias; |
| 37 | + F() : lb(-INF), bias(0) {} |
| 38 | + F(Num chmax_, Num add) : lb(chmax_), bias(add) {} |
| 39 | + }; |
| 40 | + |
| 41 | + static F id() { return F(); } |
| 42 | + |
| 43 | + static S mapping(F f, S x) { |
| 44 | + if (x.sz == 0) return e(); |
| 45 | + if (f.lb < x.lo2) { |
| 46 | + Num nxt_lo = std::max(x.lo, f.lb); |
| 47 | + x.sum += (nxt_lo - x.lo) * x.nlo + f.bias * x.sz; |
| 48 | + x.lo = nxt_lo + f.bias, x.lo2 += f.bias; |
| 49 | + return x; |
| 50 | + } |
| 51 | + x.fail = 1; |
| 52 | + return x; |
| 53 | + } |
| 54 | + |
| 55 | +public: |
| 56 | + static F composition(F fnew, F fold) { |
| 57 | + F ret; |
| 58 | + ret.lb = std::max(fold.lb + fold.bias, fnew.lb) - fold.bias; |
| 59 | + ret.bias = fold.bias + fnew.bias; |
| 60 | + return ret; |
| 61 | + } |
| 62 | + |
| 63 | + static F Chmax(Num x) noexcept { return F(x, Num(0)); } |
| 64 | + static F Add(Num x) noexcept { return F(-INF, x); }; |
| 65 | + |
| 66 | + static S Gen(Num x, unsigned sz = 1) { return S{x, sz}; } |
| 67 | + |
| 68 | + using segtree = segtree_beats<S, op, e, F, mapping, composition, id>; |
| 69 | +}; |
| 70 | +/* Usage: |
| 71 | +using RCARS = RangeChmaxAddRangeSum<long long, (1LL << 60)>; |
| 72 | +vector<RCARS::S> init; |
| 73 | +for (auto a : A) init.push_back(RCARS::Gen(a, 1)); |
| 74 | +RCARS::segtree tree(init); |
| 75 | +
|
| 76 | +tree.apply(l, r, RCARS::Chmax(x)); |
| 77 | +tree.apply(l, r, RCARS::Add(x)); |
| 78 | +auto p = tree.prod(l, r); |
| 79 | +*/ |
0 commit comments