-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSegment_Tree.cpp
More file actions
78 lines (56 loc) · 1.23 KB
/
Segment_Tree.cpp
File metadata and controls
78 lines (56 loc) · 1.23 KB
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
#include <bits/stdc++.h>
using namespace std;
int left(int x) { return x<<1; }
int right(int x){ return (x<<1)+1; }
class SegmentTree
{
vector<int> array, tree;
public:
void init(vector<int> A)
{
int N = A.size();
array = A;
tree.assign(4 * N, 0);
build(1, 0, N-1);
}
int query(int i, int j)
{
int R = ((int)array.size())-1;
return query(1, i - 1, j - 1, 0, R);
}
private:
int comparable(int vA, int vB)
{
return min(vA, vB);
}
void build(int p, int L, int R)
{
if(L == R){ tree[p] = array[L]; }
else
{
int mid = (L+R) / 2;
build( left(p), L, mid);
build(right(p), mid+1, R);
int valueLeft = tree[left(p)],
valueRight = tree[right(p)];
tree[p] = comparable(valueLeft, valueRight);
}
}
int query(int p, int i, int j, int L, int R)
{
if(j < L || i > R) return INT_MAX;
if(L >= i && R <= j) return tree[p];
int mid = (L+R) / 2;
int valueLeft = query( left(p), i, j, L, mid),
valueRight = query(right(p), i, j, mid+1, R);
return comparable(valueLeft, valueRight);
}
};
int main()
{
SegmentTree segment_tree;
vector<int> numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
segment_tree.init(numbers);
cout << segment_tree.query(5, numbers.size());
return 0;
}