题目大意:中文
代码:
/*
* Problem ID : HDU 1166 敌兵布阵
* Author : Lirx.t.Una
* Language : C
* Run Time : 218 ms
* Run Memory : 792 KB
*/
#include <memory.h>
#include <stdio.h>
#define MAXN 50000
#define MAXCMDLEN 6
#define LFT(T) ( (T) << 1 )
#define RHT(T) ( LFT(T) | 1 )
int seg[MAXN * 3];
char cmd[MAXCMDLEN];
void
update( int tree, int i, int v, int lft, int rht ) {
int mid;
seg[tree] += v;
if ( lft != rht ) {
mid = ( lft + rht ) >> 1;
if ( i <= mid ) update( LFT(tree), i, v, lft, mid );
else update( RHT(tree), i, v, mid + 1, rht );
}
}
int
query( int tree, int ql, int qr, int lft, int rht ) {
int mid;
if ( lft == ql && qr == rht ) return seg[tree];
mid = ( lft + rht ) >> 1;
if ( qr <= mid ) return query( LFT(tree), ql, qr, lft, mid );
else if ( ql > mid ) return query( RHT(tree), ql, qr, mid + 1, rht );
else return query( LFT(tree), ql, mid, lft, mid ) +
query( RHT(tree), mid + 1, qr, mid + 1, rht );
}
int
main() {
int t, iscn;
int n;
int a;
int i, j;
scanf("%d", &t);
iscn = 0;
while ( t-- ) {
memset(seg, 0, sizeof(seg));
scanf("%d", &n);
for ( i = 1; i <= n; i++ ) {
scanf("%d", &a);
update( 1, i, a, 1, n );
}
printf("Case %d:\n", ++iscn);
while ( scanf("%s", cmd), *cmd != 'E' ) {
scanf("%d%d", &i, &j);
switch ( *cmd ) {
case 'Q' :
printf("%d\n", query( 1, i, j, 1, n ));
break;
case 'A' :
update( 1, i, j, 1, n );
break;
case 'S' :
update( 1, i, -j, 1, n );
break;
default : break;
}
}
}
return 0;
}