Skip to content

Commit ba70b60

Browse files
committed
aoj/DSL2B
1 parent 1241c8e commit ba70b60

File tree

1 file changed

+40
-48
lines changed

1 file changed

+40
-48
lines changed

aoj/DSL/DSL2B/main_bit.cpp

Lines changed: 40 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,71 @@
1-
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_B
1+
// https://onlinejudge.u-aizu.ac.jp/problems/DSL_2_B
22

3-
#include <vector>
4-
#include <functional>
3+
#include <bits/stdc++.h>
54

6-
template <class T = long long>
5+
template <class T = int64_t>
76
class BinaryIndexedTree {
87
public:
9-
using F = std::function<T(const T&, const T&)>;
8+
using F = std::function<T(T const&, T const&)>;
109

1110
protected:
12-
int N;
11+
size_t N;
1312
std::vector<T> bit;
14-
T id;
15-
F plus;
16-
F minus;
13+
T empty;
14+
F combine_func;
15+
F remove_func;
1716

1817
public:
19-
// O(N)
18+
// Time: O(N)
2019
BinaryIndexedTree(
21-
int n,
22-
T id = T(),
23-
F plus = std::plus<T>(),
24-
F minus = std::minus<T>()
20+
size_t n = 0,
21+
T empty = T(),
22+
F combine = std::plus<T>(),
23+
F remove = std::minus<T>()
2524
)
2625
: N(n+1)
27-
, bit(n+1, id)
28-
, id(id)
29-
, plus(plus)
30-
, minus(minus)
26+
, bit(n+1, empty)
27+
, empty(empty)
28+
, combine_func(combine)
29+
, remove_func(remove)
3130
{
3231
// Do nothing
3332
}
34-
// O(1)
35-
int size() {
33+
// Time: O(1)
34+
size_t size() const {
3635
return N - 1;
3736
}
38-
// Sum of array[0..index)
39-
// O(logN)
37+
// Fold elements of array[0..index)
4038
// index = [0,N]
41-
T sum(int index) {
42-
if (index < 0 || index > N) throw;
43-
T ans = id;
39+
// Time: O(logN)
40+
T fold(size_t index) const {
41+
if (index > N) throw std::out_of_range("index");
42+
T ans = empty;
4443
for (; index > 0; index -= index & -index) {
45-
ans = plus(ans, bit[index]);
44+
ans = combine_func(ans, bit[index]);
4645
}
4746
return ans;
4847
}
49-
// Sum of array[l, r)
50-
// O(logN)
51-
T sum(int l, int r) {
52-
if (l > r) throw;
53-
return minus(sum(r), sum(l));
48+
// Fold elements of array[l, r)
49+
// l = [0,N]
50+
// r = [l,N]
51+
// Time: O(logN)
52+
T fold(size_t l, size_t r) const {
53+
if (l > N) throw std::out_of_range("l");
54+
if (r < l || r > N) throw std::out_of_range("r");
55+
return remove_func(fold(r), fold(l));
5456
}
55-
// Add value to array[index]
56-
// O(logN)
57+
// Combine given value to array[index]
5758
// index = [0,N)
58-
void add(int index, const T& value) {
59-
if (index < 0 || index >= N) throw;
59+
// Time: O(logN)
60+
void combine(size_t index, T const& value) {
61+
if (index >= N) throw std::out_of_range("index");
6062
for (++index; index < N; index += index & -index) {
61-
bit[index] = plus(bit[index], value);
63+
bit[index] = combine_func(bit[index], value);
6264
}
6365
}
64-
// Set value to array[index]
65-
// O(logN)
66-
// index = [0,N)
67-
void set(int index, const T& value) {
68-
if (index < 0 || index >= N) throw;
69-
T new_value = minus(value, sum(index, index+1));
70-
add(index, new_value);
71-
}
7266
};
7367

7468

75-
#include <iostream>
76-
7769
using namespace std;
7870

7971
int main() {
@@ -89,10 +81,10 @@ int main() {
8981
cin >> c >> x >> y;
9082
if (c == 0) {
9183
--x;
92-
bit.add(x, y);
84+
bit.combine(x, y);
9385
} else if (c == 1) {
9486
--x, --y;
95-
cout << bit.sum(x, y+1) << endl;
87+
cout << bit.fold(x, y+1) << endl;
9688
} else throw;
9789
}
9890

0 commit comments

Comments
 (0)