int constructSTUtil(int hist[], int ss, int se, int* st, int si) { if (ss == se) return st[si] = ss; int mid = midVal(ss, se); st[si] = minVal(hist, constructSTUtil(hist, ss, mid, st, si*2 + 1), constructSTUtil(hist, mid+1, se, st, si*2 + 2)); return st[si]; }
long constructSTUtil(long *arr,long ss,long se,long *st,long si){ if(ss==se){ st[si]=arr[ss]; return arr[ss]; } long mid=getMid(ss,se); long a=constructSTUtil(arr,ss,mid,st,si*2+1), b=constructSTUtil(arr,mid+1,se,st,si*2+2); st[si]=minVal(a,b); return st[si]; }
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] = minVal(constructSTUtil(arr, ss, mid, st, si*2+1), constructSTUtil(arr, mid+1, se, st, si*2+2)); return st[si]; }
long *constructST(long *arr,long n){ long x=(long)ceil(log2(n)); long max_size=2*(int)pow(2,x)-1; long *st=malloc(sizeof(long)*max_size); constructSTUtil(arr,0,n-1,st,0); return st; }
// A recursive function that constructs Segment Tree for array[ss..se]. // si is index of current node in segment tree st long long int constructSTUtil(long long int arr[], long long int ss, long long int se, long long int *st, long long int si) { // If there is one element in array, store it in current node of // segment tree and return if (ss == se) { st[si] = arr[ss]; return arr[ss]; } // If there are more than one elements, then recur for left and // right subtrees and store the sum of values in this node long long 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]; }
// A recursive function that constructs Segment Tree for array[ss..se]. // si is index of current node in segment tree st int constructSTUtil(int arr[], int ss, int se, int *st, int si) { // If there is one element in array, store it in current node of // segment tree and return if (ss == se) { st[si] = arr[ss]; return arr[ss]; } // If there are more than one elements, then recur for left and // right subtrees and store the minimum of two values in this node int mid = getMid(ss, se); st[si] = minVal(constructSTUtil(arr, ss, mid, st, si*2+1), constructSTUtil(arr, mid+1, se, st, si*2+2)); return st[si]; }
int* constructST(int hist[], int n) { int hi = (int)ceil(log2(n)); int max_size = 2 * (int)pow(2, hi) - 1; int* st = new int[max_size]; constructSTUtil(hist, 0, n - 1, st, 0); return st; }
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; }
/* Function to construct segment tree from given array. This function allocates memory for segment tree and calls constructSTUtil() to fill the allocated memory */ int *constructST(int arr[], int n) { // Allocate memory for segment tree int x = (int)(ceil(log2(n))); //Height of segment tree int max_size = 2*(int)pow(2, x) - 1; //Maximum size of segment tree int *st = new int[max_size]; // Fill the allocated memory st constructSTUtil(arr, 0, n-1, st, 0); // Return the constructed segment tree return st; }
/* Function to construct segment tree from given array. This function allocates memory for segment tree and calls constructSTUtil() to fill the allocated memory */ lint *constructST(lint arr[], lint n) { // Allocate memory for segment tree //Height of segment tree lint max_size = 4*n; //Maximum size of segment tree lint *st = (lint *)malloc(max_size*sizeof(lint)); // Fill the allocated memory st constructSTUtil(arr, 0, n-1, st, 0); // Return the constructed segment tree return st; }
/* Function to construct segment tree from given array. This function allocates memory for segment tree and calls constructSTUtil() to fill the allocated memory */ long long int *constructST(long long int arr[], long long int n) { // Allocate memory for segment tree long long int power[20]={2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576}; long long int x = (long long int)(ceil(log2(n))); //Height of segment tree long long int max_size = 2*(long long int)power[x-1]; //Maximum size of segment tree long long int *st = new long long int[max_size]; // Fill the allocated memory st constructSTUtil(arr, 0, n-1, st, 0); // Return the constructed segment tree return st; }