compro_library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub siro53/compro_library

:warning: data-structure/monoid/kadane.hpp

Code

#pragma once

#include <limits>
#include <algorithm>

// ref: https://hotman78.hatenablog.com/entry/2020/06/17/102519
template <typename T> struct MonoidKadane {
    struct S {
        T lmax, rmax, allmax, sum;
        S() = default;
        S(T lmax, T rmax, T allmax, T sum)
            : lmax(lmax), rmax(rmax), allmax(allmax), sum(sum) {}
    };
    using value_type = S;
    inline static S op(const S &l, const S &r) {
        T lmax = std::max(l.lmax, l.sum + r.lmax);
        T rmax = std::max(r.rmax, l.rmax + r.sum);
        T allmax = std::max({l.allmax, r.allmax, l.rmax + r.lmax});
        T sum = l.sum + r.sum;
        return S(lmax, rmax, allmax, sum);
    }
    inline static S e() {
        T m = std::numeric_limits<T>::min();
        return S(m, m, m, T(0));
    }
};
#line 2 "data-structure/monoid/kadane.hpp"

#include <limits>
#include <algorithm>

// ref: https://hotman78.hatenablog.com/entry/2020/06/17/102519
template <typename T> struct MonoidKadane {
    struct S {
        T lmax, rmax, allmax, sum;
        S() = default;
        S(T lmax, T rmax, T allmax, T sum)
            : lmax(lmax), rmax(rmax), allmax(allmax), sum(sum) {}
    };
    using value_type = S;
    inline static S op(const S &l, const S &r) {
        T lmax = std::max(l.lmax, l.sum + r.lmax);
        T rmax = std::max(r.rmax, l.rmax + r.sum);
        T allmax = std::max({l.allmax, r.allmax, l.rmax + r.lmax});
        T sum = l.sum + r.sum;
        return S(lmax, rmax, allmax, sum);
    }
    inline static S e() {
        T m = std::numeric_limits<T>::min();
        return S(m, m, m, T(0));
    }
};
Back to top page