forked from matthewsamuel95/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
range min query.cpp
66 lines (59 loc) · 1.18 KB
/
range min query.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
57
58
59
60
61
62
63
64
65
66
#include<bits/stdc++.h>
#define ll long long;
using namespace std;
int s[100];
int sn=INT_MIN;
int build(int v[],int s[],int pos,int start,int end)
{
if(start==end)
{s[pos]=v[start];
cout<<pos<<"#"<<s[pos]<<" ";
if(pos>sn)
sn=pos;
return s[pos];}
// cout<<pos<<" ";
int mid=(start+end)/2;
s[pos]=min(build(v,s,2*pos+1,start,mid),
build(v,s,2*pos+2,mid+1,end));
if(pos>sn)
sn=pos;
cout<<pos<<"#"<<s[pos]<<" ";
return s[pos];
}
int buildtree(int v[],int n)
{
// cout<<v.size()-1;
build(v,s,0,0,n-1);
return 0;
}
void disp(int n)
{
// vector<int>::iterator i;
int i=0;
for(i=0;i!=2*n+5;i++)
// for(i=0;i<sizeof(t)/sizeof(int);i++)
cout<<s[i]<<" ";
}
int rmq(int qlow,int qhigh,int high,int low,int pos)
{
if(qlow<=low&&qhigh>=high)//the query overlaps the segment tree nodes
return s[pos];
if(qlow>high||qhigh<low)//no overlap
return INT_MAX;
int mid=(high+low)/2;
return min(rmq(qlow,qhigh,mid,low,2*pos+1),rmq(qlow,qhigh,high,mid+1,2*pos+2));
}
int main()
{
int n=0,i=0,a=0,v[100],qlow=0,qhigh=0;
cin>>n;
for(i=0;i<n;i++)
cin>>v[i];
buildtree(v,n);
cout<<endl;
disp(n);
cout<<endl;
cin>>qlow>>qhigh;
cout<<rmq(qlow,qhigh,n-1,0,0);
return 0;
}