#include<stdio.h>
#include<string.h>
const int MAXN= 50001;
int dat[MAXN];
int n;
inline int Lowbit(int x) {
return x & (-x);
}
inline void Update(int pos, int val) {
while (pos <= n) {
dat[pos] += val;
pos += Lowbit(pos);
}
}
inline int Query(int x) {
int sum = 0;
while (x > 0) {
sum += dat[x];
x -= Lowbit(x);
}
return sum;
}
int main() {
// freopen("in.txt","r",stdin);
int cas, cases = 1,t;
scanf("%d", &cas);
while(cas--) {
memset(dat, 0, sizeof(dat));
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &t);
Update(i, t);
}
char str[15];
printf("Case %d:\n", cases++);
while (scanf("%s", str), strcmp(str, "End")) {
int a, b;
scanf("%d%d", &a, &b);
switch(str[0]) {
case 'Q' :
printf("%d\n", Query(b) - Query(a - 1));
break;
case 'A' :
Update(a, b);
break;
case 'S' :
Update(a, -b);
break;
}
}//while
}//while(cas)
return 0;
}
HDU 1166树状数组
最新推荐文章于 2021-05-30 16:17:32 发布