TLE error for the below code.
#include
#define ll long long int
using namespace std;
ll arr[100005];
ll tree[400021];
ll queryMin(ll index, ll low, ll high, ll qlow, ll qhigh, ll val){
ll k=0;
if(qlow>high || qhigh<low){
return k;
}
if(low==high){
if(tree[index]>=val){
k++;
}
return k;
}
ll mid=(low+high)>>1;
ll leftAns = queryMin(2index,low,mid,qlow,qhigh,val);
k+=leftAns;
ll rightAns = queryMin(2index+1,mid+1,high,qlow,qhigh,val);
k+=rightAns;
return k;
}
void buildTree(ll low, ll high, ll index){
if(low==high){
tree[index]=arr[low];
return ;
}
ll mid = (low+high)>>1;
buildTree(low,mid,2index);
buildTree(mid+1,high,2index+1);
tree[index]=min(tree[2index],tree[2index+1]);
return ;
}
int main() {
ll n,q;
cin>>n;
for(ll i=1;i<=n;i++){
cin>>arr[i];
}
cin>>q;
buildTree(1,n,1);
ll a,b,c;
while (q–){
cin>>a>>b>>c;
cout<<queryMin(1,1,n,a,b,c)<<endl;
}
}
Would like to know the reason behind this error and would also like to know better approach to solve this question.