链接:

http://poj.org/problem?id=3468

 

 

代码:

  1 #include<stdio.h>
  2 #include<algorithm>
  3 #include<stdlib.h>
  4 using namespace std;
  5 
  6 #define Lson r<<1
  7 #define Rson r<<1|1
  8 
  9 const int N = 1e5+5;
 10 
 11 struct SegmentTree
 12 {
 13     int L, R;
 14     long long sum, e;
 15     int Mid()
 16     {
 17         return (L+R)>>1;
 18     }
 19     int len()
 20     {
 21         return R-L+1;
 22     }
 23 }a[N<<2];
 24 
 25 void BuildSegTree(int r, int L, int R)
 26 {
 27     a[r].L=L, a[r].R=R;
 28     a[r].e=0;
 29 
 30     if(L==R)
 31     {
 32         scanf("%lld", &a[r].sum);
 33         return ;
 34     }
 35 
 36     BuildSegTree(Lson, L, a[r].Mid());
 37     BuildSegTree(Rson, a[r].Mid()+1, R);
 38 
 39     a[r].sum = a[Lson].sum + a[Rson].sum;
 40 }
 41 void PushDown(int r)
 42 {
 43     a[Lson].sum += a[r].e*a[Lson].len();
 44     a[Lson].e += a[r].e;
 45     a[Rson].sum += a[r].e*a[Rson].len();
 46     a[Rson].e += a[r].e;
 47 
 48     a[r].e=0;
 49 }
 50 void Update(int r, int L, int R, int e)
 51 {
 52     a[r].sum += (R-L+1)*e;
 53 
 54     if(a[r].L==L && a[r].R==R)
 55     {
 56         a[r].e += e;
 57         return ;
 58     }
 59 
 60     PushDown(r);
 61 
 62     if(R<=a[r].Mid())
 63         Update(Lson, L, R, e);
 64     else if(L>a[r].Mid())
 65        Update(Rson, L, R, e);
 66     else
 67     {
 68         Update(Lson, L, a[r].Mid(), e);
 69         Update(Rson, a[r].Mid()+1, R, e);
 70     }
 71 }
 72 long long Query(int r, int L, int R)
 73 {
 74     if(a[r].L==L && a[r].R==R)
 75        return a[r].sum;
 76 
 77     PushDown(r);
 78 
 79     if(R<=a[r].Mid())
 80         return Query(Lson, L, R);
 81     else if(L > a[r].Mid())
 82         return Query(Rson, L, R);
 83     else
 84     {
 85         long long Lsum = Query(Lson, L, a[r].Mid());
 86         long long Rsum = Query(Rson, a[r].Mid()+1, R);
 87 
 88         return Lsum+Rsum;
 89     }
 90 }
 91 
 92 int main()
 93 {
 94    int n, m;
 95    while(scanf("%d%d", &n, &m)!=EOF)
 96    {
 97        BuildSegTree(1, 1, n);
 98 
 99        while(m--)
100        {
101            char s[10];
102            int L, R, e;
103 
104            scanf("%s", s);
105 
106            if(s[0]=='Q')
107            {
108                scanf("%d%d", &L, &R);
109                printf("%lld\n", Query(1, L, R));
110            }
111            else
112            {
113                scanf("%d%d%d", &L, &R, &e);
114                Update(1, L, R, e);
115            }
116        }
117    }
118    return 0;
119 }

 

posted on 2015-07-30 16:37  栀蓝  阅读(132)  评论(0编辑  收藏  举报

levels of contents