Sigsegv error is coming after submitting the following code,What is wrong with my code?
#include<iostream>
using namespace std;
#define inf 0x7fffffff
int arr[50000]; //WHY WE DEFINE IT GLOBALLLY
int segtree[50000]; //WHY WE DEFINE IT GLOBALLY
void constructtree(int low,int high,int pos);
int rangemaxquery(int qlow,int qhigh,int low,int high,int pos);
int main()
{
int m,xi,yi,y;
long long int n,i;
cin>>n;
for(i=0;i<n;i++)
{
cin>>arr[i];
}
cin>>m;
constructtree(0,i,0);
while(m--)
{
cin>>xi>>yi;
y=rangemaxquery(xi,yi,0,i,0);
cout<<y<<"\n";
}
return 0;
}
void constructtree(int low,int high,int pos)
{
if (low==high)
{
segtree[pos]=arr[low];
return ;
}
int mid=(low+high)/2;
constructtree(low,mid,2*pos+1);
constructtree(mid+1,high,2*pos+2);
segtree[pos]=max(segtree[2*pos+1],segtree[2*pos+2]);
}
int rangemaxquery(int qlow,int qhigh,int low,int high,int pos)
{
if(qlow<=low&&qhigh>=high)
return segtree[pos];
if(qlow>high||qhigh<low)
return -inf;//don't know
int mid=(low+high)/2;
return max(rangemaxquery(qlow,qhigh,low,mid,2*pos+1),rangemaxquery(qlow,qhigh,mid+1,high,2*pos+2));
}