Update fenwick_tree_2d.cpp
This commit is contained in:
parent
d9d672e7ae
commit
28aa8c28f6
1 changed files with 6 additions and 6 deletions
|
@ -1,24 +1,24 @@
|
||||||
class fenwick_tree_2d {
|
template<typename T> class fenwick_tree_2d {
|
||||||
private: int N, M; vector<vector<int>> FT;
|
private: int N, M; vector<vector<T>> FT;
|
||||||
public:
|
public:
|
||||||
fenwick_tree_2d(int n, int m) {
|
fenwick_tree_2d(int n, int m) {
|
||||||
N = n + 1, M = m + 1;
|
N = n + 1, M = m + 1;
|
||||||
FT.resize(N);
|
FT.resize(N);
|
||||||
for (int i = 0; i < N; i++) FT[i].resize(M, 0);
|
for (int i = 0; i < N; i++) FT[i].resize(M, 0);
|
||||||
}
|
}
|
||||||
void update(int x, int y, int val) {
|
void update(int x, int y, T val) {
|
||||||
for (int i = x; i < N; i += i & -i) {
|
for (int i = x; i < N; i += i & -i) {
|
||||||
for (int j = y; j < M; j += j & -j) FT[i][j] += val;
|
for (int j = y; j < M; j += j & -j) FT[i][j] += val;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
int query(int x, int y) {
|
T query(int x, int y) {
|
||||||
int ret = 0;
|
T ret = 0;
|
||||||
for (int i = x; i > 0; i -= i & -i) {
|
for (int i = x; i > 0; i -= i & -i) {
|
||||||
for (int j = y; j > 0; j -= j & -j) ret += FT[i][j];
|
for (int j = y; j > 0; j -= j & -j) ret += FT[i][j];
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
int query(int xl, int xr, int yl, int yr) {
|
T query(int xl, int xr, int yl, int yr) {
|
||||||
return query(xr, yr) - query(xl - 1, yr) - query(xr, yl - 1) + query(xl - 1, yl - 1);
|
return query(xr, yr) - query(xl - 1, yr) - query(xr, yl - 1) + query(xl - 1, yl - 1);
|
||||||
}
|
}
|
||||||
};
|
};
|
Loading…
Reference in a new issue