标签:
1 10 1 2 3 4 5 6 7 8 9 10 Query 1 3 Add 3 6 Query 2 7 Sub 10 2 Add 6 3 Query 3 10 End
Case 1: 6 33 59
#include <iostream> #include <cstring> #include <cstdio> using namespace std; const int maxn=50000+5; int a[maxn]; int lowbit(int x) { return x&(-x); } int sum(int x)//求和,a[0]+a[1]+……+a[x] { int ans=0; while(x>0) { ans+=a[x]; x-=lowbit(x); } return ans; } void add(int x,int y)//增加,a[x]+y { while(x<=maxn) { a[x]+=y; x+=lowbit(x); } } int main() { int T,kase=0; cin>>T; while(T--) { int n; memset(a,0,sizeof(a)); cin>>n; for(int i=1;i<=n;i++) { int v; cin>>v; add(i,v); } char str[10]; int x,y; cout<<"Case "<<++kase<<":"<<endl; while(scanf("%s",str)!=EOF) { if(str[0]=='E')// break; else if(str[0]=='Q')//查询 { scanf("%d%d",&x,&y); cout<<sum(y)-sum(x-1)<<endl; } else if(str[0]=='A')//增加,a[x]+y { scanf("%d%d",&x,&y); add(x,y); } else if(str[0]=='S')//减少,a[x]-y { scanf("%d%d",&x,&y); add(x,-y); } } } return 0; }
标签:
原文地址:http://blog.csdn.net/hurmishine/article/details/51945831