跳到正文

个人记录

数据结构模版

AKCoder约 1 分钟
原文
文章信息与版本

文章信息

发布
2025年11月26日
收录
2025年12月12日
更新
2026年11月17日
篇幅
约 1 分钟

版本

  1. v2当前
  2. v1

Fenwick_Tree

C++
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; }
};
原文