#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 30005
vector<int> v[N];
typedef pair<pair<int, int>,pair<int,int>> pi;
vector<pi> pr;
int tree[4*N]={0};
void update(int node,int st,int end,int x)
{
if(st==end)
{
tree[node]=1;
return;
}
int m=(st+end)/2;
if(st<=x && x<=m)
update(2*node,st,m,x);
else
update(2*node+1,m+1,end,x);
tree[node]=tree[2*node]+tree[2*node+1];
}
int query(int node,int st,int end,int l,int r)
{
if(st>r || end<l)
return 0;
if(st>=l && end<=r)
return tree[node];
int m=(st+end)/2;
int p1=query(2*node,st,m,l,r);
int p2=query(2*node+1,m+1,end,l,r);
return p1+p2;
}
bool comp(pi a,pi b)
{
return a.first.first<b.first.first;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin>>n;
vector<int> arr,a;
for(int i=0;i<n;i++)
{
int x;
cin>>x;
arr.push_back(x);
}
set<int> s;
for(int i=0;i<n;i++)
s.insert(arr[i]);
for(auto it=s.begin();it!=s.end();it++)
{
a.push_back(*it);
}
for(int i=0;i<n;i++)
{
int ind=lower_bound(a.begin(),a.end(),arr[i])-a.begin();
v[ind].push_back(i+1);
}
int q;
cin>>q;
for(int c=0;c<q;c++)
{
int i,j,k;
cin>>i>>j>>k;
pr.push_back(make_pair(make_pair(k,c),make_pair(i,j)));
}
sort(pr.begin(),pr.end(),comp);
int ans[q];
int mx=-1;
for(int i=0;i<q;i++)
{
int l=pr[i].second.first;
int r=pr[i].second.second;
int k=lower_bound(a.begin(),a.end(),pr[i].first.first)-a.begin();
int ind=pr[i].first.second;
if(a[k]>pr[i].first.first)
k--;
while(mx<=k)
{
if(mx>=0)
{
for(auto num:v[mx])
{
update(1,1,n,num);
}
}
mx++;
}
ans[ind]=r-l+1-query(1,1,n,l,r);
}
for(int i=0;i<q;i++)
cout<<ans[i]<<endl;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUg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mVjdG9yPGludD4gYXJyLGE7CiAgICBmb3IoaW50IGk9MDtpPG47aSsrKQogICAgewogICAgICAgIGludCB4OwogICAgICAgIGNpbj4+eDsKICAgICAgICBhcnIucHVzaF9iYWNrKHgpOwogICAgfQoKICAgIHNldDxpbnQ+IHM7CiAgICBmb3IoaW50IGk9MDtpPG47aSsrKQogICAgICAgIHMuaW5zZXJ0KGFycltpXSk7CiAgICAKICAgIGZvcihhdXRvIGl0PXMuYmVnaW4oKTtpdCE9cy5lbmQoKTtpdCsrKQogICAgewogICAgICAgIGEucHVzaF9iYWNrKCppdCk7CiAgICB9CgogICAgZm9yKGludCBpPTA7aTxuO2krKykKICAgIHsKICAgICAgICBpbnQgaW5kPWxvd2VyX2JvdW5kKGEuYmVnaW4oKSxhLmVuZCgpLGFycltpXSktYS5iZWdpbigpOwogICAgICAgIHZbaW5kXS5wdXNoX2JhY2soaSsxKTsKICAgIH0KCiAgICBpbnQgcTsKICAgIGNpbj4+cTsKCiAgICBmb3IoaW50IGM9MDtjPHE7YysrKQogICAgewogICAgICAgIGludCBpLGosazsKICAgICAgICBjaW4+Pmk+Pmo+Pms7CgogICAgICAgIHByLnB1c2hfYmFjayhtYWtlX3BhaXIobWFrZV9wYWlyKGssYyksbWFrZV9wYWlyKGksaikpKTsKICAgIH0KICAgIHNvcnQocHIuYmVnaW4oKSxwci5lbmQoKSxjb21wKTsKCiAgICBpbnQgYW5zW3FdOwogICAgaW50IG14PS0xOwogICAgZm9yKGludCBpPTA7aTxxO2krKykKICAgIHsKICAgICAgICBpbnQgbD1wcltpXS5zZWNvbmQuZmlyc3Q7CiAgICAgICAgaW50IHI9cHJbaV0uc2Vjb25kLnNlY29uZDsKICAgICAgICBpbnQgaz1sb3dlcl9ib3VuZChhLmJlZ2luKCksYS5lbmQoKSxwcltpXS5maXJzdC5maXJzdCktYS5iZWdpbigpOwogICAgICAgIGludCBpbmQ9cHJbaV0uZmlyc3Quc2Vjb25kOwogICAgICAgIGlmKGFba10+cHJbaV0uZmlyc3QuZmlyc3QpCiAgICAgICAgICAgIGstLTsKCiAgICAgICAgd2hpbGUobXg8PWspCiAgICAgICAgewogICAgICAgICAgICBpZihteD49MCkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgZm9yKGF1dG8gbnVtOnZbbXhdKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIHVwZGF0ZSgxLDEsbixudW0pOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIG14Kys7CiAgICAgICAgfQogICAgICAgIGFuc1tpbmRdPXItbCsxLXF1ZXJ5KDEsMSxuLGwscik7CiAgICB9CgogICAgZm9yKGludCBpPTA7aTxxO2krKykKICAgICAgICBjb3V0PDxhbnNbaV08PGVuZGw7Cn0=