1
+ #include < iostream>
2
+ #include < algorithm>
3
+ #include < vector>
4
+ #include < climits>
5
+ #define NMAX 1000005
6
+ using namespace std ;
7
+ int lazy[100000 ] = {0 };
8
+
9
+ // Time complexity is O(logN)
10
+ int querySegmentTree (int *tree,int s,int e,int qs,int qe,int index) {
11
+ // No Overlap
12
+ if (qs>e || qe<s){
13
+ return INT_MAX;
14
+ }
15
+
16
+ // Complete Overlap
17
+ if (s>=qs && e<=qe){
18
+ return tree[index ];
19
+ }
20
+
21
+ // Partial Overlap
22
+ int middle = (s+e)/2 ;
23
+ int leftAns = querySegmentTree (tree,s,middle,qs,qe,2 *index );
24
+ int rightAns = querySegmentTree (tree,middle+1 ,e,qs,qe,2 *index +1 );
25
+ return min (leftAns,rightAns);
26
+ }
27
+
28
+ // Time complexity is O(logN)
29
+ void updateNodeSegmentTree (int *tree,int s,int e,int i,int inc,int index) {
30
+
31
+ // No Overlap
32
+ if (i > e || i < s){
33
+ return ;
34
+ }
35
+
36
+ // Leaf Node
37
+ if (s==e){
38
+ tree[index ] += inc;
39
+ return ;
40
+ }
41
+
42
+ // If i is lying in range of s and e
43
+ int middle = (s + e)/2 ;
44
+ updateNodeSegmentTree (tree,s,middle,i,inc,2 *index );
45
+ updateNodeSegmentTree (tree,middle+1 ,e,i,inc,2 *index +1 );
46
+ tree[index ] = min (tree[2 *index ] , tree[2 *index +1 ]);
47
+ return ;
48
+ }
49
+
50
+ // Time complexity is O(N) in worst case
51
+ void updateRangeSegmentTree (int *tree,int s,int e,int l,int r,int inc,int index) {
52
+
53
+ // No Overlap
54
+ if (l > e || r < s){
55
+ return ;
56
+ }
57
+
58
+ // Leaf node
59
+ if (s==e){
60
+ tree[index ] += inc;
61
+ return ;
62
+ }
63
+
64
+ // Partial overlap
65
+ int middle = (s+e)/2 ;
66
+ updateRangeSegmentTree (tree,s,middle,l,r,inc,2 *index );
67
+ updateRangeSegmentTree (tree,middle+1 ,e,l,r,inc,2 *index +1 );
68
+ tree[index ] = min (tree[2 *index ] , tree[2 *index +1 ]);
69
+ return ;
70
+ }
71
+
72
+ // Optimize Code for updation of range --> Lazy Propogation
73
+ // This will work in O(log(N))
74
+ void updateRangeLazy (int *tree,int s,int e,int l,int r,int inc,int index) {
75
+ // First task is to make Pending updates
76
+ if (lazy[index ] != 0 ) {
77
+ tree[index ] += lazy[index ];
78
+ // if current node is not a leaf node
79
+ if (s != e){
80
+ lazy[2 *index ] += lazy[index ];
81
+ lazy[2 *index +1 ] += lazy[index ];
82
+ }
83
+ lazy[index ] = 0 ;
84
+ }
85
+
86
+ // No Overlap
87
+ if (l>e || r<s){
88
+ return ;
89
+ }
90
+
91
+ // Complete Overlap
92
+ if (s>=l && e<=r) {
93
+ tree[index ] += inc;
94
+ if (s != e){
95
+ lazy[2 *index ] += inc;
96
+ lazy[2 *index +1 ] += inc;
97
+ }
98
+ return ;
99
+ }
100
+
101
+ // Partial overlap
102
+ int middle = (s+e)/2 ;
103
+ updateRangeLazy (tree,s,middle,l,r,inc,2 *index );
104
+ updateRangeLazy (tree,middle+1 ,e,l,r,inc,2 *index +1 );
105
+ tree[index ] = min (tree[2 *index ],tree[2 *index +1 ]);
106
+ return ;
107
+ }
108
+
109
+ int queryLazy (int *tree,int s,int e,int qs,int qe,int index) {
110
+ if (lazy[index ] != 0 ) {
111
+ tree[index ] += lazy[index ];
112
+
113
+ if (s!=e){
114
+ lazy[2 *index ] += lazy[index ];
115
+ lazy[2 *index +1 ] += lazy[index ];
116
+ }
117
+
118
+ lazy[index ] = 0 ;
119
+ }
120
+
121
+ // No Overlap
122
+ if (qs>e || qe<s){
123
+ return INT_MAX;
124
+ }
125
+
126
+ // Complete Overlap
127
+ if (s>=qs && e<=qe) {
128
+ return tree[index ];
129
+ }
130
+
131
+ // Partial Overlap
132
+ int middle = (s+e)/2 ;
133
+ int left = queryLazy (tree,s,middle,qs,qe,2 *index );
134
+ int right = queryLazy (tree,middle+1 ,e,qs,qe,2 *index +1 );
135
+
136
+ return min (left,right);
137
+ }
138
+
139
+ // Time complexity is O(N)
140
+ void buildSegmentTreeHelper (int *a,int s,int e,int *tree,int index) {
141
+ // Base Case
142
+ if (s == e) {
143
+ tree[index ] = a[s];
144
+ return ;
145
+ }
146
+
147
+ // Recursive Case
148
+ int middle = (s + e) / 2 ;
149
+
150
+ buildSegmentTreeHelper (a,s,middle,tree,2 *index );
151
+
152
+ buildSegmentTreeHelper (a,middle + 1 ,e,tree,2 *index +1 );
153
+ tree[index ] = min (tree[2 *index ] , tree[2 *index + 1 ]);
154
+ return ;
155
+ }
156
+
157
+ int * buildSegmentTree (int *a,int n) {
158
+ int * segmentTree = new int [4 *n+1 ];
159
+
160
+ // Helper function to build segment tree
161
+ buildSegmentTreeHelper (a,0 ,n-1 ,segmentTree,1 );
162
+
163
+ return segmentTree;
164
+ }
165
+
166
+ int main (int argc, char const *argv[])
167
+ {
168
+ int n;
169
+ cout << " Enter the number of elements in array\n " ;
170
+ cin >> n;
171
+ int a[n];
172
+ cout << " Enter array elements\n " ;
173
+ for (int i = 0 ; i < n; i++){
174
+ cin >> a[i];
175
+ }
176
+ int q,t,qs,qe,i,inc,l,r;
177
+ char ch;
178
+ cout << " Enter number of queries\n " ;
179
+ cin >> q;
180
+ int index = 1 ;
181
+
182
+ // tree is a dynamic array which will contain the elements of segment tree
183
+ int *tree = buildSegmentTree (a,n);
184
+
185
+ while (q--){
186
+ cin >> ch;
187
+
188
+ if (ch == ' Q' ){
189
+ cin >> qs >> qe;
190
+ cout << " Answer of range is " << queryLazy (tree,0 ,n-1 ,qs,qe,1 );
191
+ cout << ' \n ' ;
192
+ }
193
+ else {
194
+ cin >> l >> r >> inc;
195
+ updateRangeLazy (tree,0 ,n-1 ,l,r,inc,1 );
196
+ }
197
+ }
198
+
199
+ return 0 ;
200
+ }
0 commit comments