标签:
http://acm.hdu.edu.cn/showproblem.php?pid=1166
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 57332 Accepted Submission(s): 24215
#include<stdio.h> #include<math.h> #include<string.h> #include<algorithm> #define N 50010 #define Lson root<<1, L, tree[root].Mid()//root<<1表示root*2 #define Rson root<<1|1, tree[root].Mid() + 1, R//root<<1|1表示root*2+1 using namespace std; struct Tree { int L, R; int e, sum; int Mid() { return (L + R) / 2; } }tree[N * 4]; int al[N]; void Build(int root, int L, int R) { tree[root].L = L, tree[root].R = R; if(L == R) { tree[root].sum = al[L]; return ; } Build(Lson); Build(Rson); tree[root].sum = tree[root<<1].sum + tree[root<<1|1].sum;//上一个节点区间的和等于左右区间的和 } void Insert(int root, int k, int e)//k为要更新的 { tree[root].sum += e; if(tree[root].L == tree[root].R) return ; if(k <= tree[root].Mid()) Insert(root<<1, k, e); else Insert(root<<1|1, k, e); } int Query(int root, int L, int R) { if(tree[root].L == L && tree[root].R == R) return tree[root].sum; if(R <= tree[root].Mid()) return Query(root<<1, L, R); else if(L > tree[root].Mid()) return Query(root<<1|1, L, R); else return Query(Lson) + Query(Rson); } int main() { int t, a, b, n, i, x = 0; char s[10]; scanf("%d", &t); while(t--) { x++; scanf("%d", &n); for(i = 1 ; i <= n ; i++) scanf("%d", &al[i]); Build(1, 1, n); printf("Case %d:\n", x); while(scanf("%s", s), s[0] != ‘E‘) { scanf("%d%d", &a, &b); if(s[0] == ‘Q‘) printf("%d\n", Query(1, a, b)); else if(s[0] == ‘A‘) Insert(1, a, b); else Insert(1, a, -b); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/qq2424260747/p/4690632.html