http://acm.hdu.edu.cn/showproblem.php?pid=1166
相比与线段树,树状数组的编程难度确实很低,不容易出错,而且同等问题上效率较高些。
但是好像树状数组能解决的问题线段树都可以解决,而线段树能解决的它却不一定能解决。
code:
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <iomanip>
#include <bitset>
#include <list>
#include <ctime>
using namespace std ;
#define SET(arr, what) memset(arr, what, sizeof(arr))
#define FF(i, a) for(i=0; i<a; i++)
#define SD(a) scanf("%d", &a)
#define SSD(a, b) scanf("%d%d", &a, &b)
#define SF(a) scanf("%lf", &a)
#define SS(a) scanf("%s", a)
#define SLD(a) scanf("%lld", &a)
#define PF(a) printf("%d\n", a)
#define PPF(a, b) printf("%d %d\n", a, b)
#define SZ(arr) (int)a.size()
#define SWAP(a,b) a=a xor b;b= a xor b;a=a xor b;
#define read freopen("in.txt", "r", stdin)
#define write freopen("out.txt", "w", stdout)
#define MAX 1<<30
#define ESP 1e-5
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
template<class T> inline T sqr(T a){return a*a;}
template<class T> inline void AMin(T &a,T b){if(a==-1||a>b)a=b;}
template<class T> inline void AMax(T &a,T b){if(a<b)a=b;}
template<class T> inline T Min(T a,T b){return a>b?b:a;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
const int MAXN = 50010 ;
int n, c[MAXN] ;
int lowbit(int x){
return x & (-x) ;
}
void update(int pos, int val){
while(pos<=n){
c[pos] += val ;
pos += lowbit(pos) ;
}
}
int query(int pos){
int s = 0 ;
while(pos>0){
s += c[pos] ;
pos -= lowbit(pos) ;
}
return s ;
}
int main(){
char str[10] ;
int t, i, j, T, x, v ;
SD(t) ;
FF(T, t){
printf("Case %d:\n", T+1) ;
SET(c, 0) ;
SD(n) ;
for(i=1; i<=n; i++){
SD(x) ;
update(i, x) ;
}
while(1){
SS(str) ;
if(str[0]=='E') break ;
SSD(x, v) ;
if(str[0]=='A')
update(x, v) ;
else if(str[0]=='S')
update(x, -v) ;
else if(str[0]=='Q')
PF(query(v)-query(x-1)) ;
}
}
return 0 ;
}
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <iomanip>
#include <bitset>
#include <list>
#include <ctime>
using namespace std ;
#define SET(arr, what) memset(arr, what, sizeof(arr))
#define FF(i, a) for(i=0; i<a; i++)
#define SD(a) scanf("%d", &a)
#define SSD(a, b) scanf("%d%d", &a, &b)
#define SF(a) scanf("%lf", &a)
#define SS(a) scanf("%s", a)
#define SLD(a) scanf("%lld", &a)
#define PF(a) printf("%d\n", a)
#define PPF(a, b) printf("%d %d\n", a, b)
#define SZ(arr) (int)a.size()
#define SWAP(a,b) a=a xor b;b= a xor b;a=a xor b;
#define read freopen("in.txt", "r", stdin)
#define write freopen("out.txt", "w", stdout)
#define MAX 1<<30
#define ESP 1e-5
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
template<class T> inline T sqr(T a){return a*a;}
template<class T> inline void AMin(T &a,T b){if(a==-1||a>b)a=b;}
template<class T> inline void AMax(T &a,T b){if(a<b)a=b;}
template<class T> inline T Min(T a,T b){return a>b?b:a;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
const int MAXN = 50010 ;
int n, c[MAXN] ;
int lowbit(int x){
return x & (-x) ;
}
void update(int pos, int val){
while(pos<=n){
c[pos] += val ;
pos += lowbit(pos) ;
}
}
int query(int pos){
int s = 0 ;
while(pos>0){
s += c[pos] ;
pos -= lowbit(pos) ;
}
return s ;
}
int main(){
char str[10] ;
int t, i, j, T, x, v ;
SD(t) ;
FF(T, t){
printf("Case %d:\n", T+1) ;
SET(c, 0) ;
SD(n) ;
for(i=1; i<=n; i++){
SD(x) ;
update(i, x) ;
}
while(1){
SS(str) ;
if(str[0]=='E') break ;
SSD(x, v) ;
if(str[0]=='A')
update(x, v) ;
else if(str[0]=='S')
update(x, -v) ;
else if(str[0]=='Q')
PF(query(v)-query(x-1)) ;
}
}
return 0 ;
}