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
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 #define LL long long 5 #define lson rt<<1,first,mid 6 #define rson rt<<1 | 1,mid+1,end 7 const int maxn=100005; 8 LL tree[maxn<<2]; 9 LL add[maxn<<2]; 10 void push_up(int rt) 11 { 12 tree[rt]=tree[rt<<1]+tree[rt<<1 | 1]; 13 } 14 void push_down(int rt,int m) //更新子节点的数值 15 { 16 if(add[rt]) 17 { 18 add[rt<<1]+=add[rt]; 19 add[rt<<1|1]+=add[rt]; 20 tree[rt<<1]+=add[rt]*(m-(m>>1)); 21 tree[rt<<1|1]+=add[rt]*(m>>1); 22 add[rt]=0; //更新后需要还原 23 } 24 } 25 void build(int rt,int first,int end) 26 { 27 add[rt]=0; 28 if(first==end) 29 { 30 scanf("%lld",&tree[rt]); 31 //cin>>tree[rt]; 32 return ; 33 } 34 int mid=(first+end)>>1; 35 build(lson); 36 build(rson); 37 push_up(rt); 38 return ; 39 } 40 LL query(int rt,int first,int end,int a,int b) //求区间和 41 { 42 if(first>=a&&end<=b) 43 return tree[rt]; 44 push_down(rt,end-first+1); 45 LL sum=0; 46 int mid=(first+end)>>1; 47 if(a<=mid) 48 sum+=query(lson,a,b); 49 if(b>mid) 50 sum+=query(rson,a,b); 51 return sum; 52 } 53 void update(int rt,int first,int end,int a,int b,int c) 54 { 55 if(first>=a&&end<=b) 56 { 57 add[rt]+=c; 58 tree[rt]+=(LL)c*(end-first+1); 59 return ; 60 } 61 push_down(rt,end-first+1); 62 int mid=(first+end)>>1; 63 if(a<=mid) 64 update(lson,a,b,c); 65 if(b>mid) 66 update(rson,a,b,c); 67 push_up(rt); 68 return ; 69 } 70 int main() 71 { 72 int n,q; 73 char ch[5]; 74 int a,b,c; 75 scanf("%d%d",&n,&q); 76 //cin>>n>>q; 77 build(1,1,n); 78 while(q--) 79 { 80 scanf("%s",ch); 81 //cin>>ch; 82 if(ch[0]=='Q') 83 { 84 scanf("%d%d",&a,&b); 85 printf("%lld ",query(1,1,n,a,b)); 86 //cin>>a>>b; 87 //cout<<query(1,1,n,a,b)<<endl; 88 } 89 if(ch[0]=='C') 90 { 91 scanf("%d%d%d",&a,&b,&c); 92 //cin>>a>>b>>c; 93 update(1,1,n,a,b,c); 94 } 95 } 96 return 0; 97 }