|
| 1 | +#include<bits/stdc++.h> |
| 2 | +using namespace std; |
| 3 | +int max_size; |
| 4 | + |
| 5 | + |
| 6 | +// st, si, ss and se are same as getSumUtil() |
| 7 | +// i --> index of the element to be updated |
| 8 | +// diff --> Value to be added to all nodes which have i in range |
| 9 | + |
| 10 | +void updateValueUtil(int *st, int ss, int se, int i, int diff, int si){ |
| 11 | + // Base Case: If the input index lies outside the range of this segment |
| 12 | + if (i < ss || i > se) |
| 13 | + return; |
| 14 | + |
| 15 | + // If the input index is in range of this node, then update the value of the node and its children |
| 16 | + st[si] = st[si] + diff; |
| 17 | + if (se != ss){ |
| 18 | + int mid = ss + (se - ss)/2; |
| 19 | + updateValueUtil(st, ss, mid, i, diff, 2*si + 1); |
| 20 | + updateValueUtil(st, mid+1, se, i, diff, 2*si + 2); |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +void updateValue(int arr[], int *st, int n, int i, int new_val){ |
| 27 | + // Check for erroneous input index |
| 28 | + if (i < 0 || i > n-1){ |
| 29 | + cout<<"Invalid Input"; |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + // Get the difference between new value and old value |
| 34 | + int diff = new_val - arr[i]; |
| 35 | + |
| 36 | + // Update the value in array |
| 37 | + arr[i] = new_val; |
| 38 | + |
| 39 | + // Update the values of nodes in segment tree |
| 40 | + updateValueUtil(st, 0, n-1, i, diff, 0); |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +// st --> Pointer to segment tree |
| 47 | +// si --> Index of current node in the segment tree. |
| 48 | +// Initially 0 is passed as root is always at index 0 |
| 49 | +// ss & se --> Starting and ending indexes of the segment represented by current node, i.e., st[si] |
| 50 | +// qs & qe --> Starting and ending indexes of query range |
| 51 | + |
| 52 | +int getSumUtil(int *st, int ss, int se, int qs, int qe, int si){ |
| 53 | + // If segment of this node is a part of given range, then return the sum of the segment ( Total Overlap ) |
| 54 | + if (qs <= ss && qe >= se) |
| 55 | + return st[si]; |
| 56 | + |
| 57 | + // If segment of this node is outside the given range ( No Overlap ) |
| 58 | + if (qe < ss || qs > se) |
| 59 | + return 0; |
| 60 | + |
| 61 | + // If a part of this segment overlaps with the given range |
| 62 | + int mid = ss + (se - ss)/2; |
| 63 | + |
| 64 | + return getSumUtil(st, ss, mid, qs, qe, 2*si+1) + getSumUtil(st, mid+1, se, qs, qe, 2*si+2); |
| 65 | +} |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | +int getSum(int *st, int n, int qs, int qe){ |
| 70 | + // Check for erroneous input values |
| 71 | + if (qs < 0 || qe > n-1 || qs > qe){ |
| 72 | + cout<<"Invalid Input"; |
| 73 | + return -1; |
| 74 | + } |
| 75 | + return getSumUtil(st, 0, n-1, qs, qe, 0); |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +int constructSTUtil(int arr[], int ss, int se, int *st, int si){ // arr[ss......se] |
| 81 | + |
| 82 | + // If there is one element in array, store it in current node of segment tree and return |
| 83 | + if (ss == se){ |
| 84 | + st[si] = arr[ss]; |
| 85 | + return arr[ss]; |
| 86 | + } |
| 87 | + |
| 88 | + // If there are more than one elements, then recur for left and right subtrees and store the sum of values in this node |
| 89 | + int mid = ss + (se - ss)/2; |
| 90 | + |
| 91 | + st[si] = constructSTUtil(arr, ss, mid, st, 2*si+1) + constructSTUtil(arr, mid+1, se, st, 2*si+2); |
| 92 | + |
| 93 | + return st[si]; |
| 94 | +} |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | +int *constructST(int arr[], int n){ |
| 99 | + int x = (int)(ceil(log2(n))); // height of tree |
| 100 | + max_size = 2*(int)pow(2, x) - 1; // total number of nodes are 2n-1 but we also need space for dummy nodes |
| 101 | + int *st = new int[max_size]; |
| 102 | + constructSTUtil(arr, 0, n-1, st, 0); |
| 103 | + return st; |
| 104 | +} |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | +int main(){ |
| 110 | + int arr[] = {1, 3, 5, 7, 9, 11}; |
| 111 | + int n = sizeof(arr)/sizeof(arr[0]); |
| 112 | + |
| 113 | + int *st = constructST(arr, n); |
| 114 | + |
| 115 | + cout<<getSum(st, n, 1, 3)<<endl; // 15 |
| 116 | + updateValue(arr, st, n, 1, 10); //arr[1]=10 |
| 117 | + cout<<getSum(st, n, 1, 3)<<endl; // 22 |
| 118 | + |
| 119 | + return 0; |
| 120 | +} |
0 commit comments