forked from LiGhT-27/Kodes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsegment_tree.cpp
More file actions
83 lines (68 loc) · 1.86 KB
/
segment_tree.cpp
File metadata and controls
83 lines (68 loc) · 1.86 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
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
#include<bits/stdc++.h>
//#include<boost/multiprecision/cpp_ll.hpp>
//using boost::multiprecision::cpp_ll; // use cpp_ll as data type for large numbers
using namespace std;
#define ll long long
#define pb push_back
#define fast() ios_base::sync_with_stdio(false); cin.tie(NULL);
#define mod 1000000007
#define mp make_pair
#define ff first
#define ss second
#define vi vector<ll>
#define vll vector<long long ll>
#define log2(x) (63ll - __builtin_clzll(x))
#define test ll t; cin>>t; while(t--)
#define modInv(n) po(n,mod-2)%mod
#define ncr(n,r) (((fact[n]*modInv(fact[r]))%mod)*modInv(fact[n-r]))%mod
void buildtree(int *tree,int *a,int index,int s,int e){
///Base Case
if(s==e){
tree[index] = a[s];
return;
}
if(s>e){
return;
}
///Recursive Case
int mid = (s+e)/2;
buildtree(tree,a,2*index,s,mid);
buildtree(tree,a,2*index+1,mid+1,e);
tree[index] = min(tree[2*index],tree[2*index+1]);
return;
}
int query(int *tree,int index,int s,int e,int qs,int qe){
///No Overlap
if(qs>e || qe<s){
return INT_MAX;
}
///Complete Overlap
if(qs<=s && qe>=e){
return tree[index];
}
///Partial Overlap
int mid = (s+e)/2;
int leftAns = query(tree,2*index,s,mid,qs,qe);
int rightAns = query(tree,2*index+1,mid+1,e,qs,qe);
return min(leftAns,rightAns);
}
int main()
{
fast()
//test
{
int n,q;
cin>>n>>q;
int a[n];
for(int i=0; i<n; i++) cin>>a[i];
int *tree=new int[4*n+1];
buildtree(tree,a,1,0,n-1);
while(q--)
{
int l,r;
cin>>l>>r;
cout<<query(tree,1,0,n-1,l-1,r-1)<<"\n";
}
}
return 0;
}