-
Notifications
You must be signed in to change notification settings - Fork 344
/
Copy pathlazySegmentTree.cpp
80 lines (65 loc) · 1.7 KB
/
lazySegmentTree.cpp
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
#include<bits/stdc++.h>
using namespace std;
#define inf 1e18
int lazy[10000] = {0};
void lazyUpdate(int tree[], int s, int e, int l, int r, int val , int index) {
if (lazy[index] != 0) {
tree[index] += lazy[index];
if (e != s) {
lazy[2 * index] += lazy[index];
lazy[2 * index + 1] += lazy[index];
}
lazy[index] = 0;
}
if (s > r or e < l) return;
if (s >= l and e <= r) {
tree[index] += val;
if (e != s) {
lazy[2 * index] += val;
lazy[2 * index + 1] += val;
}
return;
}
int mid = (s + e) / 2;
lazyUpdate(tree, s, mid, l, r, val, 2 * index);
lazyUpdate(tree, mid + 1, e, l, r, val, 2 * index + 1);
tree[index] = min(tree[2 * index], tree[2 * index + 1]);
return;
}
int lazyQuery(int tree[], int s, int e, int l, int r, int index) {
if (lazy[index] != 0) {
tree[index] += lazy[index];
if (e != s) {
lazy[2 * index] = lazy[index];
lazy[2 * index + 1] = lazy[index];
}
lazy[index] = 0;
}
if (s > r or e < l) return inf;
if (s >= l and e <= r) return tree[index];
int mid = (s + e) / 2;
int left = lazyQuery(tree, s, mid, l, r, 2 * index);
int right = lazyQuery(tree, mid + 1, e, l, r, 2 * index + 1);
return min(left, right);
}
void build(int a[], int tree[], int s, int e, int index) {
if (s == e) {
tree[index] = a[e];
return;
}
int mid = (s + e) / 2;
build(a, tree, s, mid, 2 * index);
build(a, tree, mid + 1, e, 2 * index + 1);
tree[index] = min(tree[2 * index], tree[2 * index + 1]);
return;
}
int32_t main()
{
//An example of implementing this code
// int a[] = {1, 3, 2, -5, 6, 4};
// int n = sizeof(a) / sizeof(int);
// int tree[4 * n + 1];
// build(a, tree, 0, n - 1, 1);
// lazyUpdate(tree, 0, n - 1, 0, 2, 10, 1);
// return 0;
}