cp-library

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

View the Project on GitHub Hoshinonono/cp-library

:heavy_check_mark: 2次元いもす法
(DataStructure/CumulativeSum2D_imos.hpp)

概要

平面加算・1点収得を行うための構造体。 動作については いもす法 - いもす研 を参照。

関数名など 機能 計算量
(1) imos2D<T>(int H, int W) +- が定義されている構造体を載せることができる。
$H \times W$ の配列を初期値 $0$ で作成する。
$\text{O}(HW)$
void add(int ly, int lx, int ry, int rx, T v) ・ $ly \leq y < ry$
・ $lx \leq x < rx$
を満たす領域に $\text{v}$ を加算する。
$\text{O}(1)$
void build() 平面加算の結果を反映させ、1点収得ができるようにする。 $\text{O}(HW)$
(1) T a[y][x]
(2) std::vector<T> a[y]
(1) a[y][x] へのアクセスを行う。
(2) a[y] へのアクセスを行う。
$\text{O}(1)$

Verified with

Code

template <class T> struct imos2D{
    int h, w;
    std::vector<std::vector<T>> dat;
    imos2D(int H, int W) : h(H), w(W), dat(H + 1, std::vector<T>(W + 1, 0)) {}
    void add(int ly, int lx, int ry, int rx, T v){
        assert(0 <= ly && ly <= ry && ry <= h);
        assert(0 <= lx && lx <= rx && rx <= w);
        dat[ry][rx] += v;
        dat[ly][rx] -= v;
        dat[ry][lx] -= v;
        dat[ly][lx] += v;
    }
    void build(){
        for(int i = 0; i <= h; i++) {
            for(int j = 1; j <= w; j++) {
                dat[i][j] += dat[i][j - 1];
            }
        }
        for(int i = 0; i <= w; i++) {
            for(int j = 1; j <= h; j++) {
                dat[j][i] += dat[j - 1][i];
            }
        }
    }
    const std::vector<T>& operator[](int y) const {
        assert(0 <= y && y < h);
        return dat[y];
    }
    std::vector<T>& operator[](int y) { 
        assert(0 <= y && y < h);
        return dat[y];
    }
};
#line 1 "DataStructure/CumulativeSum2D_imos.hpp"
template <class T> struct imos2D{
    int h, w;
    std::vector<std::vector<T>> dat;
    imos2D(int H, int W) : h(H), w(W), dat(H + 1, std::vector<T>(W + 1, 0)) {}
    void add(int ly, int lx, int ry, int rx, T v){
        assert(0 <= ly && ly <= ry && ry <= h);
        assert(0 <= lx && lx <= rx && rx <= w);
        dat[ry][rx] += v;
        dat[ly][rx] -= v;
        dat[ry][lx] -= v;
        dat[ly][lx] += v;
    }
    void build(){
        for(int i = 0; i <= h; i++) {
            for(int j = 1; j <= w; j++) {
                dat[i][j] += dat[i][j - 1];
            }
        }
        for(int i = 0; i <= w; i++) {
            for(int j = 1; j <= h; j++) {
                dat[j][i] += dat[j - 1][i];
            }
        }
    }
    const std::vector<T>& operator[](int y) const {
        assert(0 <= y && y < h);
        return dat[y];
    }
    std::vector<T>& operator[](int y) { 
        assert(0 <= y && y < h);
        return dat[y];
    }
};
Back to top page