裸的普通单点更新线段树,基本按照http://www.notonlysuccess.com/index.php/segment-tree-complete/1:1拍的。。。
1 #include <cstdio> 2 #include <cstring> 3 using namespace std; 4 5 #define MAXN 65536 6 int _v[MAXN << 1]; 7 int *const v = &_v[-1]; 8 9 #define lsp p<<1 10 #define rsp lsp | 1 11 #define pushup(p) (v[p] = v[lsp] + v[rsp]) 12 #define lso l, m, lsp 13 #define rso m+1, r, rsp 14 15 void build(int l, int r, int p) 16 { 17 if (l == r) scanf("%d", &v[p]); 18 else { 19 int m = l+r >> 1; 20 build(lso), build(rso); 21 pushup(p); 22 23 } 24 } 25 26 void update(int o, int val, int l, int r, int p) 27 { 28 if (l == r) v[p] += val; 29 else { 30 int m = l+r >> 1; 31 if (o <= m) update(o, val, lso); 32 else update(o, val, rso); 33 pushup(p); 34 35 } 36 } 37 38 int query(int L, int R, int l, int r, int p) 39 { 40 if (L<=l && r<=R) return v[p]; 41 else { 42 int m = l+r >> 1; 43 return (L <= m ? query(L,R, lso) : 0) + (R > m ? query(L, R, rso) : 0); 44 } 45 } 46 47 48 49 int main(void) 50 { 51 // freopen("hdu1166.txt", "r", stdin); 52 char s[6]; 53 int T, N; 54 scanf("%d", &T); 55 for(int t = 1; t<=T; ++t) { 56 scanf("%d", &N); 57 build(1, N, 1); 58 printf("Case %d: ", t); 59 while(scanf("%s", s), s[0] != 'E') { 60 if (s[0] == 'Q'){ 61 int L, R; 62 scanf("%d%d", &L, &R); 63 printf("%d ", query(L, R, 1, N, 1)); 64 } else if (s[0] == 'A') { 65 int O, Oval; 66 scanf("%d%d", &O, &Oval); 67 update(O, Oval, 1, N, 1); 68 69 } else { 70 int O, Oval; 71 scanf("%d%d", &O, &Oval); 72 update(O, -Oval, 1, N, 1); 73 } 74 } 75 } 76 return 0; 77 }
2014-06-03 20:46:26 | Accepted | 1166 | 328MS | 716K | 1400 B | G++ |