标签:logs string nbsp ted tar ssi time ace sub
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 89077 Accepted Submission(s): 37522
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> using namespace std; const int MAXN=0x7f7f7f7f; const int N=1e6+5; int dat[N]; int n; void init(int n_) { memset(dat,0,sizeof(dat)); n=1; while(n<n_)n<<=1; } void add(int k,int a) { k+=n-1; dat[k]+=a; while(k>0) { k=(k-1)/2; dat[k]=dat[(k<<1)+1]+dat[(k<<1)+2]; } } void sub(int k,int a) { k+=n-1; dat[k]-=a; while(k>0) { k=(k-1)/2; dat[k]=dat[(k<<1)+1]+dat[(k<<1)+2]; } } int query(int a,int b,int k,int l,int r) { if(a>r||b<l)return 0; if(a<=l&&r<=b)return dat[k]; else { int vl=query(a,b,(k<<1)+1,l,(l+r)/2); int vr=query(a,b,(k<<1)+2,(l+r)/2+1,r); return vl+vr; } } int main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin>>t; int k=0; while(t--) { k++; int nn; scanf("%d",&nn); init(nn); //cout<<n<<endl; for(int i=0;i<nn;i++)scanf("%d",&dat[i+n-1]); for(int i=n-2;i>=0;i--)dat[i]=dat[(i<<1)+1]+dat[(i<<1)+2]; cout<<"Case "<<k<<":"<<endl; char s[10]; int a,b; while(~scanf("%s",s)) { if(s[0]==‘Q‘) { scanf("%d%d",&a,&b); cout<<query(a-1,b-1,0,0,n-1)<<endl; } else if(s[0]==‘A‘) { scanf("%d%d",&a,&b); add(a-1,b); } else if(s[0]==‘S‘) { scanf("%d%d",&a,&b); sub(a-1,b); } else break; } } return 0; }
标签:logs string nbsp ted tar ssi time ace sub
原文地址:http://www.cnblogs.com/widsom/p/6906160.html