Given a sequence, you need to deal with the following two types of queries.
"C a B c" means that all values in [a, b] intervals are increased by C (-10000 < C < 10000).
"Q a b" asks the sum of all values in the [a, b] interval.
Input
The first line contains two integers N,Q. 1 < N,Q < 100000.
The second line contains n integers, representing the initial sequence A (-1000000000 < Ai < 1000000000).
Next, line Q asks in the form of a title description.
Output
For each Q-start question, you need to output the corresponding answers, one line for each answer.
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
Template problem
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #define eps 0.00000001 using namespace std; int q[400000]; struct point { int l,r; long long nsum; long long add; }node[400000]; void Build(int P,int L,int R) { node[P].l=L; node[P].r=R; node[P].add=0; if(L==R) { node[P].nsum=q[L]; return ; } int mid=L+((R-L)>>1); Build(P<<1,L,mid); Build(P<<1|1,mid+1,R); node[P].nsum=node[P<<1].nsum+node[P<<1|1].nsum; } void Add(int P,int L,int R,long long c) { if(node[P].l==L&&node[P].r==R) { node[P].add+=c; return ; } node[P].nsum+=c*(R-L+1); int mid=(node[P].l+node[P].r)>>1; if(R<=mid) Add(P<<1,L,R,c); else if(L>mid) Add(P<<1|1,L,R,c); else { Add(P<<1,L,mid,c); Add(P<<1|1,mid+1,R,c); } } long long Query(int P,int L,int R) { if(node[P].l==L&&node[P].r==R) { return node[P].nsum+(R-L+1)*node[P].add; } node[P].nsum+=(node[P].r-node[P].l+1)*node[P].add; int mid=(node[P].l+node[P].r)>>1; Add(P<<1,node[P].l,mid,node[P].add); Add(P<<1|1,mid+1,node[P].r,node[P].add); node[P].add=0; if(R<=mid) return Query(P<<1,L,R); else if(L>mid) return Query(P<<1|1,L,R); else return Query(P<<1,L,mid)+Query(P<<1|1,mid+1,R); } int main() { int n,m,i; int a,b,c; char s[10]; while(~scanf("%d%d",&n,&m)) { for(i=1;i<=n;i++) { scanf("%d",&q[i]); } Build(1,1,n); for(i=0;i<m;i++) { scanf("%s",s); if(s[0]=='C') { scanf("%d%d%d",&a,&b,&c); Add(1,a,b,c); } else { scanf("%d%d",&a,&b); cout<<Query(1,a,b)<<endl; } } } return 0; }