-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSegment Tree - Range Min query (pass pointer comment imp.).cpp
79 lines (72 loc) · 1.53 KB
/
Segment Tree - Range Min query (pass pointer comment imp.).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
#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+1,e+1)<<" ";
}
cout<<endl;
}
}
int constructing(int st, int nd, int arr[], int tr[], int pos)
{
if(st==nd)
{
tr[pos] = arr[st];
return tr[pos];
}
int mid = (st+nd)/2;
tr[pos] = min(constructing(st,mid,arr,tr,pos*2),constructing(mid+1,nd,arr,tr,(pos*2)+1));
return tr[pos];
}
int *constructST(int arr[],int n)
{
int st = 0;
int nd = n-1;
int *tr = new int[(4*n)+2]; //way of returning pointer dynamic size
int x = constructing(st,nd,arr,tr,1);
return tr;
}
int findMin(int st[],int ss,int se,int qs,int qe,int pos)
{
int mid = (ss+se)/2;
if(ss>=qs && qe>=se)
{
return st[pos];
}
else if(se<qs || qe<ss)
{
return INT_MAX;
}
else
{
return min(findMin(st,ss,mid,qs,qe,pos*2),findMin(st,mid+1,se,qs,qe,(pos*2)+1));
}
}
int RMQ(int st[], int n, int a, int b)
{
int ss = 1;
int se = n;
return findMin(st,ss,se,a,b,1);
}