|
| 1 | +#include<bits/stdc++.h> |
| 2 | +using namespace std; |
| 3 | +//Sum of a given segment; |
| 4 | +int getSumUtil(int *st, int ss, int se, int qs, int qe, int si) |
| 5 | +{ |
| 6 | + //starting query is less than starting index of the seg and ending query is greater than |
| 7 | + //ending index of the segment, then return the value store at that index; |
| 8 | + if(qs <= ss && qe >= se) |
| 9 | + return st[si]; |
| 10 | + if(qs > se || qe < ss) |
| 11 | + return 0; |
| 12 | + int mid = (ss + se) / 2; |
| 13 | + return getSumUtil(st, ss, mid, qs, qe, 2*si+1) + getSumUtil(st, mid+1, se, qs, qe, 2*si+2); |
| 14 | +} |
| 15 | +void updateValueUtil(int *st, int ss, int se, int i, int diff, int si) |
| 16 | +{ |
| 17 | + if(i < ss || i > se) |
| 18 | + return; |
| 19 | + st[si] = st[si] + diff; |
| 20 | + if(ss != se) |
| 21 | + { |
| 22 | + int mid = (ss + se) / 2; |
| 23 | + //Break the segment tree into two parts and compute for each part to get updated segment tree; |
| 24 | + updateValueUtil(st, ss, mid, i, diff, 2*si+1); |
| 25 | + updateValueUtil(st, mid+1, se, i, diff, 2*si+2); |
| 26 | + } |
| 27 | +} |
| 28 | +//Update the value at a particular index in the array; |
| 29 | +void updateValue(int arr[], int *st, int n, int i, int val) |
| 30 | +{ |
| 31 | + int diff = val - arr[i]; |
| 32 | + arr[i] = val; |
| 33 | + updateValueUtil(st, 0, n-1, i, diff, 0); |
| 34 | +} |
| 35 | +//Calculate sum of a given segment in the array; |
| 36 | +int getSum(int* st, int n, int qs, int qe) |
| 37 | +{ |
| 38 | + //If starting query is less than 0 or ending query is greater than length of the array |
| 39 | + //or starting query is greater than ending query the program will be terminated; |
| 40 | + if(qs < 0 || qe > n-1 || qe < qs) |
| 41 | + return -1; |
| 42 | + return getSumUtil(st, 0, n-1, qs, qe, 0); |
| 43 | +} |
| 44 | +//Computer power in log(n) time complexity; |
| 45 | +int pown(int a, int b) |
| 46 | +{ |
| 47 | + if(b == 0) |
| 48 | + return 1; |
| 49 | + if(b == 1) |
| 50 | + return a; |
| 51 | + return pown(a, b/2) * pown(a, b - b/2); |
| 52 | +} |
| 53 | +int constructSTUtil(int arr[], int ss, int se, int *st, int si) |
| 54 | +{ |
| 55 | + //if function goes to leaf node then data value will be value store in that segment in the given array; |
| 56 | + if(ss == se) |
| 57 | + { |
| 58 | + st[si] = arr[se]; |
| 59 | + return arr[ss]; |
| 60 | + } |
| 61 | + int mid = (ss + se) / 2; |
| 62 | + //Data value store in a node is the sum of data value store it's two child; |
| 63 | + st[si] = constructSTUtil(arr, ss, mid, st, 2*si+1) + constructSTUtil(arr, mid+1, se, st, 2*si+2); |
| 64 | + return st[si]; |
| 65 | +} |
| 66 | +//Construct the segment tree; |
| 67 | +int *constructST(int arr[], int n) |
| 68 | +{ |
| 69 | + int x = (int)(ceil(log2(n))); |
| 70 | + //Total no of leaf node in the segment tree; |
| 71 | + int max_size = 2 * pown(2, x) - 1; |
| 72 | + int *st = new int[max_size]; |
| 73 | + //Construct the segment tree where data value store in a node is the sum of data value store it's two child; |
| 74 | + int val = constructSTUtil(arr, 0, n-1, st, 0); |
| 75 | + for(int i = 0; i < max_size; i++) |
| 76 | + cout << st[i] << " "; |
| 77 | + cout << endl; |
| 78 | + return st; |
| 79 | +} |
| 80 | +int main() |
| 81 | +{ |
| 82 | + int n; |
| 83 | + cin>>n; |
| 84 | + int arr[n+5]; |
| 85 | + for(int i = 0; i < n; i++) |
| 86 | + cin>>arr[i]; |
| 87 | + //Construct the Segment Tree; |
| 88 | + int *st = constructST(arr,n); |
| 89 | + //Get the sum of a given range; |
| 90 | + cout << getSum(st, n, 1, 3) << endl; |
| 91 | + //Update the value of a particular index in the given array; |
| 92 | + updateValue(arr, st, n, 1, 10); |
| 93 | + //Get the sum of a given range; |
| 94 | + cout << getSum(st, n, 1, 3) << endl; |
| 95 | + return 0; |
| 96 | +} |
0 commit comments