-
Notifications
You must be signed in to change notification settings - Fork 12
/
SegmentTree.c
91 lines (67 loc) · 2.39 KB
/
SegmentTree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//We can find minimum element between a range of query efficiently by segment tree. It preprocess and give results in logn time. It stores minimum value in a range at its nodes.
//*tree -> pointer to the segment tree , qstart -> start of range , qend -> end of range , start -> start index of node , end -> end index of node
#include <stdio.h>
#include <math.h>
//Function to get minimum of two numbers
int min(int x, int y) { return (x < y)? x: y; }
//Get the middle index
int getMid(int l, int r) { return l + (r-l)/2; }
int rangequeryiUtil(int *tree, int start, int end, int qstart, int qend, int index)
{
// If segment of this node is a part of range, then return the min of the segment
if (qstart <= start && qend >= end)
return tree[index];
// Outside the given range
if (end<qstart || start>qend)
return INT_MAX;
// If a range overlaps
int mid = getMid(start, end);
return min(rangequeryUtil(tree, start, mid, qstart, qend, 2*index+1),rangequeryUtil(tree, mid+1, end, qstart, qend, 2*index+2));
}
int rangequery(int *tree, int n, int qstart, int qend)
{
if (qstart < 0 || qend > n-1 || qstart > qend)
{
printf("Invalid");
return -1;
}
return rangequeryUtil(st, 0, n-1, qs, qe, 0);
}
// sindex is index of current node in segment tree
int construct(int a[], int start, int end, int *tree, int sindex)
{
if (start == end)
{
tree[sindex] = a[start];
return a[start];
}
// If more than one elements
int mid = getMid(start, end);
tree[sindex] = min(construct(a, start, mid, tree, sindex*2+1),constructSTUtil(a, mid+1, end, tree, sindex*2+2));
return tree[sindex];
}
//Function to construct segment tree from given array
int *constructTree(int a[], int n)
{
//Height of segment tree
int h = (int)(ceil(log2(n)));
// Maximum size of segment tree
int maxsize = 2*(int)pow(2, h) - 1;
int *tree = new int[max_size];
//Allocate memory to tree
construct(a, 0, n-1, tree, 0);
// Return the constructed segment tree
return tree;
}
// Driver program to test above functions
int main()
{
int a[] = {3,1,5,7,2};
int n = sizeof(arr)/sizeof(arr[0]);
// Build segment tree from given array
int *tree = construct(a, n);
int qstart = 2; // Starting index of query range
int qend = 4; // Ending index of query range
printf("Minimum of values in range given range is = "rangequery(tree, n, qstart, qend));
return 0;
}