|
10 | 10 |
|
11 | 11 | ll min(ll a, ll b) //min function to compute minimum of 2 values
|
12 | 12 | {
|
13 |
| - if (a < b) |
14 |
| - return a; |
15 |
| - return b; |
| 13 | + if (a < b) |
| 14 | + return a; |
| 15 | + return b; |
16 | 16 | }
|
17 | 17 | void build(ll left, ll right, ll tree[], ll arr[], ll index) //to recursively contruct the segment tree in O(n)
|
18 | 18 | {
|
19 |
| - if (left == right) |
20 |
| - { |
21 |
| - tree[index] = arr[left]; |
22 |
| - return; |
23 |
| - } |
24 |
| - else |
25 |
| - { |
26 |
| - ll mid = (left + right) / 2; |
27 |
| - build(left, mid, tree, arr, 2 * index + 1); //to build the left subtree |
28 |
| - build(mid + 1, right, tree, arr, 2 * index + 2); //to build the right subtree |
29 |
| - tree[index] = min(tree[2 * index + 1], tree[2 * index + 2]); // to build the parent node of left and right subtree. |
30 |
| - return; |
31 |
| - } |
| 19 | + if (left == right) |
| 20 | + { |
| 21 | + tree[index] = arr[left]; |
| 22 | + return; |
| 23 | + } |
| 24 | + else |
| 25 | + { |
| 26 | + ll mid = (left + right) / 2; |
| 27 | + build(left, mid, tree, arr, 2 * index + 1); //to build the left subtree |
| 28 | + build(mid + 1, right, tree, arr, 2 * index + 2); //to build the right subtree |
| 29 | + tree[index] = min(tree[2 * index + 1], tree[2 * index + 2]); // to build the parent node of left and right subtree. |
| 30 | + return; |
| 31 | + } |
32 | 32 | }
|
33 | 33 | void update(ll left, ll right, ll tree[], ll arr[], ll index, ll ind, ll value) //to update a given value at a particular index in O(logn)
|
34 | 34 | {
|
35 |
| - if (right < ind || left > ind) |
36 |
| - return; |
| 35 | + if (right < ind || left > ind) |
| 36 | + return; |
37 | 37 |
|
38 |
| - if (left == right && left == ind) |
39 |
| - { |
40 |
| - tree[index] = value; |
41 |
| - return; |
42 |
| - } |
| 38 | + if (left == right && left == ind) |
| 39 | + { |
| 40 | + tree[index] = value; |
| 41 | + return; |
| 42 | + } |
43 | 43 |
|
44 |
| - ll mid = (left + right) / 2; |
45 |
| - update(left, mid, tree, arr, 2 * index + 1, ind, value); |
46 |
| - update(mid + 1, right, tree, arr, 2 * index + 2, ind, value); |
47 |
| - tree[index] = min(tree[2 * index + 1], tree[2 * index + 2]); |
| 44 | + ll mid = (left + right) / 2; |
| 45 | + update(left, mid, tree, arr, 2 * index + 1, ind, value); |
| 46 | + update(mid + 1, right, tree, arr, 2 * index + 2, ind, value); |
| 47 | + tree[index] = min(tree[2 * index + 1], tree[2 * index + 2]); |
48 | 48 | }
|
49 | 49 | ll ans(ll left, ll right, ll initial, ll final, ll tree[], ll arr[], ll index) // to provide the minimum value within a given range in O(logn)
|
50 | 50 | {
|
51 |
| - if (left <= initial && right >= final) |
52 |
| - return tree[index]; |
53 |
| - if (right < initial || left > final) |
54 |
| - return 1e18; |
| 51 | + if (left <= initial && right >= final) |
| 52 | + return tree[index]; |
| 53 | + if (right < initial || left > final) |
| 54 | + return 1e18; |
55 | 55 |
|
56 |
| - ll mid = (initial + final) / 2; |
57 |
| - return min(ans(left, right, initial, mid, tree, arr, 2 * index + 1), ans(left, right, mid + 1, final, tree, arr, 2 * index + 2)); |
| 56 | + ll mid = (initial + final) / 2; |
| 57 | + return min(ans(left, right, initial, mid, tree, arr, 2 * index + 1), ans(left, right, mid + 1, final, tree, arr, 2 * index + 2)); |
58 | 58 | }
|
59 | 59 | int main()
|
60 | 60 | {
|
61 | 61 |
|
62 |
| - ll tc = 1; |
| 62 | + ll tc = 1; |
63 | 63 |
|
64 |
| - while (tc--) |
65 |
| - { |
66 |
| - ll n; |
| 64 | + while (tc--) |
| 65 | + { |
| 66 | + ll n; |
67 | 67 |
|
68 |
| - ll arr[] = {9, 4, 5, 3, 6, 2, 10}; |
69 |
| - n = 7; |
| 68 | + ll arr[] = {9, 4, 5, 3, 6, 2, 10}; |
| 69 | + n = 7; |
70 | 70 |
|
71 |
| - ll tree[12] = {0}; //to contruct a seg tree in the form of array |
72 |
| - build(0, n - 1, tree, arr, 0); //to construct segment tree in O(n) |
| 71 | + ll tree[12] = {0}; //to contruct a seg tree in the form of array |
| 72 | + build(0, n - 1, tree, arr, 0); //to construct segment tree in O(n) |
73 | 73 |
|
74 |
| - ll l = 1, r = 5; |
75 |
| - printf("the min value in the range 1 to 5 inclusive is:%lld\n", ans(l - 1, r - 1, 0, n - 1, tree, arr, 0)); |
| 74 | + ll l = 1, r = 5; |
| 75 | + printf("the min value in the range 1 to 5 inclusive is:%lld\n", ans(l - 1, r - 1, 0, n - 1, tree, arr, 0)); |
76 | 76 |
|
77 |
| - ll ind = 3, value = -20; |
78 |
| - update(0, n - 1, tree, arr, 0, ind - 1, value); //to update the value at index ind in array,thereby updating whole tree |
| 77 | + ll ind = 3, value = -20; |
| 78 | + update(0, n - 1, tree, arr, 0, ind - 1, value); //to update the value at index ind in array,thereby updating whole tree |
79 | 79 |
|
80 |
| - printf("the new min value in the range 1 to 5 inclusive is:%lld\n", ans(l - 1, r - 1, 0, n - 1, tree, arr, 0)); |
81 |
| - } |
| 80 | + printf("the new min value in the range 1 to 5 inclusive is:%lld\n", ans(l - 1, r - 1, 0, n - 1, tree, arr, 0)); |
| 81 | + } |
82 | 82 | }
|
0 commit comments