-
Notifications
You must be signed in to change notification settings - Fork 0
/
RangeMinQuery
70 lines (45 loc) · 1.09 KB
/
RangeMinQuery
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
#include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>
using namespace std;
int rmq(int A[],int vs,int ve,int qs,int qe,int si)
{
if(qs<=vs && qe>=ve)
return A[si];
if(vs>qe || ve<qs)return INT_MAX;
int m =(vs+ve)/2;
return min(rmq(A,vs,m,qs,qe,2*si+1),rmq(A,m+1,ve,qs,qe,2*si+2));
}
int construct (int A[],vector <int> &vec,int vs,int ve,int si)
{
if(ve==vs)
{
A[si]=vec[ve];
return A[si];
}
int m =(vs+ve)/2;
A[si]= min(construct(A,vec,vs,m,2*si+1),construct(A,vec,m+1,ve,2*si+2));
return A[si];
}
int main()
{
vector < int > vec;
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
vec.push_back(4);
vec.push_back(5);
vec.push_back(6);
vec.push_back(7);
vec.push_back(8);
int v = (int)vec.size();
int n = (int) ceil(log2(v));
n = 2*(int)pow(2,n)-1;
int A[n];
int mi= construct(A,vec,0,v-1,0);
// cout <<mi;
int out = rmq(A,0,v-1,3,5,0);
cout <<out;
return 1;
}