-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSegmentTree.cpp
More file actions
91 lines (79 loc) · 2.35 KB
/
SegmentTree.cpp
File metadata and controls
91 lines (79 loc) · 2.35 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
85
86
87
88
89
90
91
#include <bits/stdc++.h>
using namespace std;
int getSumUtil(int *st, int treeStart, int treeEnd, int start, int end, int idx){
// current segment inside the search range
if(treeStart >= start and treeEnd <= end)
return st[idx];
// current segment outside search range
if(treeStart > end or treeEnd < start)
return 0;
int mid = (treeStart + treeEnd) / 2;
return getSumUtil(st, treeStart, mid, start, end, 2*idx+1)
+ getSumUtil(st, mid+1, treeEnd, start, end, 2*idx+2);
}
int getSum(int *st, int n, int start, int end){
if(start < 0 or end > n-1 or start > end){
cout<<"Error at getSum : invalid index range.\n";
return 0;
}
return getSumUtil(st, 0, n-1, start, end, 0); // search sum from root
}
void updateSTUtil(int arr[], int *st, int start, int end, int arrayIdx, int idx, int diff){
if(arrayIdx < start or arrayIdx > end)
return;
st[idx] += diff;
if(start != end){
int mid = (start+end)/2;
updateSTUtil(arr, st, start, mid, arrayIdx, idx*2+1, diff); // update left child
updateSTUtil(arr, st, mid+1, end, arrayIdx, idx*2+2, diff); // update right child
}
}
void updateST(int arr[], int *st, int n, int idx, int val){
if (idx < 0 || idx > n-1){
cout<<"Error at updateST : index out of range.\n";
return;
}
int diff = val - arr[idx];
arr[idx] = val;
updateSTUtil(arr, st, 0, n-1, idx, 0, diff); // update from root
}
int buildSTUtil(int arr[], int *st, int start, int end, int idx){
if(start == end){
st[idx] = arr[start];
return arr[start];
}
int mid = (start+end)/2;
st[idx] = buildSTUtil(arr, st, start, mid, idx*2+1)
+ buildSTUtil(arr, st, mid+1, end, idx*2+2);
return st[idx];
}
int *buildST(int arr[], int n){
int x = (int)(ceil(log2(n)));
int max_size = 2*(int)pow(2, x) - 1;
int *st = new int[max_size];
// construct from root tree
buildSTUtil(arr, st, 0, n-1, 0);
return st;
}
int main(){
int arr[] = {1,3,5,2,3,5,91,2,8,2,7};
int n = sizeof(arr)/sizeof(int);
int *st = buildST(arr, n);
while(true){
cout<<"cmd: ";
int cmd; cin>>cmd;
if(cmd == 1){
cout<<"get sum (l, r): ";
int l,r;
cin>>l>>r;
cout<<getSum(st, n, l, r)<<endl;
}else if( cmd == 2){
cout<<"update (idx, val): ";
int idx, val;
cin>>idx>>val;
updateST(arr, st, n, idx, val);
cout<<"updated.\n";
}
}
return 0;
}