Skip to content

Commit

Permalink
acl_beats: add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
hitonanode committed Apr 28, 2024
1 parent 0f84dd1 commit 07bdf7c
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions segmenttree/acl_beats.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once
#include "acl_lazysegtree.hpp"

// CUT begin
template <class S, S (*op)(S, S), S (*e)(), class F, S (*mapping)(F, S), F (*composition)(F, F),
F (*id)()>
class segtree_beats : public atcoder::lazy_segtree<S, op, e, F, mapping, composition, id> {
Expand All @@ -19,12 +18,14 @@ class segtree_beats : public atcoder::lazy_segtree<S, op, e, F, mapping, composi
namespace RangeChMinMaxAddSum {
#include <algorithm>

template <typename Num>
inline Num second_lowest(Num a, Num a2, Num c, Num c2) noexcept { // a < a2, c < c2
template <typename Num> inline Num second_lowest(Num a, Num a2, Num c, Num c2) noexcept {
assert(a <= a2); // a < a2 or a == a2 == INF
assert(c <= c2); // c < c2 or c == c2 == -INF
return a == c ? std::min(a2, c2) : a2 <= c ? a2 : c2 <= a ? c2 : std::max(a, c);
}
template <typename Num>
inline Num second_highest(Num a, Num a2, Num b, Num b2) noexcept { // a > a2, b > b2
template <typename Num> inline Num second_highest(Num a, Num a2, Num b, Num b2) noexcept {
assert(a >= a2); // a > a2 or a == a2 == -INF
assert(b >= b2); // b > b2 or b == b2 == INF
return a == b ? std::max(a2, b2) : a2 >= b ? a2 : b2 >= a ? b2 : std::min(a, b);
}

Expand All @@ -36,7 +37,7 @@ struct S {
unsigned sz, nlo, nhi;
bool fail;
S() : lo(BINF), hi(-BINF), lo2(BINF), hi2(-BINF), sum(0), sz(0), nlo(0), nhi(0), fail(0) {}
S(BNum x, unsigned sz_ = 1)
S(BNum x, unsigned sz_)
: lo(x), hi(x), lo2(BINF), hi2(-BINF), sum(x * sz_), sz(sz_), nlo(sz_), nhi(sz_), fail(0) {}
friend std::ostream &operator<<(std::ostream &os, const S s) {
return os << "[(" << s.lo << "x" << s.nlo << ", " << s.lo2 << ", " << s.hi2 << ", " << s.hi
Expand All @@ -49,10 +50,12 @@ S op(S l, S r) {
if (l.lo > l.hi) return r;
if (r.lo > r.hi) return l;
S ret;
ret.lo = std::min(l.lo, r.lo), ret.hi = std::max(l.hi, r.hi);
ret.lo = std::min(l.lo, r.lo);
ret.hi = std::max(l.hi, r.hi);
ret.lo2 = second_lowest(l.lo, l.lo2, r.lo, r.lo2),
ret.hi2 = second_highest(l.hi, l.hi2, r.hi, r.hi2);
ret.sum = l.sum + r.sum, ret.sz = l.sz + r.sz;
ret.sum = l.sum + r.sum;
ret.sz = l.sz + r.sz;
ret.nlo = l.nlo * (l.lo <= r.lo) + r.nlo * (r.lo <= l.lo);
ret.nhi = l.nhi * (l.hi >= r.hi) + r.nhi * (r.hi >= l.hi);
return ret;
Expand All @@ -76,21 +79,32 @@ F composition(F fnew, F fold) {
F id() { return F(); }
S mapping(F f, S x) {
if (x.sz == 0) return e();
if (x.lo == x.hi or f.lb == f.ub or f.lb >= x.hi or f.ub <= x.lo)

// f の作用後 x の要素が 1 種類だけになるケース
if (x.lo == x.hi or f.lb == f.ub or f.lb >= x.hi or f.ub <= x.lo) {
return S(std::min(std::max(x.lo, f.lb), f.ub) + f.bias, x.sz);
}

// 2 種類 -> 1 種類
if (x.lo2 == x.hi) {
x.lo = x.hi2 = std::max(x.lo, f.lb) + f.bias, x.hi = x.lo2 = std::min(x.hi, f.ub) + f.bias;
x.lo = x.hi2 = std::max(x.lo, f.lb) + f.bias;
x.hi = x.lo2 = std::min(x.hi, f.ub) + f.bias;
x.sum = x.lo * x.nlo + x.hi * x.nhi;
return x;
}

// lo と lo2, hi と hi2 が潰れないケース
if (f.lb < x.lo2 and f.ub > x.hi2) {
BNum nxt_lo = std::max(x.lo, f.lb), nxt_hi = std::min(x.hi, f.ub);
x.sum += (nxt_lo - x.lo) * x.nlo - (x.hi - nxt_hi) * x.nhi + f.bias * x.sz;
x.lo = nxt_lo + f.bias, x.hi = nxt_hi + f.bias, x.lo2 += f.bias, x.hi2 += f.bias;
return x;
}

x.fail = 1;
return x;
}

using segtree = segtree_beats<S, op, e, F, mapping, composition, id>;

} // namespace RangeChMinMaxAddSum

0 comments on commit 07bdf7c

Please sign in to comment.