POJ 3468 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 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

Hint

The sums may exceed the range of 32-bit integers.
线段树区间修改+区间查询
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 #include<cstdlib>
 6 #define MAXN 100005
 7 
 8 using namespace std;
 9 
10 typedef long long LL;
11 struct Seg_Ment_Tree{ int l,r;LL Sum,lazy; } tre[MAXN << 3];
12 
13 void Build(int u,int l,int r){
14     tre[u].l = l,tre[u].r = r,tre[u].lazy = 0,tre[u].Sum = 0;
15     if(l == r){ scanf("%lld",&tre[u].Sum); return ; }
16     int Mid = l + r >> 1;
17     Build(u<<1,l,Mid); Build(u<<1|1,Mid+1,r);
18     tre[u].Sum = tre[u << 1].Sum + tre[u << 1 | 1].Sum;
19 }
20 
21 inline void Push_Down(int u){
22     tre[u<<1].Sum += tre[u].lazy * (tre[u<<1].r - tre[u<<1].l + 1);
23     tre[u<<1].lazy += tre[u].lazy;
24     tre[u<<1|1].Sum += tre[u].lazy * (tre[u<<1|1].r - tre[u<<1|1].l + 1);
25     tre[u<<1|1].lazy += tre[u].lazy;
26     tre[u].lazy = 0;
27 }
28 
29 void Modify(int u,int l,int r,int val){
30     if(l <= tre[u].l && tre[u].r <= r){
31         tre[u].Sum += val *(tre[u].r - tre[u].l + 1); tre[u].lazy += val;
32         return;
33     }
34     if(tre[u].lazy) Push_Down(u);
35     int Mid = tre[u].l + tre[u].r >> 1;
36     if(l > Mid) Modify(u<<1|1,l,r,val);
37     else if(r <= Mid) Modify(u<<1,l,r,val);
38     else Modify(u<<1,l,Mid,val),Modify(u<<1|1,Mid+1,r,val);
39     tre[u].Sum = tre[u<<1].Sum + tre[u<<1|1].Sum;
40 }
41 
42 LL Query(int u,int l,int r){
43     if(l <= tre[u].l && tre[u].r <= r) return tre[u].Sum;
44     if(tre[u].lazy) Push_Down(u);
45     int Mid = tre[u].l + tre[u].r >> 1;
46     if(r <= Mid) return Query(u<<1,l,r);
47     else if(l > Mid) return Query(u<<1|1,l,r);
48     else return Query(u<<1,l,Mid) + Query(u<<1|1,Mid+1,r);
49 }
50 
51 int main(int argc,char *argv[]){
52     int l,r,val,n,m; char c;
53     while(scanf("%d%d",&n,&m) != EOF){
54         Build(1,1,n);
55         for(int i=1; i<=m; ++i){
56             cin >> c; scanf("%d%d",&l,&r);
57               if(c == 'C') { scanf("%d",&val); Modify(1,l,r,val); }
58             else printf("%lld
",Query(1,l,r));
59         }
60     }
61     return 0;
62 }
代码
原文地址:https://www.cnblogs.com/whistle13326/p/7673943.html