poj 3468 A Simple Problem with Integers

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

  1 #include <cstdio>

  2 #include <cstring>

  3 #include <algorithm>

  4 #define maxn 100100

  5 using namespace std;

  6 

  7 __int64 a[maxn],c;

  8 int N,Q,x,y;

  9 struct node

 10 {

 11     int l;

 12     int r;

 13     __int64 sum;

 14     __int64 add;

 15 } p[maxn*4];

 16 

 17 void build_tree(int i,int l,int r)

 18 {

 19     p[i].l=l;

 20     p[i].r=r;

 21     if(l==r)

 22     {

 23         p[i].add=0;

 24         p[i].sum=a[r];

 25         return ;

 26     }

 27     int mid=(l+r)/2;

 28     build_tree(i+i,l,mid);

 29     build_tree(i+i+1,mid+1,r);

 30     p[i].sum=p[i+i].sum+p[i+i+1].sum;

 31 }

 32 

 33 void update(int i,int l,int r,__int64 add)

 34 {

 35 p[i].sum+=(add*(r-l+1));

 36     if(p[i].l==l&&p[i].r==r)

 37     {

 38         

 39         p[i].add+=add;

 40         return ;

 41     }

 42     if(p[i].add)

 43     {

 44         p[i+i].add+=p[i].add;

 45         p[i+i+1].add+=p[i].add;

 46         p[i+i].sum+=(p[i].add*(p[i+i].r-p[i+i].l+1));

 47         p[i+i+1].sum+=(p[i].add*(p[i+i+1].r-p[i+i+1].l+1));

 48         p[i].add=0;

 49     }

 50     int mid=(p[i].l+p[i].r)/2;

 51     if(r<=mid)

 52     {

 53         update(i+i,l,r,add);

 54     }

 55     else if(l>mid)

 56     {

 57         update(i+i+1,l,r,add);

 58     }

 59     else

 60     {

 61         update(i+i,l,mid,add);

 62         update(i+i+1,mid+1,r,add);

 63     }

 64 }

 65 

 66 __int64 search1(int i,int l,int r)

 67 {

 68     if(p[i].l==l&&p[i].r==r)

 69     {

 70         return p[i].sum;

 71     }

 72     if(p[i].add)

 73     {

 74         p[i+i].add+=p[i].add;

 75         p[i+i+1].add+=p[i].add;

 76         p[i+i].sum+=p[i].add*(p[i+i].r-p[i+i].l+1);

 77         p[i+i+1].sum+=p[i].add*(p[i+i+1].r-p[i+i+1].l+1);

 78         p[i].add=0;

 79     }

 80     int mid=(p[i].r+p[i].l)/2;

 81     if(r<=mid)

 82     {

 83         search1(i+i,l,r);

 84     }

 85     else if(l>mid)

 86     {

 87         search1(i+i+1,l,r);

 88     }

 89     else

 90     {

 91         return search1(i+i,l,mid)+search1(i+i+1,mid+1,r);

 92     }

 93 }

 94 

 95 int main()

 96 {

 97     while(scanf("%d%d",&N,&Q)!=EOF)

 98     {

 99         for(int i=1; i<=N; i++)

100         {

101             scanf("%I64d",&a[i]);

102         }

103         getchar();

104         build_tree(1,1,N);

105         for(int i=1; i<=Q; i++)

106         {

107             char ch;

108             scanf("%c",&ch);

109             if(ch=='Q')

110             {

111                 scanf("%d%d",&x,&y);

112                 printf("%I64d\n",search1(1,x,y));

113             }

114             else if(ch=='C')

115             {

116                 scanf("%d%d%I64d",&x,&y,&c);

117                 update(1,x,y,c);

118             }

119             getchar();

120         }

121     }

122     return 0;

123 }
View Code

 

你可能感兴趣的:(Integer)