Total Submission(s): 42907 Accepted Submission(s): 18153
一个裸树状数组直接套模板
#include<iostream> #include<cstdio> #include<cstring> #define M 100005 using namespace std; int a[M],n; int lowbit(int i) { return i&(-i); } void add(int i,int x) { while(i<=n) { a[i]+=x; i+=lowbit(i); } } int sum(int i) { int ans=0; while(i>0) { ans+=a[i]; i-=lowbit(i); } return ans; } int main() { int i,cas,t; scanf("%d",&cas); t=1; while(cas--) { memset(a,0,sizeof a); scanf("%d",&n); int b; for(i=1;i<=n;i++) { scanf("%d",&b); add(i,b); } printf("Case %d:\n",t); t++; while(1) { int s,e; char str[10]; scanf("%s",str); if(str[0]=='E') break; scanf("%d%d",&s,&e); if(str[0]=='Q') { printf("%d\n",sum(e)-sum(s-1)); } else if(str[0]=='A') { add(s,e); } else if(str[0]=='S') { add(s,-e); } } } return 0; }
原文地址:http://blog.csdn.net/fanerxiaoqinnian/article/details/38389661