Skip to content

Commit 2e2f2a6

Browse files
committed
style: removed Fenwick<T>::merge, Fenwick<T>::subtract
1 parent e212976 commit 2e2f2a6

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

src/alfred/data_structure/fenwick.hpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,25 @@ struct Fenwick {
1111
Fenwick(int len) : n(len + 2), c(len + 2) {}
1212
inline void init(int _n) { n = _n + 2, c.resize(n); }
1313
inline int lowbit(int x) { return x & -x; }
14-
inline void merge(T &x, T &y) { x = x + y; }
15-
inline T subtract(T x, T y) { return x - y; }
1614
inline void update(int pos, T x) {
17-
for (pos++; pos < n; pos += lowbit(pos)) merge(c[pos], x);
15+
if (++pos >= n) return;
16+
for (; pos < n; pos += lowbit(pos)) {
17+
c[pos] += x;
18+
}
1819
}
1920
inline void clear(void) {
2021
for (auto &x : c) x = T();
2122
}
2223
inline T query(int pos) {
2324
T ans = T();
24-
for (pos++; pos; pos ^= lowbit(pos)) merge(ans, c[pos]);
25+
if (++pos >= n) pos = n - 1;
26+
for (; pos; pos ^= lowbit(pos)) {
27+
ans += c[pos];
28+
}
2529
return ans;
2630
}
2731
inline T query(int l, int r) {
28-
return subtract(query(r), query(l - 1));
32+
return query(r) - query(l - 1);
2933
}
3034
inline int kth(T k) {
3135
int ans = 0;

0 commit comments

Comments
 (0)