数据结构模版

· · 个人记录

Fenwick_Tree


template<class T = long long> class Fenwick {
public:
    Fenwick(int n_) : n(n_), bit(n + 1, T{}) {}
    void modify(int x, T v) {
        assert(x > 0);
        for (; x <= n; x += lowbit(x)) bit[x] += v;
    }
    T query(int x) {
        assert(x > 0); T res = 0;
        for (; x; x -= lowbit(x)) res += bit[x];
        return res;
    }
    T query(int l, int r) {
        if (l > r) return 0;
        if (l == 1) return query(r);
        else return query(r) - query(l - 1);
    }
private:
    int n;
    vector<T> bit;
    int lowbit(int x) { return x & -x; }
};