Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 41899 | Accepted: 12169 | |
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
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define N 222222
using namespace std;
int num[N];
struct Tree
{
int l;
int r;
long long sum;
long long col;
} tree[N*4];
void push_up(int rt)
{
tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
}
void push_down(int rt,int m)
{
if (tree[rt].col!=0)
{
tree[rt<<1].col+=tree[rt].col;
tree[rt<<1|1].col+=tree[rt].col;
tree[rt<<1].sum+=(long long)(m-(m/2))*tree[rt].col;
tree[rt<<1|1].sum+=(long long)(m/2)*tree[rt].col;
tree[rt].col=0;
}
}
void build(int root,int l,int r)
{
tree[root].l=l;
tree[root].r=r;
tree[root].col=0;
if(tree[root].l==tree[root].r)
{
tree[root].sum=num[l];
return;
}
int mid=(l+r)/2;
build(root<<1,l,mid);
build(root<<1|1,mid+1,r);
push_up(root);
}
void update(int root,int L,int R,int val)
{
if(L<=tree[root].l&&R>=tree[root].r)
{
tree[root].col+=val;
tree[root].sum+=(long long)val*(tree[root].r-tree[root].l+1);
return;
}
push_down(root,tree[root].r-tree[root].l+1);
int mid=(tree[root].l+tree[root].r)/2;
if (L<=mid)
update(root<<1,L,R,val);
if (R>mid)
update(root<<1|1,L,R,val);
push_up(root);
}
long long query(int root,int L,int R)
{
if(L<=tree[root].l&&R>=tree[root].r)
return tree[root].sum;
push_down(root,tree[root].r-tree[root].l+1);
int mid=(tree[root].l+ tree[root].r)/2;
long long ret=0;
if(L<=mid) ret+=query(root<<1,L,R);
if(R>mid) ret+=query(root<<1|1,L,R);
return ret;
}
int main()
{
int n,q;
char s[2];
scanf("%d%d",&n,&q);
for (int i=1; i<=n; i++) scanf("%d",&num[i]);
build(1,1,n);
while (q--)
{
int x,y,z;
scanf("%s",s);
if (s[0]=='Q')
{
scanf("%d%d",&x,&y);
printf("%I64d\n",query(1,x,y));
}
else if (s[0]=='C')
{
scanf("%d%d%d",&x,&y,&z);
update(1,x,y,z);
}
}
return 0;
}