下面是代码
#include <stdio.h>
int lowbit(int x) {
return x & (-x);
}
int tree[1000001];
int n, m;
void Add(int pos, int x) {
for(int i = pos; i <= n; i += lowbit(i))
tree[i] += x;
}
int Ask(int pos) {
int ans = 0;
for(int i = pos; i; i -= lowbit(i))
ans += tree[i];
return ans;
}
int main() {
scanf("%d %d", &n, &m);
int last, now;
for(int i = 1; i <= n; ++i) {
scanf("%d", &now);
Add(i, now - last);
last = now;
}
for(int i = 1; i <= m; ++i) {
int opt, x, y, k;
scanf("%d %d", &opt, &x);
if(opt == 1) {
scanf("%d %d", &y, &k);
Add(x, k), Add(y + 1, -k);
}
else
printf("%d\n", Ask(x));
}
return 0;
}
编译器版本是这个
gcc version 13.2.0 (x86_64-win32-seh-rev1, Built by MinGW-Builds project)