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
Hint
The sums may exceed the range of 32-bit integers.
ps:怕了怕了,数组开大点。空格%c注意,用long long 吧。呜呜呜
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
const int N=500005;
ll a[N];
ll sum[N],lazy[N];
void build(ll l,ll r,ll rt){
if(l==r){
sum[rt]=a[l];
return;
}
ll mid=(l+r)/2;
build(l,mid,rt*2);
build(mid+1,r,rt*2+1);
sum[rt]=sum[rt*2]+sum[rt*2+1];
}
void pushdown(ll l,ll r,ll rt){
if(lazy[rt]){
lazy[rt*2]+=lazy[rt];
lazy[rt*2+1]+=lazy[rt];
sum[rt*2]+=lazy[rt]*l;//这里以后还是这样写吧。。。
sum[rt*2+1]+=lazy[rt]*r;
lazy[rt]=0;
}
}
void update(ll l1,ll r1,ll v,ll l,ll r,ll rt){
if(l1<=l&&r1>=r){
sum[rt]+=v*(r-l+1);
lazy[rt]+=v;
return;
}
ll mid=(l+r)/2;
pushdown(mid-l+1,r-mid,rt);
if(l1<=mid){
update(l1,r1,v,l,mid,rt*2);
}
if(r1>mid){
update(l1,r1,v,mid+1,r,rt*2+1);
}
sum[rt]=sum[rt*2]+sum[rt*2+1];
}
ll query(ll l1,ll r1,ll l,ll r,ll rt){
if(l1<=l&&r1>=r){
return sum[rt];
}
ll ret=0;
ll mid=(l+r)/2;
pushdown(mid-l+1,r-mid,rt);
if(l1<=mid){
ret+=query(l1,r1,l,mid,rt*2);
}
if(r1>mid){
ret+=query(l1,r1,mid+1,r,rt*2+1);
}
return ret;
}
int main()
{
ll n,q;
scanf("%lld%lld",&n,&q);
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
}
build(1,n,1);
while(q--){
char s;
ll a,b,c;
scanf(" %c",&s);
if(s=='Q'){
scanf("%lld %lld",&a,&b);
ll ans=query(a,b,1,n,1);
printf("%lld
",ans);
}
else{
scanf("%lld %lld %lld",&a,&b,&c);
update(a,b,c,1,n,1);
}
}
return 0;
}