树状数组模板(具体原理及说明见文末)
void update(vector<int>& a, int pos, int k) {
//更新数组
while (pos <= n) {
a[pos] += k;
pos += lowbit(pos);
}
}
int getsum(vector<int> &a, int pos) {
// 得到从1加到pos的和
int ans = 0;
while (pos > 0) {
ans += a[pos];
pos -= lowbit(pos);
}
return ans;
}
例题:洛谷p3373