标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 63302 Accepted Submission(s): 26706
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> #include <time.h> #include <algorithm> using namespace std; const int N = 50005; int num[N << 2]; void Update(int i){ num[i] = num[i * 2] + num[i * 2 + 1]; //更新父亲结点 } void Build(int left, int right, int i){ //建树 if(left == right){ scanf("%d", &num[i]); return; } int mid = (left + right) / 2; Build(left, mid, i * 2); Build(mid + 1, right, i * 2 + 1); Update(i); } void Add(int loc, int number, int left, int right, int i){ //单点更新 if(left == right){ num[i] += number; return; } int mid = (left + right) / 2; if(loc <= mid) Add(loc, number, left, mid, i * 2); else Add(loc, number, mid + 1, right, i * 2 + 1); Update(i); } int Query(int ll, int rr, int left, int right, int i){ //线段查询 if(ll == left && rr == right) return num[i]; int mid = (left + right) / 2; if(rr <= mid) return Query(ll, rr, left, mid, i * 2); else if(ll > mid) return Query(ll, rr, mid + 1, right, i * 2 + 1); else return Query(ll, mid, left, mid, i * 2) + Query(mid + 1, rr, mid + 1, right, i * 2 + 1); } int main(){ int T, count = 1; scanf("%d", &T); while(T--){ int n; scanf("%d", &n); printf("Case %d:\n", count++); Build(1, n, 1); while(1){ char order[10]; scanf("%s", order); if(order[0] == ‘E‘) break; int a, b; scanf("%d%d", &a, &b); if(order[0] == ‘A‘) Add(a, b, 1, n, 1); else if(order[0] == ‘S‘) Add(a, -b, 1, n, 1); else if(order[0] == ‘Q‘) printf("%d\n", Query(a, b, 1, n, 1)); } }
return 0;
}
标签:
原文地址:http://www.cnblogs.com/Cyy666/p/4969169.html