-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSegment Tree - Lazy Propagation.cpp
More file actions
84 lines (64 loc) · 1.71 KB
/
Segment Tree - Lazy Propagation.cpp
File metadata and controls
84 lines (64 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int MAX = 1e5 + 10;
ll arr[MAX];
struct Node
{
ll prop, sum;
} tree[MAX * 4];
void build(ll node, ll l, ll r)
{
if(l == r)
{
tree[node].sum = arr[l];
return;
}
ll lf = node << 1;
ll rt = (node << 1) + 1;
ll mid = (l + r) >> 1;
build(lf, l, mid);
build(rt, mid + 1, r);
tree[node].sum = tree[lf].sum + tree[rt].sum;
tree[node].prop = 0;
}
ll query(ll node, ll l, ll r, ll L, ll R, ll cary = 0) // returns result for the range (L, R) inclusive
{
if(L > r || R < l)
return 0;
if(l >= L and r <= R)
return tree[node].sum + cary * (r - l + 1);
ll lf = node << 1;
ll rt = (node << 1) + 1;
ll mid = (l + r) >> 1;
ll u = query(lf, l, mid, L, R, cary + tree[node].prop);
ll v = query(rt, mid + 1, r, L, R, cary + tree[node].prop);
return u + v;
}
void update(ll node, ll l, ll r, ll L, ll R, ll val) // updates the values of the range (L, R) inclusive with 'val'
{
if(L > r || R < l)
return;
if(l >= L && r <= R)
{
tree[node].sum += ((r - l + 1) * val);
tree[node].prop += val;
return;
}
ll lf = node << 1;
ll rt = (node << 1) + 1;
ll mid = (l + r) >> 1;
update(lf, l, mid, L, R, val);
update(rt, mid + 1, r, L, R, val);
tree[node].sum = tree[lf].sum + tree[rt].sum + (r - l + 1) * tree[node].prop;
}
int main()
{
for(int i = 1; i <= 100000; ++i)
arr[i] = 1;
build(1, 1, 100000);
cout << query(1, 1, 100000, 10, 20) << endl;
update(1, 1, 100000, 10, 20, 2);
cout << query(1, 1, 100000, 10, 20) << endl;
return 0;
}