Max query-i (segment trees)

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(2
index+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,2
index+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.

provide code using ide link

Link for the code

basically using segment tree you are traversing each element and checking if it is smaller or not … this can be done using normal array traversing also thats why its giving tle… what you can do is make every node of segment tree a policy based data structure… and for every segment check how many elements are samller than k in that… complexity will be log(n)*logn(n) for each query…

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.