forked from anishpatil31/CP-Templates-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSegment Tree.cpp
52 lines (41 loc) · 1.26 KB
/
Segment Tree.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
// Code for segment tree data structure.
// Time Complexity - O(n) for build and O(log n) for query where n is the length of array.
int segmentTree[4 * LIM], a[LIM];
void build(int index, int l, int r)
{
if (l == r)
segmentTree[index] = a[l];
else
{
int mid = (l + r) / 2;
build(2 * index, l, mid);
build(2 * index + 1, mid + 1, r);
segmentTree[index] = segmentTree[index * 2] +
segmentTree[index * 2 + 1];
}
}
int query(int index, int curL, int curR, int queryL, int queryR)
{
if (queryR < curL || queryL > curR)
return 0;
if (queryL <= curL && queryR >= curR)
return segmentTree[index];
int mid = (curL + curR) / 2;
return query(2 * index, curL, mid, queryL, queryR) +
query(2 * index + 1, mid + 1, curR, queryL, queryR);
}
void update(int index, int l, int r, int pos, int newVal)
{
if (l == r)
segmentTree[index] = newVal;
else
{
int mid = (l + r) / 2;
if (pos <= mid)
update(2 * index, l, mid, pos, newVal);
else
update(2 * index + 1, mid + 1, r, pos, newVal);
segmentTree[index] = segmentTree[index * 2] +
segmentTree[index * 2 + 1];
}
}