Fu_L's Library

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

View the Project on GitHub Fu-L/cp-library

:heavy_check_mark: CumulativeSum2D
(src/data_structure/cumulative_sum_2d.hpp)

CumulativeSum2D

$2$ 次元累積和を計算するデータ構造です.

事前に add で $2$ 次元配列を作り, build で累積和テーブルの初期化を行ってください.
それ以降は sum で長方形領域の総和を計算できます.

コンストラクタ

CumulativeSum2D<T> cum(int n, int m)

計算量

add

cum.add(int i, int j, T x)

cum[i][j] += x をします.

制約

計算量

build

cum.build()

$2$ 次元累積和テーブルの初期化を行います.
add による配列の構築が終わった後,ちょうど $1$ 度だけ呼び出してください.

計算量

sum

T cum.sum(int li, int lj, int ri, int rj)

長方形領域 $[li, ri) \times [lj, rj)$ における配列 cum の総和を返します.

制約

計算量

get

T cum.get(int i, int j)

cum[i][j] の値を返します.

制約

計算量

Depends on

Verified with

Code

#pragma once
#include "../template/template.hpp"
template <typename T>
struct CumulativeSum2D {
    CumulativeSum2D(const int H, const int W)
        : h(H), w(W), data(H + 1, vector<T>(W + 1, 0)) {}
    void add(const int i, const int j, const T& x) {
        assert(0 <= i and i < h);
        assert(0 <= j and j < w);
        data[i + 1][j + 1] += x;
    }
    void build() {
        for(int i = 1; i < (int)data.size(); ++i) {
            for(int j = 1; j < (int)data[i].size(); ++j) {
                data[i][j] += data[i][j - 1] + data[i - 1][j] - data[i - 1][j - 1];
            }
        }
    }
    T sum(const int li, const int lj, const int ri, const int rj) const {
        assert(0 <= li and li <= ri and ri <= h);
        assert(0 <= lj and lj <= rj and rj <= w);
        return data[ri][rj] - data[li][rj] - data[ri][lj] + data[li][lj];
    }
    T get(const int i, const int j) const {
        assert(0 <= i and i < h);
        assert(0 <= j and j < w);
        return data[i + 1][j + 1] - data[i][j + 1] - data[i + 1][j] + data[i][j];
    }

   private:
    int h, w;
    vector<vector<T>> data;
};
#line 2 "src/template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<long long, long long>;
#define rep(i, a, b) for(long long i = (a); i < (b); ++i)
#define rrep(i, a, b) for(long long i = (a); i >= (b); --i)
constexpr long long inf = 4e18;
struct SetupIO {
    SetupIO() {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout << fixed << setprecision(30);
    }
} setup_io;
#line 3 "src/data_structure/cumulative_sum_2d.hpp"
template <typename T>
struct CumulativeSum2D {
    CumulativeSum2D(const int H, const int W)
        : h(H), w(W), data(H + 1, vector<T>(W + 1, 0)) {}
    void add(const int i, const int j, const T& x) {
        assert(0 <= i and i < h);
        assert(0 <= j and j < w);
        data[i + 1][j + 1] += x;
    }
    void build() {
        for(int i = 1; i < (int)data.size(); ++i) {
            for(int j = 1; j < (int)data[i].size(); ++j) {
                data[i][j] += data[i][j - 1] + data[i - 1][j] - data[i - 1][j - 1];
            }
        }
    }
    T sum(const int li, const int lj, const int ri, const int rj) const {
        assert(0 <= li and li <= ri and ri <= h);
        assert(0 <= lj and lj <= rj and rj <= w);
        return data[ri][rj] - data[li][rj] - data[ri][lj] + data[li][lj];
    }
    T get(const int i, const int j) const {
        assert(0 <= i and i < h);
        assert(0 <= j and j < w);
        return data[i + 1][j + 1] - data[i][j + 1] - data[i + 1][j] + data[i][j];
    }

   private:
    int h, w;
    vector<vector<T>> data;
};
Back to top page