时间复杂度
修改:
O
(
l
o
g
n
)
O(logn)
O(logn)
求和:
O
(
l
o
g
n
)
O(logn)
O(logn)
模板
int lowbit(int x) {
return x & -x;
}
//修改数据
int add(int x, int c) {
for (int i = x; i <= n; i += lowbit(i)) tr[i] += c;
}
//求和
int sum(int x) {
int res = 0;
for (int i = x; i; i -= lowbit(i)) res += tr[i];
return res;
}
//初始化
for (int i = 1; i <= n; i ++ ) tr[i] = lowbit(i);