input:simple.in output:simple.out
Time limit:
1000 MS space constraints: 262144 KB specific restrictions
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long n,q,d,s,c1[100001],sum1[100001],c2[100001],a[100001]; 4 char w; 5 long long lowbit(long long x) 6 { 7 return x&(-x); 8 } 9 long long sum(long long c[],long long x) 10 { 11 long long ret=0; 12 while(x>0) 13 { 14 ret+=c[x]; 15 x-=lowbit(x); 16 } 17 return ret; 18 } 19 void add(long long c[],long long x,long long y) 20 { 21 while(x<=n) 22 { 23 c[x]+=y; 24 x+=lowbit(x); 25 } 26 } 27 int main() 28 { 29 freopen("simple.in","r",stdin); 30 freopen("simple.out","w",stdout); 31 cin>>n>>q; 32 for(int i=1;i<=n;i++) 33 { 34 cin>>a[i]; 35 sum1[i]=sum1[i-1]+a[i]; 36 } 37 for(int j=1;j<=q;j++) 38 { 39 cin>>w; 40 cin>>s>>d; 41 if(int(w)==int('Q')) 42 { 43 long long suml=sum1[s-1]+s*sum(c1,s-1)-sum(c2,s-1); 44 long long sumd=sum1[d]+(d+1)*sum(c1,d)-sum(c2,d); 45 cout<<sumd-suml<<endl; 46 } 47 else 48 { 49 long long cost; 50 cin>>cost; 51 add(c1,s,cost); 52 add(c1,d+1,-cost); 53 add(c2,s,s*cost); 54 add(c2,d+1,-(d+1)*cost); 55 } 56 } 57 return 0; 58 }