forked from LeadCoding/3-weeks-Google-Prep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1. SegmentTreeSnippetClass.cpp
77 lines (63 loc) · 2.14 KB
/
1. SegmentTreeSnippetClass.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
#include<bits/stdc++.h>
using namespace std;
class SegmentTree {
private:
int siz;
void buildSegmentTree(int startOfRange, int endOfRange, int indexOfSegmentTree, vector<int>&v) {
if(startOfRange == endOfRange) {
segmentTreeArray[indexOfSegmentTree] = v[startOfRange];
return;
}
int mid = (startOfRange + endOfRange)/2;
buildSegmentTree(startOfRange, mid, indexOfSegmentTree*2 + 1, v);
buildSegmentTree(mid + 1, endOfRange, indexOfSegmentTree*2 + 2,v);
segmentTreeArray[indexOfSegmentTree] = segmentTreeArray[indexOfSegmentTree*2 + 1] + segmentTreeArray[indexOfSegmentTree*2 + 2];
}
int query(int startOfRange, int endOfRange, int indexOfSegmentTree, int l, int r) {
if(endOfRange < l || startOfRange > r) {
return 0;
}
if(startOfRange >= l && endOfRange <= r) {
return segmentTreeArray[indexOfSegmentTree];
}
int mid = (startOfRange + endOfRange)/2;
int leftSide = query(startOfRange, mid, indexOfSegmentTree*2 + 1, l, r);
int rightSide = query(mid+1, endOfRange, indexOfSegmentTree*2 + 2, l, r);
return leftSide + rightSide;
}
void update(int startOfRange, int endOfRange, int indexOfSegmentTree, int index, int val) {
if(startOfRange == endOfRange) {
segmentTreeArray[indexOfSegmentTree] = val;
return;
}
int mid = (startOfRange + endOfRange)/2;
if(mid >= index) {
update(startOfRange, mid, indexOfSegmentTree*2 + 1, index, val);
} else {
update(mid + 1, endOfRange, indexOfSegmentTree*2 + 2, index, val);
}
segmentTreeArray[indexOfSegmentTree] = segmentTreeArray[indexOfSegmentTree*2 +1] + segmentTreeArray[indexOfSegmentTree*2 +2];
}
public:
vector<int> segmentTreeArray;
SegmentTree(int siz):siz(siz) {
segmentTreeArray.resize(siz*4);
}
void buildSegmentTree(vector<int>&v) {
buildSegmentTree(0, siz - 1, 0, v);
}
int query(int l, int r) {
return query(0, siz - 1, 0, l, r);
}
void update(int index, int val) {
update(0, siz - 1, 0, index, val);
}
};
int main() {
vector<int> v = {1, 2, 3, 4, 5};
SegmentTree segmentTree(5);
segmentTree.buildSegmentTree(v);
// for(auto a:segmentTree.segmentTreeArray) cout<<a<<" ";
segmentTree.update(2,10);
cout<<segmentTree.query(2,4);
}