-
Notifications
You must be signed in to change notification settings - Fork 27
/
segmenttree.cpp
56 lines (54 loc) · 1.22 KB
/
segmenttree.cpp
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
#include <bits/stdc++.h>
using namespace std;
//QUERRY CODE
int querry(int segTree[], int low, int high, int pos, int qh, int ql)
{
if (low >= ql && high <= qh)
{
return segTree[pos];
}
if (low < ql && high > qh)
{
int mid = (low + high) / 2;
int q1 = querry(segTree, low, mid, 2 * pos + 1, qh, ql);
int q2 = querry(segTree, mid + 1, high, 2 * pos + 2, qh, ql);
return min(q1, q2);
}
else
{
return INT_MAX;
}
}
//BUILD TREE
void consTree(int input[], int low, int high, int pos, int segTree[])
{
if (low == high)
{
segTree[pos] = input[low];
}
int mid = (low + high) / 2;
consTree(input, low, mid, 2 * pos + 1, segTree);
consTree(input, mid + 1, high, 2 * pos + 2, segTree);
segTree[pos] = min(segTree[2 * pos + 1], segTree[2 * pos + 2]);
}
//UPDATE CODE
int main()
{
int n;
cin >> n;
int *input = new int[n];
for (int i = 0; i < n; i++)
{
cin >> input[i];
}
int *segTree = new int[2 * n - 1];
int pos = 0;
consTree(input, 0, n - 1, pos, segTree);
for (int i = 0; i < 2 * n - 1; i++)
{
cout << segTree[i];
}
int qh, ql;
cin >> qh >> ql;
return 0;
}