-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdifference.cpp
57 lines (49 loc) · 1.13 KB
/
difference.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <bits/stdc++.h>
template <class T>
class DifferenceArray {
public:
DifferenceArray(size_t size)
: data_(size, 0), diff_(size + 1, 0), diff_applied_(true) {}
DifferenceArray(const std::vector<T>& data)
: data_(data.begin(), data.end()),
diff_(data.size() + 1, 0),
diff_applied_(true) {}
void Add(size_t left, size_t right, T x) {
if (left > right || right >= data_.size()) {
throw std::out_of_range("Invalid range");
}
diff_[left] += x;
diff_[right + 1] -= x;
diff_applied_ = false;
}
const std::vector<T>& GetArray() {
ApplyDiff();
return data_;
}
private:
void ApplyDiff() {
if (diff_applied_)
return;
T acc = 0;
for (size_t i = 0; i < data_.size(); ++i) {
acc += diff_[i];
data_[i] += acc;
diff_[i] = 0;
}
diff_[data_.size()] = 0;
diff_applied_ = true;
}
std::vector<T> data_;
std::vector<T> diff_;
bool diff_applied_;
};
using namespace std;
int main() {
DifferenceArray<int> df(5);
df.Add(0, 1, 3);
df.Add(1, 3, 2);
auto v = df.GetArray();
for (auto& x : v) {
cout << x << " ";
}
}