-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRMQSegtree.cpp
More file actions
58 lines (45 loc) · 1.3 KB
/
Copy pathRMQSegtree.cpp
File metadata and controls
58 lines (45 loc) · 1.3 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
// Author: yvdyash
#include <bits/stdc++.h>
using namespace std;
int A[100002]; // Input Array
int tree[2000001];// Array which stores all the nodes of the Segment Tree
void buildST(int node, int start, int end) // start,end - starting and ending index of the array
{
if(start == end)
tree[node] = A[start];
else
{
int mid = (start + end) / 2;
// 2*node will represent the left node and 2*node + 1 will represent the right node
buildST(2*node, start, mid);
buildST(2*node+1, mid+1, end);
// Internal node will have the minimum of both of its children after merging
tree[node] = min(tree[2*node] , tree[2*node+1]);
}
}
int query(int node, int start, int end, int L, int R)
{
if(R < start || end < L)
return 100002;
if(L <= start && end <= R)
return tree[node]; //Required condition
int mid = (start + end) / 2;
int p1 = query(2*node, start, mid, L, R);
int p2 = query(2*node+1, mid+1, end, L, R);
return (min(p1,p2)); //After merging of segments
}
int main()
{
int n,q;
cin>>n>>q;
for(int i=1;i<=n;i++)
cin>>A[i];
buildST(1,1,n);
while(q--)
{
int l,r;
cin>>l>>r;
cout<<query( 1 , 1 , n , l , r )<<"\n";
}
return 0;
}