标签:des style blog color java os io strong
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> #include<cmath> #include<algorithm> using namespace std; int n,c[50010];//fff[40010]; int lll(int x) { return x&(-x); } void ad(int x,int y) { while(x<=n) { c[x]+=y; x+=lll(x); } } int he(int x) { int sum=0; while(x>0) { sum+=c[x]; x-=lll(x); } return sum; } int main() { int t,k=1; scanf("%d",&t); while(t--) { scanf("%d",&n); int a,b,i; memset(c,0,sizeof c); //memset(fff,0,sizeof fff); for(i=1;i<=n;i++) { scanf("%d",&a); ad(i,a); } /*for(i=1;i<=n;i++) { printf("%d\n",he(i)); }*/ getchar(); char g[10]; //int zzz=0; printf("Case %d:\n",k++); while(scanf("%s",g)!=EOF) { if(g[0]=='Q') { scanf("%d %d",&a,&b); printf("%d\n",he(b)-he(a-1)); //fff[zzz]=he(b)-he(a-1); //zzz++; }else if(g[0]=='A') { scanf("%d %d",&a,&b); ad(a,b); }else if(g[0]=='S') { scanf("%d %d",&a,&b); ad(a,-b); }else{ break; } } /*for(i=0;i<zzz;i++) { printf("%d\n",fff[i]); }*/ } return 0; }
#include<iostream> #include<cstring> #include<cstdio> using namespace std; const int MX=50011; int n; int tree[MX*4]; void up(int index) { tree[index]=tree[index<<1]+tree[index<<1|1]; } void create(int index,int l,int r) { if(l==r) { scanf("%d",&tree[index]);return; } int mind=(l+r)/2; create(index<<1,l,mind); create(index<<1|1,mind+1,r); up(index); } int getse(int index,int s,int e,int l,int r) { if(s<=l&&r<=e) return tree[index]; int ans=0; int mind=(r+l)/2; if(s<=mind) ans+=getse(index<<1,s,e,l,mind); if(e>mind) ans+=getse(index<<1|1,s,e,mind+1,r); return ans; } void update(int index,int p,int v,int l,int r) { if(l==r){ tree[index]+=v;return; } int mind=(r+l)/2; if(p<=mind) update(index<<1,p,v,l,mind); else update(index<<1|1,p,v,mind+1,r); up(index); } int main() { int t,tt=1; scanf("%d",&t); while(t--) { scanf("%d",&n); memset(tree,0,sizeof tree); create(1,1,n); char g[10]; int a,b; printf("Case %d:\n",tt++); while(scanf("%s",g)) { if(g[0]=='E')break; scanf("%d%d",&a,&b); if(g[0]=='Q'){ printf("%d\n",getse(1,a,b,1,n)); }else if(g[0]=='A'){ update(1,a,b,1,n); }else{ update(1,a,-b,1,n); } } } return 0; }
标签:des style blog color java os io strong
原文地址:http://blog.csdn.net/fljssj/article/details/38460477