Trying to code segment tree for a very basic question called Range Minimum Query 1 from CSES. Basicly I have to find the minimum number from a range of numbers from an array. But I am getting some ambiguous error can’t understand with it means.
#include<iostream>
using namespace std;
const int mxN = 2e5+5;
int arr[mxN], tree[4*mxN];
void build(int s, int e, int idx){
if( s == e){
tree[idx] = arr[s];
return;
}
int mid = s + (e - s)/2;
build(s, mid, 2*idx);
build(mid+1, e, 2*idx+1);
tree[idx] = min(tree[2*idx], tree[2*idx+1]);
}
int query(int s, int e, int qs, int qe, int idx){
//fully overlap
if( qs <= s and e <= qe ){
return tree[idx];
}
//partial overlap
if( qe < s or qs > e ){
return 0;
}
//partial overlap
int mid = s + ( e - s)/2;
int l = query(s, mid, qs, qe, 2*idx);
int r = query(mid+1, e, qs, qe, 2*idx+1);
return min(l ,r);
}
int main(){
int n, m;
cin>>n>>m;
for(int i = 0; i < n; i++)
cin>>arr[i];
build(0, n-1, 1);
while(m--){
int qs, we;
cin>>qs>>qe;
--qs, --qe;
int ans = query(0, n-1, qs, qe, 1);
cout<<ans<<endl;
}
return 0;
}
Source: Windows Questions C++