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.
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxm = 1e5 + 5;
typedef long long ll;
ll a[maxm];
struct node
{
ll val;//具体数值
ll len;//覆盖区间长度
ll lazy;//延迟标记
ll l,r;//左右儿子编号
}tree[maxm<<2];
//递归建树,遇到叶子节点直接赋值,否则递归遍历左右建树,最后回溯即可
void build(ll l,ll r,ll root)
{
tree[root].lazy = 0;
tree[root].l = l;
tree[root].r = r;
tree[root].len = r - l + 1;
if (l==r) tree[root].val = a[l];
else
{
ll mid = (l + r)>>1;
build(l,mid,root<<1);//递归构造左子树
build(mid+1,r,root<<1|1);//递归构造右子数
tree[root].val = tree[root<<1].val + tree[root<<1|1].val;//存储左右子树的和
}
}
/*//单点更新
//递归更新,递归至要更新的叶子节点,回溯更改该叶子节点会导致其他节点的值
void add(int id,int change,int root)
{
if (tree[root].l==tree[root].r)
tree[root].val += change;
else
{
int mid = (tree[root].l + tree[root].r)>>1;
if (id<=l) add(id,change,root<<1);
else add(id,change,root<<1|1);
tree[root].val = tree[root<<1].val + tree[root<<1|1].val;
}
}
//查询区间最大值最小值
//只需要在建树的时候注意tree[].val存储的是每个节点区间的最大值或最小值即可。
int ask(int l,inr r,int root)
{
if (tree[root].l==l &&tree[root].r==r)
return tree[root].val;
else
{
int mid = (tree[root].l + tree[root].r)>>1;
if (mid>=r) return ask(l,r,root<<1);
else if (mid<l) return ask(root<<1|1);
else return ask(l,mid,root<<1) + ask(mid+1,r,root<<1|1);
}
}*/
//核心操作(pushdown):
void pushdown(ll root)//向下传递lazy标记
{
if (tree[root].lazy)
{
tree[root<<1].lazy += tree[root].lazy;
tree[root<<1|1].lazy += tree[root].lazy;
tree[root<<1].val += tree[root<<1].len * tree[root].lazy;
tree[root<<1|1].val += tree[root<<1|1].len * tree[root].lazy;
tree[root].lazy = 0;
}
}
//区间更新
void update(ll l,ll r,ll root,ll change)
{
if (tree[root].l>=l && tree[root].r<=r)
{
tree[root].lazy += change;
tree[root].val += tree[root].len * change;
return;
}
if (tree[root].l>r || tree[root].r<l)
return;
if (tree[root].lazy) pushdown(root);
update(l,r,root<<1,change);
update(l,r,root<<1|1,change);
tree[root].val = tree[root<<1].val + tree[root<<1|1].val;
}
//计算区间和
ll query(ll l, ll r,ll root)
{
if (tree[root].l>=l && tree[root].r<=r)
return tree[root].val;
if (tree[root].l>r || tree[root].r<l)
return 0;
if (tree[root].lazy) pushdown(root);
return query(l,r,root<<1) + query(l,r,root<<1|1);
}
int main()
{
char s[3];
int n,m,i,z;
ll x,y,k;
scanf("%d %d",&n,&m);
for (i=1;i<=n;i++)
scanf("%lld",&a[i]);
build(1,n,1);
while (m--)
{
scanf("%s",s);
if (s[0]=='Q')
{
scanf("%lld %lld",&x,&y);
printf("%lld\n",query(x,y,1));
}
else
{
scanf("%lld %lld %lld",&x,&y,&k);
update(x,y,1,k);
}
}
return 0;
}