int ct[MAX_N];
int lowbit(int x) {
return x & (-x);
}
void init(int n) {
for (int i = 1; i <= n; i++) ct[i] = 0;
}
void add(int x, int v) {
for (int i = x; i <= n; i += lowbit(i)) {
ct[i] += v;
}
}
int sum(int x) {
int ret = 0;
for(int i = x; i >= 1; i -= lowbit(i)) {
ret += ct[i];
}
return ret;
}
树状数组模板
![](https://img-home.csdnimg.cn/images/20240709112858.png)