-
Notifications
You must be signed in to change notification settings - Fork 0
/
fenwick-tree-2d.hpp
291 lines (274 loc) · 8.04 KB
/
fenwick-tree-2d.hpp
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#ifndef FENWICK_TREE_2D_HPP
#define FENWICK_TREE_2D_HPP
#include <vector>
template <typename T, typename V = std::vector<std::vector<T>>>
class FenwickTree2D {
// One-based Fenwick tree with a twist (A[0] saves nums[0])
V A;
public:
typedef typename V::size_type size_type;
typedef T value_type;
private:
static const size_type LSB(const size_type i) {
return i & -i;
}
// Convert A[] in place to Fenwick tree form
static void init(V &A) {
for (size_type yi = 1; yi < A[0].size(); ++yi) {
auto yj = yi + LSB(yi);
if (yj < A[0].size())
A[0][yj] += A[0][yi];
}
for (size_type xi = 1; xi < A.size(); ++xi) {
auto xj = xi + LSB(xi);
if (xj < A.size()) {
A[xj][0] += A[xi][0];
for (size_type yi = 1; yi < A[0].size(); ++yi) {
auto yj = yi + LSB(yi);
if (yj < A[0].size())
A[xj][yj] += A[xi][yi];
}
}
}
}
// Convert back to array of per-element counts
static void fini(V &A) {
for (size_type yi = A[0].size(); yi-- > 1; ) {
auto yj = yi + LSB(yi);
if (yj < A[0].size())
A[0][yj] -= A[0][yi];
}
for (size_type xi = A.size(); xi-- > 1; ) {
auto xj = xi + LSB(xi);
if (xj < A.size()) {
A[xj] -= A[xi];
for (size_type yi = A[0].size(); yi-- > 1; ) {
auto yj = yi + LSB(yi);
if (yj < A[0].size())
A[xj][yj] -= A[xi][yi];
}
}
}
}
public:
FenwickTree2D(size_type m, size_type n):A(m, typename V::value_type(n)) {}
FenwickTree2D(FenwickTree2D &&) = default;
FenwickTree2D(const FenwickTree2D &) = default;
FenwickTree2D(const V &other) : A(other.A) { init(A); }
FenwickTree2D(V &&other) : A(move(other.A)) { init(A); }
operator V () const & {
V ret = A;
fini(ret);
return ret;
}
operator V () && {
fini(A);
return A;
}
#if 0
FenwickTree2D &operator =(FenwickTree2D &&) = default;
FenwickTree2D &operator =(const FenwickTree2D &) = default;
FenwickTree2D &operator =(const V &other) { A = other; init(A); }
FenwickTree2D &operator =(V &&other) { A = move(other); init(A); }
#endif
// Returns the sum of the first i elements (indices 0 to i)
// Equivalent to range_sum(0, i)
T prefix_sum(size_type x, size_type y) const {
// assert(i < A.size());
T sum = A[0][0];
#if 0
for (auto i = x; i != 0; i -= LSB(i))
sum += A[i][0];
#endif
for (auto j = y; j != 0; j -= LSB(j))
sum += A[0][j];
for (auto i = x; i != 0; i -= LSB(i)) {
sum += A[i][0];
for (auto j = y; j != 0; j -= LSB(j))
sum += A[i][j];
}
return sum;
}
// Add delta to element with index i (zero-based)
void add(size_type x, size_type y, T delta) {
// assert(i < A.size());
if (y == 0) {
if (x == 0) {
A[0][0] += delta;
return;
}
for (auto i = x; i < A.size(); i += LSB(i))
A[i][0] += delta;
return;
}
if (x == 0) {
for (auto j = y; j < A[0].size(); j += LSB(j))
A[0][j] += delta;
return;
}
for (auto i = x; i < A.size(); i += LSB(i)) {
for (auto j = y; j < A[0].size(); j += LSB(j))
A[i][j] += delta;
}
}
// returns sum of rectangle (xi + 1, xi + 1) to (xj, yj)
T range_sum(size_type xi, size_type yi, size_type xj, size_type yj) const {
#if 0
T sum = 0;
// assert(i < A.size());
// assert(j < A.size());
// assert(i < j);
for (; j > i; j -= LSB(j))
sum += A[j];
for (; i > j; i -= LSB(i))
sum -= A[i];
return sum;
#else
auto sum = prefix_sum(xj, yj);
sum -= prefix_sum(xi, yj);
sum += prefix_sum(xi, yi);
sum -= prefix_sum(xj, yi);
#endif
return sum;
}
// range sum of (0:yi + 1) to (0:yj)
T range_sum_x0(size_type yi, size_type yj) const {
T sum = 0;
// assert(yi < A[0].size());
// assert(yj < A[0].size());
// assert(yi < yj);
for (; yj > yi; yj -= LSB(yj))
sum += A[0][yj];
for (; yi > yj; yi -= LSB(yi))
sum -= A[0][yi];
return sum;
}
// range sum of (0:yi + 1) to (0:yj)
T range_sum_y0(size_type xi, size_type xj) const {
T sum = 0;
// assert(xi < A.size());
// assert(xj < A.size());
// assert(xi < xj);
for (; xj > xi; xj -= LSB(xj))
sum += A[xj][0];
for (; xi > xj; xi -= LSB(xi))
sum -= A[xi][0];
return sum;
}
T range_sum_closed(size_type xi, size_type yi, size_type xj, size_type yj) const {
auto sum = prefix_sum(xj, yj);
if (xi > 0) {
sum -= prefix_sum(xi - 1, yj);
if (yi > 0)
sum += prefix_sum(xi - 1, yi - 1);
}
if (yi > 0)
sum -= prefix_sum(xj, yi);
}
#if 0
//
T get(size_type i) const {
return i == 0 ? A[0] : range_sum(i - 1, i);
}
#endif
#if 0
//
void set(size_type i, const T v) {
add(i, v - get(i));
}
#endif
#if 0 // broken
template <class... Args>
void emplace_back(Args&&...args) {
A.emplace_back(forward<Args>(args)...);
}
void push_back(T&&x) {
return emplace_back(move(x));
}
void push_back(const T& x) {
return emplace_back(x);
}
#endif
void pop_back() {
A.pop_back();
}
#if 0
// Find the largest idx (i <= idx <= j) with prefix_sum(i) <= value.
// Requires: all values are non-negative
// Regular binary search: O(log^2 j)
size_type range_query_npow2(T value, size_type i, size_type j) const {
while(j > i) {
size_type m = i + (j - i + 1) / 2;
if (prefix_sum(m) > value) {
j = m - 1;
} else {
i = m;
}
}
return i;
}
size_type range_query_npow2(T value, size_type i = 0) const {
return range_query_npow2(value, i, size() - 1);
}
// Find the largest idx (i <= idx <= j) with prefix_sum(idx) <= value.
// Requires: all values are non-negative
// Requires: j is power-of-two
// O(log j)
size_type range_query_pow2(T value, size_type i, size_type j) const {
value -= A[0];
assert("j is power of two" && (j & (j - 1)) == 0);
for (; j > 0; j >>= 1) {
if (i + j < A.size() && A[i + j] <= value) {
value -= A[i + j];
i += j;
}
}
return i;
}
size_type range_query_pow2(T value, size_type i = 0) const {
return range_query_pow2(value, i, size() - 1);
}
size_type range_query(T value, size_type i, size_type j) const {
return (j & (j - 1)) ? range_query_npow2(i, j) : range_query_pow2(i, j);
}
size_type range_query(T value, size_type i = 0) const {
return range_query(value, i, size() - 1);
}
size_type size() const {
return A.size();
}
bool empty() const {
return A.empty();
}
private:
struct FenwickReference {
FenwickTree &f;
size_type index;
FenwickReference(FenwickTree &f, size_type index) : f(f), index(index) {}
FenwickReference(const FenwickReference & fr) : FenwickReference(fr.f, fr.index) {}
operator T() const {
return f.get(index);
}
FenwickReference &operator =(T v) {
f.set(index, v);
return *this;
}
T operator = (const FenwickReference &fr) {
return *this = T(fr);
}
bool operator == (const FenwickReference &fr) {
return T(*this) == T(fr);
}
bool operator < (const FenwickReference &fr) {
return T(*this) < T(fr);
}
};
public:
FenwickReference operator [](size_type index) {
return FenwickReference(*this, index);
}
FenwickReference front() { return (*this)[0]; }
FenwickReference back() { return (*this)[size() - 1]; }
#endif
};
#endif // FENWICK_TREE_2D_HPP