《树状数组》hdu acm 5.3.2 一维

来源:互联网 发布:淘宝买家秀 珍珠内裤 编辑:程序博客网 时间:2024/06/17 00:43
#include<stdio.h>#include<string.h>int map[50005];int n;int sum(int x){int ret = 0,i;for (i = x;i > 0;i -= (i&-i)){ret += map[i];}return ret;}void add(int x, int y){int i;for (i = x;i <=n;i += (i&-i)){map[i] += y;}}int main(){int t, num,a,b,c,i,k;char str[25];k = 0;scanf("%d", &t);while (t--){printf("Case %d:\n", ++k);memset(map, 0, sizeof(map));scanf("%d", &n);for (i = 1;i <= n;i++){scanf("%d", &num);add(i, num);}while (scanf("%s", str) && strcmp(str, "End")!=0){scanf("%d%d", &a, &b);if (strcmp(str, "Query")==0){printf("%d\n", sum(b) - sum(a-1));}else if (strcmp(str, "Add")==0){add(a, b);}else{add(a,-b);}}}return 0;}
0 0