首页 > 代码库 > [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

[POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

A Simple Problem with Integers
 

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 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4

Sample Output

455915

Hint

The sums may exceed the range of 32-bit integers.

Source

POJ Monthly--2007.11.25, Yang Yi
 

线段树功能:update:成段增减 query:区间求和

 

  1 #include<cstdio>  2 #include<algorithm>  3   4 #define clr(x,y) memset(x,y,sizeof(x))  5 #define LL       long long  6 #define lson l,m,rt<<1  7 #define rson m+1,r,rt<<1|1  8   9 const int maxn=1e5+3511; 10 using namespace std; 11  12 LL sum[maxn<<2],Lazy[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(Lazy[rt]) { 22         Lazy[rt<<1]+=Lazy[rt]; 23         Lazy[rt<<1|1]+=Lazy[rt]; 24         sum[rt<<1]+=(m-(m>>1))*Lazy[rt]; 25         sum[rt<<1|1]+=(m>>1)*Lazy[rt]; 26         Lazy[rt]=0; 27     }     28 } 29  30 void build(int l,int r,int rt) 31 { 32     int m; 33     Lazy[rt]=0; 34     if(l==r) { 35         scanf("%lld",&sum[rt]); 36         return; 37     } 38      39     m=(l+r)>>1; 40     build(lson); 41     build(rson); 42     PushUp(rt); 43 } 44  45 void Updata(int L,int R,int c,int l,int r,int rt) 46 { 47     int m; 48     if(L<=l && r<=R) { 49         Lazy[rt]+=c; 50         sum[rt]+=(LL)c*(r-l+1); 51         return; 52     } 53      54     PushDown(rt,r-l+1); 55     m=(l+r)>>1; 56     if(L<=m) Updata(L,R,c,lson); 57     if(R>m)  Updata(L,R,c,rson); 58     PushUp(rt); 59      60 } 61  62  63 LL query(int L,int R,int l,int r,int rt) 64 { 65     int m; 66     LL ret=0; 67     if(L<=l && r<=R) { 68         return sum[rt]; 69     } 70      71     PushDown(rt,r-l+1); 72     m=(l+r)>>1; 73     if(L<=m) ret+=query(L,R,lson); 74     if(R>m)  ret+=query(L,R,rson); 75      76     return  ret; 77 } 78  79  80  81 int main() 82 { 83     int Q,n,a,b,c; 84     char st[10]; 85      86     scanf("%d%d",&n,&Q); 87     build(1,n,1); 88      89     while(Q--) { 90         scanf("%s",st); 91         if(st[0]==C) { 92             scanf("%d%d%d",&a,&b,&c); 93             Updata(a,b,c,1,n,1); 94         } else { 95             scanf("%d%d",&a,&b); 96             printf("%lld\n",query(a,b,1,n,1)); 97         } 98          99     }100     101     return 0;102 }

 

 

 

[POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]