1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std;
#define Lson(x) (x<<1) #define Rson(x) (Lson(x)|1) #define Mid(x,y) ((x+y)>>1) #define Sum(x,y) (x+y)
const int N = 10; const int MAXN = 50010;
int n; int num[MAXN]; struct Node{ int left; int right; int sum; }; Node node[4*MAXN];
void push_up(int pos){ node[pos].sum = Sum(node[Lson(pos)].sum,node[Rson(pos)].sum); }
void init(int left , int right , int pos){ node[pos].left = left; node[pos].right = right;
if(left == right){ node[pos].sum = num[left]; return; }
int x = Mid(left , right);
init(left , x , Lson(pos)); init(x+1 , right , Rson(pos)); push_up(pos); }
void update(int index , int val , int pos){ if(node[pos].left == node[pos].right){ node[pos].sum += val; return; }
int x = Mid(node[pos].left , node[pos].right);
if(index <= x) update(index , val , Lson(pos)); else update(index , val , Rson(pos)); push_up(pos); }
int query(int left , int right , int pos){ if(node[pos].left == left && node[pos].right == right) return node[pos].sum;
int x = Mid(node[pos].left , node[pos].right);
if(right <= x) return query(left , right , Lson(pos)); else if(left > x) return query(left , right , Rson(pos)); else return query(left , x , Lson(pos))+query(x+1 , right , Rson(pos)); }
void input(){ char str[N]; scanf("%d" , &n); for(int i = 1 ; i <= n ; i++) scanf("%d" , &num[i]); init(1 , n , 1); getchar(); int x , y; while(scanf("%s" , str) && str[0] != 'E'){ scanf("%d%d%*c" , &x , &y); if(str[0] == 'Q') printf("%d\n" , query(x , y , 1)); else if(str[0] == 'A') update(x , y , 1); else update(x , -y , 1); } }
int main(){ int Case; int cas = 1; scanf("%d" , &Case); while(Case--){ printf("Case %d:\n" , cas++); input(); } return 0; }
|