树状数组裸题
直接进行树状数组即可,线段树当然也行(但是树状数组简单呀)
模板:
struct BIT {
int tr[N];
int lowbit(int x) { return x & (-x); }
void modify(int x, int c) {
for (; x <= n; x += lowbit(x)) {
tr[x] += c;
}
}
int check(int x) {
int ans = 0;
for (; x; x -= lowbit(x)) {
ans += tr[x];
};
return ans;
}
} TR;
之后就是这题代码了,,,
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
int n, m, tmp, p;
char opt[2];
struct BIT {
int tr[N];
int lowbit(int x) { return x & (-x); }
void modify(int x, int c) {
for (; x <= n; x += lowbit(x)) {
tr[x] += c;
}
}
int check(int x) {
int ans = 0;
for (; x; x -= lowbit(x)) {
ans += tr[x];
};
return ans;
}
} TR;
int main() {
scanf("%d%d", &n, &m);
for (; m; m--) {
scanf("%s", opt);
if (*opt == 'A') {
scanf("%d", &tmp);
printf("%d\n", TR.check(tmp));
} else if (*opt == 'B') {
scanf("%d%d", &tmp, &p);
TR.modify(tmp, p);
} else {
scanf("%d%d", &tmp, &p);
TR.modify(tmp, -p);
}
}
return 0;
}
ac记录(LOJ):https://loj.ac/submission/850709