Skip to content

Commit 1241c8e

Browse files
committed
Add tests of binary-indexed-tree
1 parent 6922d7a commit 1241c8e

File tree

4 files changed

+99
-10
lines changed

4 files changed

+99
-10
lines changed

lib/cpalgo/ds/binary_indexed_tree.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// Update: O(logN)
1313
//
1414
// Verified:
15-
// - http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_B
15+
// - https://onlinejudge.u-aizu.ac.jp/problems/DSL_2_B
1616
//
1717
template <class T = int64_t>
1818
class BinaryIndexedTree {
@@ -58,11 +58,11 @@ class BinaryIndexedTree {
5858
return ans;
5959
}
6060
// Fold elements of array[l, r)
61-
// l = [0,N)
61+
// l = [0,N]
6262
// r = [l,N]
6363
// Time: O(logN)
6464
T fold(size_t l, size_t r) const {
65-
if (l >= N) throw std::out_of_range("l");
65+
if (l > N) throw std::out_of_range("l");
6666
if (r < l || r > N) throw std::out_of_range("r");
6767
return remove_func(fold(r), fold(l));
6868
}
@@ -75,4 +75,4 @@ class BinaryIndexedTree {
7575
bit[index] = combine_func(bit[index], value);
7676
}
7777
}
78-
};
78+
};

lib/cpalgo/ds/binary_indexed_tree_2d.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ class BinaryIndexedTree2D {
6262
return ans;
6363
}
6464
// Fold elements of array[[xl,xr),[yl,yr)]
65-
// xl = [0,N), xr = [xl,N]
66-
// yl = [0,M), yr = [yl,M]
65+
// xl = [0,N], xr = [xl,N]
66+
// yl = [0,M], yr = [yl,M]
6767
// Time: O(logN logM logNM)
6868
T fold(size_t xl, size_t xr, size_t yl, size_t yr) const {
69-
if (xl >= N) throw std::out_of_range("xl");
69+
if (xl > N) throw std::out_of_range("xl");
7070
if (xr < xl || xr > N) throw std::out_of_range("xr");
71-
if (yl >= M) throw std::out_of_range("yl");
71+
if (yl > M) throw std::out_of_range("yl");
7272
if (yr < yl || yr > M) std::out_of_range("yr");
7373
T ans = empty;
7474
ans = combine_func(ans, fold(xr, yr));
@@ -82,7 +82,7 @@ class BinaryIndexedTree2D {
8282
// Time: O(logN logN logNM)
8383
void combine(size_t x, size_t y, T const& value) {
8484
if (x >= N) throw std::out_of_range("x");
85-
if ( y >= M) throw std::out_of_range("y");
85+
if (y >= M) throw std::out_of_range("y");
8686
for (size_t i = x + 1; i < N; i += i & -i) {
8787
for (size_t j = y + 1; j < M; j += j & -j) {
8888
if (!bit.count({i,j})) {
@@ -92,4 +92,4 @@ class BinaryIndexedTree2D {
9292
}
9393
}
9494
}
95-
};
95+
};
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <bits/stdc++.h>
2+
#include "gtest/gtest.h"
3+
#include "cpalgo/ds/binary_indexed_tree_2d.hpp"
4+
5+
6+
TEST(BinaryIndexedTree2DTest, ShouldHandleEmpty) {
7+
BinaryIndexedTree2D<int> tree;
8+
auto expected_size = std::make_pair(size_t(0), size_t(0));
9+
EXPECT_EQ(expected_size, tree.size());
10+
}
11+
12+
TEST(BinaryIndexedTree2DTest, ShouldReturnSize) {
13+
auto tree = BinaryIndexedTree2D<int>(5, 10);
14+
auto expected_size = std::make_pair(size_t(5), size_t(10));
15+
EXPECT_EQ(expected_size, tree.size());
16+
}
17+
18+
TEST(BinaryIndexedTree2DTest, ShouldCombineAndFold) {
19+
const size_t N = 30, M = 20;
20+
auto tree = BinaryIndexedTree2D<int>(N, M);
21+
22+
for (size_t x = 0; x < N; ++x) {
23+
for (size_t y = 0; y < M; ++y) {
24+
tree.combine(x, y, x+1);
25+
}
26+
}
27+
28+
for (size_t x = 0; x <= N; ++x) {
29+
for (size_t y = 0; y <= M; ++y) {
30+
int expected_sum = x * (x + 1) / 2 * y;
31+
EXPECT_EQ(expected_sum, tree.fold(x, y));
32+
}
33+
}
34+
35+
36+
for (size_t xl = 0; xl <= N; ++xl) {
37+
for (size_t xr = xl; xr <= N; ++xr) {
38+
for (size_t yl = 0; yl <= M; ++yl) {
39+
for (size_t yr = yl; yr <= M; ++yr) {
40+
int expected_sum =
41+
xr * (xr + 1) / 2 * yr
42+
- xr * (xr + 1) / 2 * yl
43+
- xl * (xl + 1) / 2 * yr
44+
+ xl * (xl + 1) / 2 * yl;
45+
EXPECT_EQ(expected_sum, tree.fold(xl, xr, yl, yr));
46+
}
47+
}
48+
}
49+
}
50+
51+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <bits/stdc++.h>
2+
#include "gtest/gtest.h"
3+
#include "cpalgo/ds/binary_indexed_tree.hpp"
4+
5+
6+
TEST(BinaryIndexedTreeTest, ShouldHandleEmpty) {
7+
BinaryIndexedTree<int> tree;
8+
EXPECT_EQ(0ULL, tree.size());
9+
}
10+
11+
TEST(BinaryIndexedTreeTest, ShouldReturnSize) {
12+
auto tree = BinaryIndexedTree<int>(5);
13+
EXPECT_EQ(5ULL, tree.size());
14+
}
15+
16+
TEST(BinaryIndexedTreeTest, ShouldCombineAndFold) {
17+
const size_t N = 1000;
18+
auto tree = BinaryIndexedTree<int64_t>(N);
19+
20+
for (size_t i = 0; i < N; ++i) {
21+
tree.combine(i, i+1);
22+
}
23+
24+
for (size_t i = 0; i <= N; ++i) {
25+
int64_t expected_sum = i * (i + 1) / 2;
26+
EXPECT_EQ(expected_sum, tree.fold(i));
27+
}
28+
29+
for (size_t l = 0; l <= N; ++l) {
30+
for (size_t r = l; r <= N; ++r) {
31+
int64_t lsum = l * (l + 1) / 2;
32+
int64_t rsum = r * (r + 1) / 2;
33+
auto expected_sum = rsum - lsum;
34+
EXPECT_EQ(expected_sum, tree.fold(l,r));
35+
}
36+
}
37+
38+
}

0 commit comments

Comments
 (0)