55
66template <class T >
77struct Fenwick {
8+ int n;
89 std::vector<T> c;
10+ Fenwick (void ) = default ;
11+ Fenwick (int len) : n(len + 2 ), c(len + 2 ) {}
12+ inline void init (int _n) { n = _n + 2 , c.resize (n); }
913 inline int lowbit (int x) { return x & -x; }
1014 inline void merge (T &x, T &y) { x = x + y; }
1115 inline T subtract (T x, T y) { return x - y; }
12- inline void update (size_t pos, T x) {
13- for (pos++; pos < c. size () ; pos += lowbit (pos)) merge (c[pos], x);
16+ inline void update (int pos, T x) {
17+ for (pos++; pos < n ; pos += lowbit (pos)) merge (c[pos], x);
1418 }
1519 inline void clear (void ) {
1620 for (auto &x : c) x = T ();
1721 }
18- inline T query (size_t pos) {
22+ inline T query (int pos) {
1923 T ans = T ();
2024 for (pos++; pos; pos ^= lowbit (pos)) merge (ans, c[pos]);
2125 return ans;
2226 }
23- inline T query (size_t l, size_t r) {
24- return l == 0 ? query (r) : subtract (query (r), query (l - 1 ));
27+ inline T query (int l, int r) {
28+ return subtract (query (r), query (l - 1 ));
2529 }
2630 inline int kth (T k) {
2731 int ans = 0 ;
@@ -32,7 +36,6 @@ struct Fenwick {
3236 }
3337 return ans;
3438 }
35- Fenwick (size_t len) : c(len + 2 ) {}
3639};
3740
3841#endif // AFDS_FENWICK
0 commit comments