Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 117094 | Accepted: 36404 | |
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<algorithm>
#include<cstdio>
using namespace std;
typedef long long ll;
const int N=1000005;
struct node{
ll x,mark;
}t[N<<2];
void creat(int i,int l,int r)
{
t[i].mark=0ll;
if(l==r)
{
scanf("%lld",&t[i].x);
return;
}
int mid=(l+r)>>1;
creat(i<<1,l,mid);
creat(i<<1|1,mid+1,r);
t[i].x=t[i<<1].x+t[i<<1|1].x;
}
void pushdown(int i,int l,int r)
{
if(t[i].mark)
{
int mid=(l+r)>>1;
t[i<<1].mark+=t[i].mark;
t[i<<1|1].mark+=t[i].mark;
t[i<<1].x+=t[i].mark*(mid-l+1);
t[i<<1|1].x+=t[i].mark*(r-mid);
t[i].mark=0;
}
}
void update(int i,int l,int r,int x,int y, ll z)
{
if(r<x||y<l) return ;
if(x<=l&&r<=y)
{
t[i].mark+=z;
t[i].x+=z*(r-l+1);
return ;
}
pushdown(i,l,r);
int mid=(l+r)>>1;
update(i<<1,l,mid,x,y,z);
update(i<<1|1,mid+1,r,x,y,z);
t[i].x=t[i<<1].x+t[i<<1|1].x;
}
ll sum(int i,int l,int r,int x,int y)
{
if(r<x||y<l) return 0;
if(x<=l&&r<=y) return t[i].x;
pushdown(i,l,r);
int mid=(l+r)>>1;
return sum(i<<1,l,mid,x,y)+sum(i<<1|1,mid+1,r,x,y);
}
int n,m;
int main()
{
scanf("%d%d",&n,&m);
creat(1,1,n);
while(m--)
{
char c;
int a,b;
ll d;
scanf(" %c",&c);
if(c=='Q')
{
scanf("%d%d",&a,&b);
printf("%lld\n",sum(1,1,n,a,b));
continue;
}
scanf("%d%d%lld",&a,&b,&d);
update(1,1,n,a,b,d);
}
return 0;
}