hdu 1166 敌兵布阵 树状数组 模板题
这题是树状数组入门的一模板题,非常基础,被小白成为"赤裸裸"的入门题,哈哈,一个plus,一个sum全部搞定
#include<stdio.h> #include<string.h> #define lowbit( x ) ( x )&( -x ) int tree[50024],num,n; void plus( int num,int x ) { while( x <= n ) { tree[x] += num; x += lowbit( x ); } } int sum( int x ) { int sum = 0; while( x ) { sum += tree[x]; x -= lowbit( x ); } return sum; } int main( ) { int t; char str[100]; scanf( "%d",&t ); int k = t; while( k-- ) { memset( tree,0,sizeof( tree ) ); scanf( "%d",&n ); for( int i = 1; i <= n; ++i ) { scanf( "%d",&num ); plus( num,i ); //第i个增加了NUM人 } int r = 1; while( scanf( "%s",str ),str[0] != 'E' ) { int a,b; scanf( "%d%d",&a,&b ); switch( str[0] ) { case 'A': plus( b,a ); break; case 'S': plus( -b,a ); break; case 'Q': if( r ) printf( "Case %d:\n",t - k ); r = 0; printf("%d\n",sum( b ) - sum( a - 1 )); break; } } } return 0; }
本人还是新手 ,转载请注明来自Lvsi‘s home