-
Notifications
You must be signed in to change notification settings - Fork 139
/
SegmentTree(Range Minimum Query).cpp
90 lines (76 loc) · 1.7 KB
/
SegmentTree(Range Minimum 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//SegmentTree(Range Minimum Query):-https://practice.geeksforgeeks.org/problems/range-minimum-query/1
#include<bits/stdc++.h>
using namespace std;
int *constructST(int arr[],int n);
int RMQ(int st[] , int n, int a, int b);
int main()
{
int T;
cin>>T;
while(T--)
{
int N;
cin>>N;
int A[N];
for(int i=0;i<N;i++)
cin>>A[i];
int Q;
cin>>Q;
int *segTree = constructST(A,N);
for(int i=0;i<Q;i++)
{
int start,e;
cin>>start>>e;
if(start>e)
{
swap(start,e);
}
cout<<RMQ(segTree,N,start,e)<<" ";
}
cout<<endl;
}
}
// } Driver Code Ends
void cons(int seg[],int a[],int index,int st,int end){
if(st==end){
seg[index]=a[st];
return;
}
else{
int mid=(st+end)/2;
cons(seg,a,2*index+1,st,mid);
cons(seg,a,2*index+2,mid+1,end);
seg[index]=min(seg[2*index+1],seg[2*index+2]);
}
}
/* The functions which
builds the segment tree */
int *constructST(int arr[],int n)
{
int n1=2*n+2;
int seg[n1]={0};
cons(seg,arr,0,0,n-1);
for(int i=0;i<n1;i++){
cout<<seg[i]<<" ";
}
return seg;
}
int Range(int st[],int s,int e,int index,int l,int r){
if(s>r || e<l)
return INT_MAX;
else if(s>=l && e<=r)
return st[index];
else{
int m=(s+e)/2;
int l1=Range(st,s,m,2*index+1,l,r);
int r1=Range(st,m+1,e,2*index+2,l,r);
return min(l1,r1);
}
}
/* The functions returns the
min element in the range
from a and b */
int RMQ(int st[] , int n, int a, int b)
{
return Range(st,0,n-1,0,a,b);
}