1
0
Fork 0

Create BIT2d.cc

This commit is contained in:
Ariel 2024-03-06 14:00:42 +08:00 committed by GitHub
parent f5f0ca32b2
commit 93b42cbe81
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 28 additions and 0 deletions

28
trees/BIT2d.cc Normal file
View File

@ -0,0 +1,28 @@
template<typename T>
struct BIT2d {
int n, m;
vector<vector<T>> c;
BIT2d(size_t n, size_t m) : n(n), m(m), c(n + 1, vector<T>(m + 1)) {}
void add(size_t i, size_t j, const T& k) {
while (i <= n) {
size_t j1 = j;
while (j1 <= m) {
c[i][j1] += k;
j1 += lowbit(j1);
}
i += lowbit(i);
}
}
T getsum(size_t i, size_t j) {
T res = {};
while (i) {
size_t j1 = j;
while (j1) {
res += c[i][j1];
j1 -= lowbit(j1);
}
i -= lowbit(i);
}
return res;
}
};