This documentation is automatically generated by online-judge-tools/verification-helper
#include "src/data_structure/fenwick_tree_2d.hpp"
サイズが $N \times M$ である $2$ 次元配列に対し,
を $O(\log N \log M)$ 時間で処理することができます.
FenwickTree2D<T> fw(int n, int m)
T
でサイズが $n \times m$ である$2$ 次元配列 fw
を作ります.計算量
void fw.add(int i, int j, T x)
fw[i][j] += x
をします.
制約
計算量
T fw.sum(int li, int lj, int ri, int rj)
長方形領域 $[li, ri) \times [lj, rj)$ における配列 fw
の総和を返します.
制約
計算量
T fw.get(int i, int j)
fw[i][j]
を返します.
制約
計算量
#pragma once
#include "../template/template.hpp"
template <typename T>
struct FenwickTree2D {
FenwickTree2D(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& z) {
assert(0 <= i and i < h);
assert(0 <= j and j < w);
for(int x = i + 1; x <= h; x += x & -x) {
for(int y = j + 1; y <= w; y += y & -y) {
data[x - 1][y - 1] += z;
}
}
}
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 sum(ri, rj) - sum(li, rj) - sum(ri, lj) + sum(li, lj);
}
T get(const int i, const int j) const {
assert(0 <= i and i < h);
assert(0 <= j and j < w);
return sum(i + 1, j + 1) - sum(i, j + 1) - sum(i + 1, j) + sum(i, j);
}
private:
int h, w;
vector<vector<T>> data;
inline T sum(const int i, const int j) const {
T s = 0;
for(int x = i; x > 0; x -= x & -x) {
for(int y = j; y > 0; y -= y & -y) {
s += data[x - 1][y - 1];
}
}
return s;
}
};
#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/fenwick_tree_2d.hpp"
template <typename T>
struct FenwickTree2D {
FenwickTree2D(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& z) {
assert(0 <= i and i < h);
assert(0 <= j and j < w);
for(int x = i + 1; x <= h; x += x & -x) {
for(int y = j + 1; y <= w; y += y & -y) {
data[x - 1][y - 1] += z;
}
}
}
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 sum(ri, rj) - sum(li, rj) - sum(ri, lj) + sum(li, lj);
}
T get(const int i, const int j) const {
assert(0 <= i and i < h);
assert(0 <= j and j < w);
return sum(i + 1, j + 1) - sum(i, j + 1) - sum(i + 1, j) + sum(i, j);
}
private:
int h, w;
vector<vector<T>> data;
inline T sum(const int i, const int j) const {
T s = 0;
for(int x = i; x > 0; x -= x & -x) {
for(int y = j; y > 0; y -= y & -y) {
s += data[x - 1][y - 1];
}
}
return s;
}
};