Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 92921 | Accepted: 28910 | |
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 <algorithm> #include <cstdio> #include <cstring> #include <cmath> using namespace std; const int maxn=100000+100; typedef long long ll; ll tree[3*maxn]; ll lazy[3*maxn]; ll num[maxn]; void build (int p,int l,int r) { if(l==r) {tree[p]=num[l];return;} int mid=(l+r)>>1; build(p<<1,l,mid); build((p<<1)|1,mid+1,r); tree[p]=tree[p<<1]+tree[(p<<1)|1]; } void pushdown(int p,int m)//把lazy标记下放到儿子节点 { if(lazy[p]) { lazy[p<<1]+=lazy[p]; lazy[(p<<1)|1]+=lazy[p]; tree[p<<1]+=(m-(m>>1))*lazy[p]; tree[(p<<1)|1]+=(m>>1)*lazy[p]; lazy[p]=0; } } void update(int p,int l,int r,int x,int y,int v) { if(x<=l&&y>=r) { lazy[p]+=v; tree[p]+=(ll)v*(r-l+1);//区间长度 return; } pushdown(p,r-l+1); int mid=(l+r)>>1; if(y<=mid) update(p<<1,l,mid,x,y,v); else if(x>mid) update((p<<1)|1,mid+1,r,x,y,v); else {update(p<<1,l,mid,x,mid,v),update((p<<1)|1,mid+1,r,mid+1,y,v);} tree[p]=tree[p<<1]+tree[(p<<1)|1]; } ll find(int p,int l,int r,int x,int y) { if(x<=l&&y>=r){return tree[p];} pushdown(p,r-l+1); int mid=(l+r)>>1; ll ans=0; if(y<=mid) ans=find(p<<1,l,mid,x,y); else if(x>mid) ans=find((p<<1)|1,mid+1,r,x,y); else ans=find(p<<1,l,mid,x,mid)+find((p<<1)|1,mid+1,r,mid+1,y); return ans; } int main() { int n,q; char s[5]; int a,b,c; while(~scanf("%d%d",&n,&q)) { memset(tree,0,sizeof(tree)); memset(lazy,0,sizeof(lazy)); for(int i=1;i<=n;i++) scanf("%lld",&num[i]); build(1,1,n); while(q--) { scanf("%s",s); if(s[0]=='Q') { scanf("%d%d",&a,&b); printf("%lld\n",find(1,1,n,a,b)); } else { scanf("%d%d%d",&a,&b,&c); update(1,1,n,a,b,c); } } } }