ContinueLib

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

View the Project on GitHub Continue765/ContinueLib

:heavy_check_mark: Fenwick2D
(data_structure/fenwick/fenwick_2d.hpp)

Fenwick2D

定义于头文件 fenwick_2d.hpp

template <typename Mono>
struct Fenwick2D;

维护 $h \times w$ 的矩阵 $a$,支持单点聚合与矩形前缀聚合查询,各操作 $O(\log h \log w)$。

模板参数

参数 说明
Mono 聚合运算类型,满足 Monoid 要求

成员类型

成员类型 定义
X typename Mono::Type

成员函数

构造

成员函数 说明
Fenwick2D(int h, int w) 构造 $h \times w$ 个 e() 的矩阵,$O(hw)$
Fenwick2D(const vector<vector<X>>& init) 以 init 构造,$O(hw)$

修改

成员函数 说明
void add(int x, int y, const X& v) $a_{x,y} \leftarrow \text{op}(a_{x,y}, v)$,$O(\log h \log w)$

查询

成员函数 说明
X query(int x, int y) 返回 $[0, x) \times [0, y)$ 的聚合,$O(\log h \log w)$
X query(int x1, int x2, int y1, int y2) 返回 $[x_1, x_2) \times [y_1, y_2)$ 的聚合,$O(\log h \log w)$

注解

四参数 query 需 Mono::inv。

示例

Fenwick2D<AddMono<i64>> f(3, 4);
f.add(1, 2, 5);
i64 s = f.query(3, 4);
i64 r = f.query(0, 2, 1, 3);

Fenwick2D<AddMono<i64>> g(vector<vector<i64>>{ {1, 2}, {3, 4} });
i64 t = g.query(1, 2);

Depends on

Verified with

Code

#pragma once
#include <common.hpp>

template <typename Mono>
struct Fenwick2D {
    using X = typename Mono::Type;

    int h, w;
    vector<X> a;

    Fenwick2D(int h_ = 0, int w_ = 0) : h(h_), w(w_), a((h + 1) * (w + 1), Mono::e()) {}
    Fenwick2D(const vector<vector<X>>& init) : Fenwick2D(init.size(), init[0].size()) {
        for (int x = 0; x < h; x++) {
            assert(init[x].size() == w);
            for (int y = 0; y < w; y++) {
                a[(x + 1) * (w + 1) + (y + 1)] = init[x][y];
            }
        }
        for (int i = 1; i <= h; i++) {
            for (int j = 1; j <= w; j++) {
                int jj = j + (j & -j);
                if (jj <= w) {
                    a[i * (w + 1) + jj] = Mono::op(a[i * (w + 1) + jj], a[i * (w + 1) + j]);
                }
            }
        }
        for (int i = 1; i <= h; i++) {
            for (int j = 1; j <= w; j++) {
                int ii = i + (i & -i);
                if (ii <= h) {
                    a[ii * (w + 1) + j] = Mono::op(a[ii * (w + 1) + j], a[i * (w + 1) + j]);
                }
            }
        }
    }

    void add(int x, int y, const X& v) {
        assert(0 <= x && x < h);
        assert(0 <= y && y < w);
        for (int i = x + 1; i <= h; i += i & -i) {
            for (int j = y + 1; j <= w; j += j & -j) {
                a[i * (w + 1) + j] = Mono::op(a[i * (w + 1) + j], v);
            }
        }
    }

    X query(int x, int y) {
        assert(0 <= x && x <= h);
        assert(0 <= y && y <= w);
        X res = Mono::e();
        for (int i = x; i > 0; i -= i & -i) {
            for (int j = y; j > 0; j -= j & -j) {
                res = Mono::op(res, a[i * (w + 1) + j]);
            }
        }
        return res;
    }

    X query(int x1, int x2, int y1, int y2) {
        assert(0 <= x1 && x1 <= x2 && x2 <= h);
        assert(0 <= y1 && y1 <= y2 && y2 <= w);
        X s22 = query(x2, y2);
        X s12 = query(x1, y2);
        X s21 = query(x2, y1);
        X s11 = query(x1, y1);
        return Mono::op(Mono::op(s22, Mono::inv(s12)), Mono::op(Mono::inv(s21), s11));
    }
};
Back to top page