You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.
Output
You need to answer all Q commands in order. One answer in a line.
Sample Input
10 5 1 2 3 4 5 6 7 8 9 10 Q 4 4 Q 1 10 Q 2 4 C 3 6 3 Q 2 4
Sample Output
4 55 9 15
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 7 typedef long long ll; 8 int t,n,jishu,m; 9 const int N=100005; 10 ll cnt1[N],arr[N],cnt2[N]; 11 int lowbits(int x) { return x&-x; } 12 void add(int x,int value){ 13 ll temp=x; 14 while(x<=N){ 15 cnt1[x]+=value; 16 cnt2[x]+=value*(temp-1); 17 x+=lowbits(x); 18 } 19 } 20 ll query(int x){ 21 ll sum=0; 22 int temp=x; 23 while(x>0){ 24 sum+=temp*cnt1[x]-cnt2[x]; 25 x-=lowbits(x); 26 } 27 return sum; 28 } 29 30 int main(){ 31 32 scanf("%d%d",&n,&m); 33 for(int i=1,d;i<=n;i++) scanf("%lld",&arr[i]),add(i,arr[i]-arr[i-1]); 34 char ss[10]; 35 for(int i=1;i<=m;i++){ 36 int d1,d2,d3; 37 scanf("%s",ss); 38 if(ss[0]=='Q'){ 39 scanf("%d%d",&d1,&d2); 40 printf("%lld ",query(d2)-query(d1-1)); 41 } 42 else{ 43 scanf("%d%d%d",&d1,&d2,&d3); 44 add(d1,d3),add(d2+1,-d3); 45 } 46 } 47 return 0; 48 }