Topic links:
http://poj.org/problem?id=3468
Code:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #define LL long long 5 using namespace std; 6 const int maxn=100000+20; 7 LL tree[maxn<<2]; 8 LL lazy[maxn<<2]; 9 void pushup(int rt) 10 { 11 tree[rt]=tree[rt<<1]+tree[rt<<1|1]; 12 } 13 void build(int l,int r,int rt) 14 { 15 if(l==r) 16 { 17 scanf("%lld",&tree[rt]); 18 return ; 19 } 20 int m=(l+r)>>1; 21 build(l,m,rt<<1); 22 build(m+1,r,rt<<1|1); 23 pushup(rt); 24 } 25 void pushdown(int rt,int ln,int rn) 26 { 27 if(lazy[rt]) 28 { 29 lazy[rt<<1]+=lazy[rt]; 30 lazy[rt<<1|1]+=lazy[rt]; 31 tree[rt<<1]+=lazy[rt]*ln; 32 tree[rt<<1|1]+=lazy[rt]*rn; 33 lazy[rt]=0; 34 } 35 } 36 void update(int L,int R,int C,int l,int r,int rt) 37 { 38 if( L<=l && r <= R) 39 { 40 tree[rt]+=C*(r-l+1); 41 lazy[rt]+=C; 42 return ; 43 } 44 LL m=(l+r)>>1; 45 pushdown(rt,m-l+1,r-m); 46 if(L <= m) 47 update(L,R,C,l,m,rt<<1); 48 if(R > m) 49 update(L,R,C,m+1,r,rt<<1|1); 50 pushup(rt); 51 } 52 LL query(int L,int R,int l,int r,int rt) 53 { 54 if(L <= l && r <= R) 55 { 56 return tree[rt]; 57 } 58 int= m (L + R & lt) >> . 1 ; 59 pushdown (RT, m-L + . 1 , RM); // before recursion must first index the lazy down 60 LL ANS = 0 ; 61 is IF (L <= m ) 62 is ANS + = Query (L, R & lt, L, m, RT << . 1 ); 63 is IF (R & lt> m) 64 ANS + = Query (L, R & lt, m + . 1 , R & lt, RT << . 1 | . 1 ); 65 return ANS; 66 } 67 int main () 68 { 69 int n-, m; 70 cin>>n>>m; 71 build(1,n,1); 72 memset(lazy,0,sizeof(lazy)); 73 char ch[20]; 74 int a,b,c; 75 while(m--) 76 { 77 scanf("%s",ch); 78 if(ch[0]=='Q') 79 { 80 scanf("%d%d",&a,&b); 81 cout<<query(a,b,1,n,1)<<endl; 82 } 83 else 84 { 85 scanf("%d%d%d",&a,&b,&c); 86 update(a,b,c,1,n,1); 87 } 88 } 89 }