题目链接:~( ̄▽ ̄~)(~ ̄▽ ̄)~
哎~ 不会树状数组郁闷........╮(╯Д╰)╭
code:
#include <stdio.h> typedef struct { int l, r, count; }node; node tree[4*50005]; void Init(int root, int left, int right) { int m = (left+right)/2; if(left == right)//叶节点为单个的数 { tree[root].l = tree[root].r = left; scanf("%d",&tree[root].count); return ; } tree[root].l = left; tree[root].r = right; Init(2*root, left, m); Init(2*root+1, m+1, right); tree[root].count = tree[2*root].count+tree[2*root+1].count; } void modify(int root, int a, int b)//a为第几个营地, b为要修改的数 { int m = (tree[root].l+tree[root].r)/2; if(tree[root].l == tree[root].r && tree[root].l == a)//找到叶节点 { tree[root].count += b; return ; } if(a>m) modify(2*root+1, a, b); else modify(2*root, a, b); tree[root].count = tree[2*root].count+tree[2*root+1].count; } int search(int root, int left, int right) { int l = tree[root].l ,r = tree[root].r, m = (tree[root].l+tree[root].r)/2, sum = 0; if(left<=l && right>=r)//当前区间属于查询区间 return sum += tree[root].count; if(left>m)//查询区间属于树的右区间 return sum = search(2*root+1, left, right); else if(right<=m)//查询区见属于树的左区间 return sum = search(2*root, left, right); else return sum = search(2*root+1, left, right)+search(2*root, left, right); } int main() { int t = 0, n = 0, count = 0, a = 0, b = 0; char ch[10]; scanf("%d",&t); while(t--) { scanf("%d",&n); Init(1, 1, n); printf("Case %d:\n", ++count); while(1) { scanf("%s",ch); if(ch[0] == 'E') break; if(ch[0] == 'A') { scanf("%d %d",&a,&b); modify(1,a,b); } else if(ch[0] == 'S') { scanf("%d %d",&a,&b); modify(1,a,-b); } else { scanf("%d %d",&a,&b); printf("%d\n",search(1,a,b)); } } } return 0; }