POJ 3468 A Simple Problem with Integers

A Simple Problem with Integers

 

Description

You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.

Output

You need to answer all Q commands in order. One answer in a line.

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<cstring>
  3 using namespace std;
  4 
  5 const int maxn=100050;
  6 long long int sum[maxn<<2];
  7 long long int add[maxn<<2];
  8 
  9 struct node
 10 {
 11     int l,r;
 12 }tree[maxn<<2];
 13 
 14 void PushUp(int rt)
 15 {
 16     sum[rt]=sum[rt<<1]+sum[rt<<1|1];
 17 }
 18 
 19 void PushDown(int rt,int m)
 20 {
 21     if(add[rt])
 22     {
 23         add[rt<<1]+=add[rt];
 24         add[rt<<1|1]+=add[rt];
 25         sum[rt<<1]+=add[rt]*(m-(m>>1));
 26         sum[rt<<1|1]+=add[rt]*(m>>1);
 27         add[rt]=0;
 28     }
 29 }
 30 
 31 
 32 void build(int l,int r,int rt)
 33 {
 34     tree[rt].l=l;
 35     tree[rt].r=r;
 36     add[rt]=0;
 37     if(l==r)
 38     {
 39         scanf("%lld",&sum[rt]);
 40         return;
 41     }
 42     int mid=(r+l)>>1;
 43     build(l,mid,rt<<1);
 44     build(mid+1,r,rt<<1|1);
 45     PushUp(rt);
 46 }
 47 
 48 void update(long long t,int l,int r,int rt)
 49 {
 50     if(tree[rt].l==l&&tree[rt].r==r)
 51     {
 52         add[rt]+=t;
 53         sum[rt]+=t*(r-l+1);
 54         return;
 55     }
 56     if(tree[rt].l==tree[rt].r)return;
 57     PushDown(rt,tree[rt].r-tree[rt].l+1);
 58     int mid=(tree[rt].r+tree[rt].l)>>1;
 59     if(r<=mid)update(t,l,r,rt<<1);
 60     else if(l>mid)update(t,l,r,rt<<1|1);
 61     else
 62     {
 63         update(t,l,mid,rt<<1);
 64         update(t,mid+1,r,rt<<1|1);
 65     }
 66     PushUp(rt);
 67 }
 68 
 69 long long int query(int l,int r,int rt)
 70 {
 71     if(l==tree[rt].l&&r==tree[rt].r)
 72     {
 73         return sum[rt];
 74     }
 75     PushDown(rt,tree[rt].r-tree[rt].l+1);
 76     int mid=(tree[rt].l+tree[rt].r)>>1;
 77     long long int ans=0;
 78     if(r<=mid)ans+=query(l,r,rt<<1);
 79     else if(l>mid)ans+=query(l,r,rt<<1|1);
 80     else
 81     {
 82         ans+=query(l,mid,rt<<1);
 83         ans+=query(mid+1,r,rt<<1|1);
 84     }
 85     return ans;
 86 }
 87 
 88 int main()
 89 {
 90     //freopen("in.txt","r",stdin);
 91     char com;
 92     int i,t,l,r,val,n,m;
 93     while(scanf("%d%d",&m,&n)!=EOF)
 94     {
 95         memset(add,0,sizeof(add));
 96         build(1,m,1);
 97         while(n--)
 98         {
 99             getchar();
100             scanf("%c",&com);
101             if(com=='Q')
102             {
103                 scanf("%d%d",&l,&r);
104                 long long int ans=query(l,r,1);
105                 printf("%lld
",ans);
106             }
107             else
108             {
109                 scanf("%d%d%d",&l,&r,&val);
110                 update(val,l,r,1);
111             }
112         }
113     }
114     return 0;
115 }
原文地址:https://www.cnblogs.com/homura/p/4737238.html