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.
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.
You need to answer all Q commands in order. One answer in a line.
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 4Sample Output
4 55 9 15Hint
#include<algorithm>
#include<string.h>
#include<stdio.h>
#define M 100010
using namespace std;
long long a[M],T[M],Ti[M];
int n,m;
long long lowbit(long long b)
{
return b&-b;
}
void add(long long *arry,long long pos,long long x)
{
while(pos<=n)
{
arry[pos]+=x;
pos+=lowbit(pos);
}
}
long long query(long long *arry,long long pos)
{
long long ans=0;
while(pos>0)
{
ans+=arry[pos];
pos-=lowbit(pos);
}
return ans;
}
int main()
{
long long l,r,x,sumr,suml;
char str[10];
scanf("%d%d",&n,&m);
for(long long i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
a[i]+=a[i-1];
}
while(m--)
{
scanf("%s",str);
if(str[0]=='C')
{
scanf("%lld%lld%lld",&l,&r,&x);
add(T,l,x);
add(T,r+1,-x);
add(Ti,l,l*x);
add(Ti,r+1,-x*(r+1));
}
else
{
scanf("%lld%lld",&l,&r);
suml=a[l-1]+l*query(T,l)-query(Ti,l);
sumr=a[r]+(r+1)*query(T,r)-query(Ti,r);
printf("%lld\n",sumr-suml);
}
}
}