标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 52815 Accepted Submission(s): 22123
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <string> 5 #include <iomanip> 6 using namespace std; 7 #define lson l,m,rt<<1 8 #define rson m+1,r,rt<<1|1 9 #define maxn 55555 10 int sum[maxn<<2]; 11 //PushUP(int rt)是把当前结点的信息更新到父结点 12 //单点更新。 13 int T, N; 14 void PushUP(int rt){ 15 sum[rt] = sum[rt<<1] + sum[rt<<1|1]; 16 } 17 void build(int l, int r, int rt){ 18 if(l == r){ 19 scanf("%d", &sum[rt]); 20 return; 21 } 22 int m = (l+r) >> 1; 23 build(lson); 24 build(rson); 25 PushUP(rt); 26 } 27 void update(int p, int add, int l, int r, int rt){ 28 if(l == r){ 29 sum[rt] += add; 30 return; 31 } 32 int m = (l+r)>>1; 33 if(p <= m) update(p, add, lson); 34 else update(p, add, rson); 35 PushUP(rt); 36 } 37 int query(int L, int R, int l, int r, int rt){ 38 if(L <= l && r <= R){ 39 return sum[rt]; 40 } 41 int m = (l+r)>>1; 42 int ret = 0; 43 if(L <= m) ret += query(L, R, lson); 44 if(R > m) ret += query(L, R, rson); 45 return ret; 46 } 47 int main(){ 48 scanf("%d", &T); 49 int cnt = 0; 50 while(T--){ 51 cnt++; 52 scanf("%d", &N); 53 build(1,N,1); 54 string s; 55 printf("Case %d:\n", cnt); 56 while(cin>>s){ 57 if(s[0] == ‘E‘) break; 58 else if(s[0] == ‘Q‘ ){ 59 int L, R; 60 scanf("%d%d", &L, &R); 61 printf("%d\n", query(L, R, 1, N, 1)); 62 } 63 else if(s[0] == ‘A‘){ 64 int p, add; 65 scanf("%d%d", &p, &add); 66 update(p, add, 1, N, 1); 67 } 68 else if(s[0] == ‘S‘){ 69 int p, add; 70 scanf("%d%d", &p, &add); 71 update(p, -add, 1, N, 1); 72 } 73 } 74 } 75 76 return 0; 77 }
标签:
原文地址:http://www.cnblogs.com/titicia/p/4453749.html