cp-library-cpp

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

View the Project on GitHub suisen-cp/cp-library-cpp

:warning: 2D 累積和
(library/util/cumulative_sum_2d.hpp)

2D 累積和

Depends on

Code

#ifndef SUISEN_CUMULATIVE_SUM_2D
#define SUISEN_CUMULATIVE_SUM_2D

#include <cassert>
#include <vector>
#include "library/util/default_operator.hpp"

namespace suisen {
    template <typename T, auto zero = default_operator::zero<T>, auto add = default_operator::add<T>, auto sub = default_operator::sub<T>>
    struct CumulativeSum2D {
        CumulativeSum2D() = default;
        CumulativeSum2D(const std::vector<std::vector<T>> &a) : n(a.size()), m(n == 0 ? 0 : a[0].size()), s(n + 1, std::vector<T>(m + 1, zero())) {
            for (size_t i = 0; i < n; ++i) for (size_t j = 0; j < m; ++j) {
                s[i + 1][j + 1] = sub(add(add(a[i][j], s[i + 1][j]), s[i][j + 1]), s[i][j]);
            }
        }
        T operator()(size_t u, size_t d, size_t l, size_t r) const {
            if (not (u <= d and d <= n and l <= r and r <= m)) return zero();
            return sub(add(s[d][r], s[u][l]), add(s[u][r], s[d][l]));
        }
    private:
        size_t n, m;
        std::vector<std::vector<T>> s;
    };
}

#endif // SUISEN_CUMULATIVE_SUM_2D
#line 1 "library/util/cumulative_sum_2d.hpp"



#include <cassert>
#include <vector>
#line 1 "library/util/default_operator.hpp"



namespace suisen {
    namespace default_operator {
        template <typename T>
        auto zero() -> decltype(T { 0 }) { return T { 0 }; }
        template <typename T>
        auto one()  -> decltype(T { 1 }) { return T { 1 }; }
        template <typename T>
        auto add(const T &x, const T &y) -> decltype(x + y) { return x + y; }
        template <typename T>
        auto sub(const T &x, const T &y) -> decltype(x - y) { return x - y; }
        template <typename T>
        auto mul(const T &x, const T &y) -> decltype(x * y) { return x * y; }
        template <typename T>
        auto div(const T &x, const T &y) -> decltype(x / y) { return x / y; }
        template <typename T>
        auto mod(const T &x, const T &y) -> decltype(x % y) { return x % y; }
        template <typename T>
        auto neg(const T &x) -> decltype(-x) { return -x; }
        template <typename T>
        auto inv(const T &x) -> decltype(one<T>() / x)  { return one<T>() / x; }
    } // default_operator
    namespace default_operator_noref {
        template <typename T>
        auto zero() -> decltype(T { 0 }) { return T { 0 }; }
        template <typename T>
        auto one()  -> decltype(T { 1 }) { return T { 1 }; }
        template <typename T>
        auto add(T x, T y) -> decltype(x + y) { return x + y; }
        template <typename T>
        auto sub(T x, T y) -> decltype(x - y) { return x - y; }
        template <typename T>
        auto mul(T x, T y) -> decltype(x * y) { return x * y; }
        template <typename T>
        auto div(T x, T y) -> decltype(x / y) { return x / y; }
        template <typename T>
        auto mod(T x, T y) -> decltype(x % y) { return x % y; }
        template <typename T>
        auto neg(T x) -> decltype(-x) { return -x; }
        template <typename T>
        auto inv(T x) -> decltype(one<T>() / x)  { return one<T>() / x; }
    } // default_operator
} // namespace suisen


#line 7 "library/util/cumulative_sum_2d.hpp"

namespace suisen {
    template <typename T, auto zero = default_operator::zero<T>, auto add = default_operator::add<T>, auto sub = default_operator::sub<T>>
    struct CumulativeSum2D {
        CumulativeSum2D() = default;
        CumulativeSum2D(const std::vector<std::vector<T>> &a) : n(a.size()), m(n == 0 ? 0 : a[0].size()), s(n + 1, std::vector<T>(m + 1, zero())) {
            for (size_t i = 0; i < n; ++i) for (size_t j = 0; j < m; ++j) {
                s[i + 1][j + 1] = sub(add(add(a[i][j], s[i + 1][j]), s[i][j + 1]), s[i][j]);
            }
        }
        T operator()(size_t u, size_t d, size_t l, size_t r) const {
            if (not (u <= d and d <= n and l <= r and r <= m)) return zero();
            return sub(add(s[d][r], s[u][l]), add(s[u][r], s[d][l]));
        }
    private:
        size_t n, m;
        std::vector<std::vector<T>> s;
    };
}
Back to top page