Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 76020 | Accepted: 23419 | |
Case Time Limit: 2000MS |
Description
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
Source
#include <iostream>
#include <cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#include<algorithm>
#define LL long long
using namespace std;
const int maxn=100010;
LL Sum[maxn<<2];
LL Add[maxn<<2];
void PushUp(int rt)
{
Sum[rt]=Sum[rt<<1]+Sum[rt<<1|1];
}
void PushDown(int rt,int m)
{
if(Add[rt])
{
Add[rt<<1]+=Add[rt];
Add[rt<<1|1]+=Add[rt];
Sum[rt<<1]+=(m-(m>>1))*Add[rt];
Sum[rt<<1|1]+=(m>>1)*Add[rt];
Add[rt]=0;
}
}
void build(int l,int r,int rt)
{
Add[rt]=0;
if(l==r)
{
scanf("%lld",&Sum[rt]);
return;
}
int mid=(l+r)>>1;
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
PushUp(rt);
}
LL Query(int L,int R,int l,int r,int rt)
{
if(L<=l&&R>=r)
return Sum[rt];
PushDown(rt,r-l+1);
int mid=(l+r)>>1;
LL ans=0;
if(L<=mid)
ans+=Query(L,R,l,mid,rt<<1);
if(R>mid)
ans+=Query(L,R,mid+1,r,rt<<1|1);
return ans;
}
void Updata(int L,int R,int c,int l,int r,int rt)
{
if(L<=l&&R>=r)
{
Add[rt]+=c;
Sum[rt]+=c*(r-l+1);
return;
}
PushDown(rt,r-l+1);
int mid=(l+r)>>1;
if(L<=mid)
Updata(L,R,c,l,mid,rt<<1);
if(R>mid)
Updata(L,R,c,mid+1,r,rt<<1|1);
PushUp(rt);
}
int main()
{
char s[3];
int a,b,c;
int n,m;
while(~scanf("%d%d",&n,&m))
{
build(1,n,1);
while(m--)
{
scanf("%s",s);
if(s[0]=='Q')
{
scanf("%d%d",&a,&b);
printf("%lld\n",Query( a,b,1,n,1));
}
else if(s[0]=='C')
{
scanf("%d%d%d",&a,&b,&c);
Updata(a,b,c,1,n,1);
}
}
}
return 0;
}