poj 3468 成段增减

 

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<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<queue>
 7 #define lson l,m,rt<<1
 8 #define rson m+1,r,rt<<1|1
 9 #define ll long long
10 using namespace std;
11 const int maxn=100005;
12 ll sum[maxn<<2];
13 ll cov[maxn<<2];
14 int n,m,t;
15 void pushup(int rt)
16 {
17     sum[rt]=sum[rt<<1]+sum[rt<<1|1];
18 }
19 void pushdown(int rt,int m)
20 {
21     if(cov[rt])
22     {
23         cov[rt<<1]+=cov[rt];
24         cov[rt<<1|1]+=cov[rt];
25         sum[rt<<1]+=cov[rt]*(m-(m>>1)); //注意括号
26         sum[rt<<1|1]+=cov[rt]*(m>>1);
27         cov[rt]=0;
28     }
29 }
30 void build(int l,int r,int rt)
31 {
32     cov[rt]=0;
33     if(l==r)
34     {
35         scanf("%lld",&sum[rt]);
36         return;
37     }
38     int m=(l+r)>>1;
39     build(lson);
40     build(rson);
41     pushup(rt);
42 }
43 void update(int add,int L,int R,int l,int r,int rt)
44 {
45     if(L<=l&&r<=R)
46     {
47         cov[rt]+=add;
48         sum[rt]+=(ll)add*(r-l+1);
49         return;
50     }
51     pushdown(rt,r-l+1);
52     int m=(l+r)>>1;
53     if(L<=m)    update(add,L,R,lson);
54     if(m<R)    update(add,L,R,rson);
55     pushup(rt);
56 }
57 ll query(int L,int R,int l,int r,int rt)
58 {
59     if (L<=l&&r<=R)
60     {
61         return sum[rt];
62     }
63     pushdown(rt,r-l+1);
64     int m=(l+r)>>1;
65     ll ret=0;
66     if (L<=m) ret+=query(L,R,lson);
67     if (m<R) ret+=query(L,R,rson);
68     return ret;
69 }
70 int main()
71 {
72     int i,j,k,q;
73     //freopen("1.in","r",stdin);
74     scanf("%d%d",&n,&q);
75     build(1,n,1);
76     while(q--)
77     {
78         char s[10];
79         scanf("%s",s);
80         if(s[0]=='Q')
81         {
82             int L,R;
83             scanf("%d%d",&L,&R);
84             printf("%lld\n",query(L,R,1,n,1));
85         }
86         else
87         {
88             int L,R,c;
89             scanf("%d%d%d",&L,&R,&c);
90             update(c,L,R,1,n,1);
91         }
92     }
93     return 0;
94 }

 

posted @ 2015-02-09 14:55  miao_a_miao  阅读(159)  评论(0编辑  收藏  举报