-
Notifications
You must be signed in to change notification settings - Fork 342
/
Segment_tree.cpp
174 lines (141 loc) · 3.71 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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//Segment Tree
//Updation and Finding sum in given range happens in O(logn)
#include <bits/stdc++.h>
using namespace std;
int getMid(int s, int e) {
return s + (e -s)/2;
}
int getSumUtil(int *st, int ss, int se, int qs, int qe, int si)
{
if (qs <= ss && qe >= se)
return st[si];
if (se < qs || ss > qe)
return 0;
int mid = getMid(ss, se);
return getSumUtil(st, ss, mid, qs, qe, 2*si+1) +
getSumUtil(st, mid+1, se, qs, qe, 2*si+2);
}
void updateValueUtil(int *st, int ss, int se, int i, int diff, int si)
{
if (i < ss || i > se)
return;
st[si] = st[si] + diff;
if (se != ss)
{
int mid = getMid(ss, se);
updateValueUtil(st, ss, mid, i, diff, 2*si + 1);
updateValueUtil(st, mid+1, se, i, diff, 2*si + 2);
}
}
void updateValue(int arr[], int *st, int n, int i, int new_val)
{
if (i < 0 || i > n-1)
{
cout<<"Invalid Input";
return;
}
int diff = new_val - arr[i];
arr[i] = new_val;
updateValueUtil(st, 0, n-1, i, diff, 0);
}
int getSum(int *st, int n, int qs, int qe)
{
if (qs < 0 || qe > n-1 || qs > qe)
{
cout<<"Invalid Input";
return -1;
}
return getSumUtil(st, 0, n-1, qs, qe, 0);
}
int constructSTUtil(int arr[], int ss, int se, int *st, int si)
{
if (ss == se)
{
st[si] = arr[ss];
return arr[ss];
}
int mid = getMid(ss, se);
st[si] = constructSTUtil(arr, ss, mid, st, si*2+1) +
constructSTUtil(arr, mid+1, se, st, si*2+2);
return st[si];
}
int *constructST(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];
constructSTUtil(arr, 0, n-1, st, 0);
return st;
}
int main()
{
int *arr , n , i , a , b ;
cout << "Enter size of the array" << endl ;
cin >> n ;
arr = new int[n] ;
cout << "Enter the elements of the array" << endl ;
for ( i = 0 ; i < n ; i++ )
cin >> arr[i] ;
int *st = constructST(arr, n);
int k = 1 ;
do{
cout << "Enter 1 to find the sum of the values in the given range" << endl ;
cout << "Enter 2 to update a value in the array" << endl ;
cout << "Enter 0 to exit" << endl ;
cout << "Enter your choice" << endl ;
int k ;
cin >> k ;
switch(k){
case 1 : cout << "Enter the range" << endl ;
cin >> a >> b ;
cout<<"Sum of values in given range = "<<getSum(st, n, a, b)<<endl;
break ;
case 2 : cout << "Enter the position to be updated and number to be inserted" << endl ;
cin >> a >> b ;
updateValue(arr, st, n, a, b);
cout << "Updated array is : " ;
for ( i = 0 ; i < n ; i++ )
cout << arr[i] << " " ;
cout << endl ;
break ;
case 0 : exit(0) ;
}
}
while (1);
return 0;
}
/*Sample Output :
Enter size of the array
5
Enter the elements of the array
1 3 5 7 9
Enter 1 to find the sum of the values in the given range
Enter 2 to update a value in the array
Enter 0 to exit
Enter your choice
1
Enter the range
1 3
Sum of values in given range = 15
Enter 1 to find the sum of the values in the given range
Enter 2 to update a value in the array
Enter 0 to exit
Enter your choice
2
Enter the position to be updated and number to be inserted
1 10
Updated array is : 1 10 5 7 9
Enter 1 to find the sum of the values in the given range
Enter 2 to update a value in the array
Enter 0 to exit
Enter your choice
1
Enter the range
1 3
Sum of values in given range = 22
Enter 1 to find the sum of the values in the given range
Enter 2 to update a value in the array
Enter 0 to exit
Enter your choice
0
*/