hdoj 1166
题目大意:
解决:树状数组简单题,不解释
#include <iostream> #include <cstdio> #include <cstring> using namespace std; #define L(x) (x<<1) #define R(x) (x<<1|1) #define M(x,y) ((x+y)>>1) int n; int c[50005]; int lowbit(int x) { return x&(-x); } void updata(int p,int inc) { for(int i=p;i<=n;i+=lowbit(i)) c[i]+=inc; } int getsum(int p) { int sum=0; for(int i=p;i>=1;i-=lowbit(i)) sum+=c[i]; return sum; } int main() { int T,num,a,b; scanf("%d",&T); for(int t=1;t<=T;t++) { memset(c,0,sizeof(c)); scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&num); updata(i,num); } char cmd[10]; printf("Case %d:\n",t); while(scanf("%s",cmd),cmd[0]!='E') { if(cmd[0]=='A'){scanf("%d%d",&a,&b); updata(a,b);} else if(cmd[0]=='S'){scanf("%d%d",&a,&b); updata(a,-b);} else { scanf("%d%d",&a,&b); printf("%d\n",getsum(b)-getsum(a-1)); } } } system("pause"); return 0; }