hdu 1166

来源:互联网 发布:养老院软件 编辑:程序博客网 时间:2024/06/06 07:47

#include <stdio.h>#include <string.h>#define maxn 500005int p[maxn], c[maxn], n;char ask[15];int lowbit(int x){return x & (-x);}void update(int x, int num){while(x <= n){c[x] += num;x += lowbit(x);}}int getSum(int x){int s = 0;while(x > 0){s += c[x];x -= lowbit(x);}return s;}int main(){int t, i, k, x, y;scanf("%d", &t);k = 1;while(t--){printf("Case %d:\n", k++);scanf("%d", &n);memset(c, 0, sizeof(c));for(i = 1;i <= n;i++){scanf("%d", &p[i]);update(i, p[i]);}while(1){scanf("%s", ask);if(!strcmp(ask, "Add")){scanf("%d %d", &x, &y);update(x, y);}else if(!strcmp(ask, "Sub")){scanf("%d %d", &x, &y);update(x, -y);}else if(!strcmp(ask, "Query")){scanf("%d %d", &x, &y);printf("%d\n", getSum(y) - getSum(x - 1));}else if(!strcmp(ask, "End")) break;}}return 0;} 

第一次自己写树状数组


0 0
原创粉丝点击