题目链接:hdu 1166 敌兵布阵
标准的树状数组模板题,需要注意的是树状数组的初始节点的编号为1。
1 #define MAX_N 1000007 2 #include <cstdlib> 3 #include <cstdio> 4 #include <iostream> 5 #include <cstring> 6 using namespace std; 7 int bit[MAX_N+1], n; 8 int lowbit(int x) 9 { 10 return x&(-x); 11 } 12 int sum(int i) 13 { 14 int s = 0; 15 while( i > 0 ) 16 { 17 s += bit[i]; 18 i -= lowbit(i); 19 } 20 return s; 21 } 22 void add(int i , int x) 23 { 24 while( i <= n ) 25 { 26 bit[i] += x; 27 i += lowbit(i); 28 } 29 } 30 int main(int argc, char *argv[]) 31 { 32 int T; 33 int c = 1; 34 scanf ( "%d", &T ); 35 while( T-- ) 36 { 37 scanf("%d", &n); 38 memset(bit, 0, sizeof(bit)); 39 for( int i = 1 ; i <= n ; i++ ) 40 { 41 int tmp; 42 scanf ( "%d", &tmp ); 43 add(i, tmp); 44 } 45 char s[10]; 46 int a, b; 47 printf("Case %d: ", c++); 48 while(1) 49 { 50 scanf("%s", s); 51 if( !strcmp(s , "End" )) 52 { 53 break; 54 } 55 else 56 { 57 scanf("%d%d", &a, &b); 58 if( !strcmp(s, "Query") ) 59 { 60 printf("%d ", sum(b) - sum(a-1)); 61 } 62 else if(!strcmp( s , "Add") ) 63 { 64 add(a, b); 65 } 66 else if(!strcmp(s, "Sub")) 67 { 68 add(a, -b); 69 } 70 } 71 } 72 } 73 }
同样也可以用线段树来解决:
1 #include <iostream> 2 #include <string> 3 #include <cstring> 4 #include <cstdlib> 5 #include <climits> 6 7 using namespace std; 8 9 struct Node 10 { 11 int lt, rt, sum; 12 }; 13 14 const int MAXN = 50005; 15 Node tree[MAXN<<2]; 16 17 void build(int l, int r, int v) 18 { 19 tree[v].lt = l; 20 tree[v].rt = r; 21 if( l == r ) 22 { 23 scanf("%d", &tree[v].sum); 24 return; 25 } 26 int mid = (l + r)>>1; 27 build(l, mid, v<<1); 28 build(mid+1, r, v<<1|1); 29 tree[v].sum = tree[v<<1].sum + tree[v<<1|1].sum; 30 } 31 32 void update(int p, int v, int val) 33 { 34 if( tree[v].lt == tree[v].rt ) 35 { 36 tree[v].sum += val; 37 return; 38 } 39 if( p <= tree[v<<1].rt ) update(p, v<<1, val); 40 if( p >= tree[v<<1|1].lt ) update(p, v<<1|1, val); 41 tree[v].sum = tree[v<<1].sum + tree[v<<1|1].sum; 42 } 43 44 int query(int l, int r, int v) 45 { 46 if( l <= tree[v].lt && r >= tree[v].rt) 47 return tree[v].sum; 48 int a = 0, b = 0; 49 if( l <= tree[v<<1].rt ) a = query(l, r, v<<1); 50 if( r >= tree[v<<1|1].lt ) b = query(l, r, v<<1|1); 51 return a+b; 52 } 53 54 int main(int argc, char *argv[]) 55 { 56 int T; 57 scanf("%d", &T); 58 int c = 0; 59 for (c = 1; c <= T; c++) 60 { 61 int N; 62 scanf("%d", &N); 63 build(1, N, 1); 64 char str[10]; 65 int a, b; 66 printf("Case %d: ", c); 67 while(1) 68 { 69 scanf("%s", str); 70 if( str[0] == 'Q') 71 { 72 scanf("%d%d", &a, &b); 73 printf("%d ", query(a, b, 1)); 74 } 75 else if( str[0] == 'A' ) 76 { 77 scanf("%d%d", &a, &b); 78 update(a,1, b); 79 } 80 else if( str[0] == 'S' ) 81 { 82 scanf("%d%d", &a, &b); 83 update(a, 1, -b); 84 } 85 else 86 { 87 break; 88 } 89 } 90 } 91 }