Fenwick Tree/Binary Indexed Tree
// 模板来源 https://leetcode.cn/circle/discuss/mOr1u6/
// 根据题目用 FenwickTree<int> t(n) 或者 FenwickTree<long long> t(n) 初始化
template<typename T>
class FenwickTree {
vector<T> tree;
public:
// 使用下标 1 到 n
FenwickTree(int n) : tree(n + 1) {}
// a[i] 增加 val
// 1 <= i <= n
// 时间复杂度 O(log n)
void update(int i, T val) {
for (; i < tree.size(); i += i & -i) {
tree[i] += val;
}
}
// 求前缀和 a[1] + ... + a[i]
// 1 <= i <= n
// 时间复杂度 O(log n)
T pre(int i) const {
T res = 0;
for (; i > 0; i -= (i & -i)) {
res += tree[i];
}
return res;
}
// 求區間
// 0 <= l <= r <= n - 1
// 时间复杂度 O(log n)
//usage: [l, r] -> query(l, r)
T query(int l, int r) const {
if (r < l) {
return 0;
}
return pre(r) - pre(l - 1);
}
};
int mapping(int i) {
return i + 1;
}