首页 > 代码库 > POJ3468(KB7-C 线段树)

POJ3468(KB7-C 线段树)

A Simple Problem with Integers

Time Limit: 5000MS  Memory Limit: 131072K
Total Submissions: 108903   Accepted: 33919
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

Hint

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

Source

POJ Monthly--2007.11.25, Yang Yi
 
带lazy的线段树
 1 //2017-05-17
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 #define ll long long
 7 #define mid ((st[id].l+st[id].r)>>1)
 8 #define lson (id<<1)
 9 #define rson ((id<<1)|1)
10 
11 using namespace std;
12 
13 const ll N = 100005;
14 ll arr[N];
15 struct Node{
16     ll l, r, sum, lazy;
17 }st[N<<2];
18 
19 void build(int id, int l, int r)
20 {
21     st[id].l = l; st[id].r = r; st[id].lazy = 0;
22     if(l == r){
23         st[id].sum = arr[l];
24         return;
25     }
26     build(lson, l, mid);
27     build(rson, mid+1, r);
28     st[id].sum = st[lson].sum+st[rson].sum;
29 }
30 
31 void push_down(int id)
32 {
33     if(st[id].lazy != 0){
34         st[lson].lazy += st[id].lazy;
35         st[rson].lazy += st[id].lazy;
36         st[id].sum += (st[id].r-st[id].l+1)*st[id].lazy;
37         st[id].lazy = 0;
38     }
39     return;
40 }
41 
42 ll query(int id, int l, int r)
43 {
44     if(st[id].l == l && st[id].r == r)return st[id].sum+(r-l+1)*st[id].lazy;
45     push_down(id);
46     if(r <= mid)return query(lson, l, r);
47     else if(l > mid)return query(rson, l, r);
48     else return query(lson, l, mid)+query(rson, mid+1, r);
49 }
50 
51 void update(int id, int l, int r, int w)
52 {
53     if(st[id].l == l && st[id].r == r){
54         st[id].lazy += w;
55         return;
56     }
57     st[id].sum += (r-l+1)*w;
58     if(r <= mid)update(lson, l, r, w);
59     else if(l > mid)update(rson, l, r, w);
60     else{
61         update(lson, l, mid, w);
62         update(rson, mid+1, r, w);
63     }
64 }
65 
66 int main()
67 {
68     ll n, q;
69     while(scanf("%lld%lld", &n, &q)!=EOF){
70         for(int i = 1; i <= n; i++)
71           scanf("%lld", &arr[i]);
72         build(1, 1, n);
73         char op[5];
74         ll a, b, c;
75         while(q--){
76             scanf("%s", op);
77             if(op[0] == Q){
78                 scanf("%lld%lld", &a, &b);
79                 printf("%lld\n", query(1, a, b));
80             }else if(op[0] == C){
81                 scanf("%lld%lld%lld", &a, &b, &c);
82                 update(1, a, b, c);
83             }
84         }
85     }
86 
87     return 0;
88 }

 

POJ3468(KB7-C 线段树)