发现洛谷里没人发题解,可能是这样的全裸树状数组没人看得上吧。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 int c[200005],n,a[200005]; 6 int lowbit(int x) 7 { 8 return x&(-x); 9 } 10 int sum(int x) //区间求和 11 { 12 int ret=0; 13 while(x>0) 14 { 15 ret+=c[x];x-=lowbit(x); 16 } 17 return ret; 18 } 19 void add(int x,int d) //单点修改 20 { 21 while(x<=n) 22 { 23 c[x]+=d;x+=lowbit(x); 24 } 25 if(x<1) return; 26 } 27 int main() 28 { 29 int m,p,q,kase=0; 30 char s[5]; 31 while(scanf("%d",&n)==1&&n) 32 { 33 if(kase) printf("\n"); 34 memset(c,0,sizeof(c)); 35 memset(a,0,sizeof(a)); 36 for(int i=1;i<=n;++i) {scanf("%d",&a[i]);add(i,a[i]);} 37 printf("Case %d:\n",++kase); 38 while(scanf("%s",s)) 39 { 40 if(s[0]=='M') 41 { 42 scanf("%d%d",&p,&q); 43 printf("%d\n",sum(q)-sum(p-1)); 44 } 45 else if(s[0]=='S') 46 { 47 scanf("%d%d",&p,&q); 48 add(p,q-a[p]); 49 a[p]=q; 50 } 51 else break; 52 } 53 } 54 }