1 #include<cstdio>
 2 typedef __int64 LL;
 3 #define MAXN 100010
 4 LL tree[MAXN<<2],lazy[MAXN<<2];
 5 inline void PushUp(int rt)
 6 {
 7     tree[rt]=tree[rt<<1]+tree[rt<<1|1];
 8 }
 9 void Build(int L,int R,int rt)
10 {
11     lazy[rt]=0;
12     if(L==R)
13         scanf("%I64d",&tree[rt]);
14     else
15     {
16         int mid=(L+R)>>1;
17         Build(L,mid,rt<<1);
18         Build(mid+1,R,rt<<1|1);
19         PushUp(rt);
20     }
21 }
22 inline void PushDown(int mid,int L,int R,int rt)
23 {
24     if(lazy[rt])
25     {
26         lazy[rt<<1]+=lazy[rt];
27         lazy[rt<<1|1]+=lazy[rt];
28         tree[rt<<1]+=(mid-L+1)*lazy[rt];
29         tree[rt<<1|1]+=(R-mid)*lazy[rt];
30         lazy[rt]=0;
31     }
32 }
33 LL Query(int x,int y,int L,int R,int rt)
34 {
35     if(x<=L&&R<=y)
36         return tree[rt];
37     int mid=(L+R)>>1;
38     LL ans=0;
39     PushDown(mid,L,R,rt);
40     if(mid>=x)
41         ans+=Query(x,y,L,mid,rt<<1);
42     if(y>mid)
43         ans+=Query(x,y,mid+1,R,rt<<1|1);
44     return ans;
45 }
46 void Update(int x,int y,int z,int L,int R,int rt)
47 {
48     if(x<=L&&R<=y)
49     {
50         lazy[rt]+=z;
51         tree[rt]+=(LL)(R-L+1)*z;
52     }
53     else
54     {
55         int mid=(L+R)>>1;
56         PushDown(mid,L,R,rt);
57         if(mid>=x)
58             Update(x,y,z,L,mid,rt<<1);
59         if(y>mid)
60             Update(x,y,z,mid+1,R,rt<<1|1);
61         PushUp(rt);
62     }
63 }
64 int main()
65 {
66     char ch;
67     int n,q,a,b,c;
68     while(~scanf("%d%d",&n,&q))
69     {
70         Build(1,n,1);
71         while(q--)
72         {
73             scanf(" %c",&ch);
74             if(ch=='Q')
75             {
76                 scanf("%d%d",&a,&b);
77                 printf("%I64d\n",Query(a,b,1,n,1));
78             }
79             else
80             {
81                 scanf("%d%d%d",&a,&b,&c);
82                 Update(a,b,c,1,n,1);
83             }
84         }
85     }
86     return 0;
87 }
posted on 2012-05-21 14:40  DrunBee  阅读(414)  评论(0编辑  收藏  举报