File tree Expand file tree Collapse file tree
src/alfred/data_structure Expand file tree Collapse file tree Original file line number Diff line number Diff 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 ;
You can’t perform that action at this time.
0 commit comments