poj3468树状数组之区间更新+区间询问

转载来自:CSDN acm_xyyh

Language:  Default
A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 54069   Accepted: 16249
Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+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

[cpp]  view plain copy print ?
  1. /*分析:由于本题更新的时候是区间更新 
  2. 所以不能直接去一个个更新区间内的点,肯定会超时 
  3. 对于每次更新C(a,b,d)表示区间[a,b]内的值增加d 
  4. 用ans[a]表示a~n区间元素增加的值,所以对于C(a,b,d)有:ans[a]+=d,ans[b+1]-=d; 
  5. 则每次询问的时候Q(a,b),求a~b的和SUM=sum(a,b)+ans[a]*(b-a+1)+ans[a+1]*(b-a)...+ans[b]//sum(a,b)表示a,b的和  
  6. Sum=sum(a,b)+sum(ans[a+t]*(b-a-t+1))=sum(a,b)+sum(ans[i]*(b-i+1));a<=i<=b; 
  7. Sum=sum(a,b)+(b+1)*sum(ans[i])-sum(ans[i]*i);//1~b所以(b+1)*sum(ans[i]),1~a-1则a*sum(ans[i])  
  8. 所以可以用两个树状数组分别表示ans[i]的前缀和 和 ans[i]*i的前缀和  
  9. */  
  10. #include <iostream>  
  11. #include <cstdio>  
  12. #include <cstdlib>  
  13. #include <cstring>  
  14. #include <string>  
  15. #include <queue>  
  16. #include <algorithm>  
  17. #include <map>  
  18. #include <cmath>  
  19. #include <iomanip>  
  20. #define INF 99999999  
  21. typedef long long LL;  
  22. using namespace std;  
  23.   
  24. const int MAX=100000+10;  
  25. LL n,q;  
  26. LL sum[MAX],c1[MAX],c2[MAX];  
  27.   
  28. LL lowbit(LL x){  
  29.     return x&(-x);  
  30. }  
  31.   
  32. void Update(LL x,LL d,LL *c){  
  33.     while(x<=n){  
  34.         c[x]+=d;  
  35.         x+=lowbit(x);  
  36.     }  
  37. }  
  38.   
  39. LL Query(LL x,LL *c){  
  40.     LL sum=0;  
  41.     while(x>0){  
  42.         sum+=c[x];  
  43.         x-=lowbit(x);  
  44.     }  
  45.     return sum;  
  46. }  
  47.   
  48. int main(){  
  49.     char op[3];  
  50.     LL x,y,d;  
  51.     while(~scanf("%lld%lld",&n,&q)){  
  52.         memset(c1,0,sizeof c1);  
  53.         memset(c2,0,sizeof c2);  
  54.         for(int i=1;i<=n;++i)scanf("%lld",sum+i),sum[i]+=sum[i-1];  
  55.         for(int i=0;i<q;++i){  
  56.             scanf("%s",op);  
  57.             if(op[0] == 'C'){//ans[x]+=d,ans[y+1]-=d  
  58.                 scanf("%lld%lld%lld",&x,&y,&d);  
  59.                 Update(x,d,c1);  
  60.                 Update(y+1,-d,c1);  
  61.                 Update(x,x*d,c2);  
  62.                 Update(y+1,-(y+1)*d,c2);  
  63.             }else{  
  64.                 scanf("%lld%lld",&x,&y);  
  65.                 printf("%lld\n",sum[y]-sum[x-1]+Query(y,c1)*(y+1)-Query(x-1,c1)*x-Query(y,c2)+Query(x-1,c2));  
  66.             }  
  67.         }  
  68.     }  
  69.     return 0;  
  70. }  

你可能感兴趣的:(树状数组,区间查询,区间更新)