标签:sum char main res cpp upd http include ret
用一个数组记录lowbit的值
#include <stdio.h>
#include <string.h>
int n, k;
char st;
int arr[500005], lowbit[500005];
void update(int x, int y) {
while (x <= n) {
arr[x] += y;
x += lowbit[x];
}
}
int sum(int x) {
int res = 0;
while (x) {
res += arr[x];
x -= lowbit[x];
}
return res;
}
int main() {
scanf("%d %d\n", &n, &k);
for (int i = 1; i <= n; ++i) {
lowbit[i] = i & (-i);
}
for (int i = 1, a, b; i <= k; ++i) {
st = getchar();
// putchar(st);
if (st == 'A') {
scanf("%d\n", &a);
printf("%d\n", sum(a));
} else if (st == 'B') {
scanf("%d %d\n", &a, &b);
update(a, b);
} else {
scanf("%d %d\n", &a, &b);
update(a, -b);
}
}
return 0;
}
标签:sum char main res cpp upd http include ret
原文地址:https://www.cnblogs.com/liuzz-20180701/p/11488457.html