#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef tree<int,null_type,less<int>,rb_tree_tag,
tree_order_statistics_node_update> indexed_set;
typedef long long ll;
#define pb push_back
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define P pair
#define V vector
const long long MOD = 1000000007; // 1e9 + 7
void file(){
freopen("input.txt.txt","r",stdin);
freopen("output.txt.txt","w",stdout);
}
void setio(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
struct segtree{
private:
struct node{
ll sum;
};
node neutral={0LL};
node single(ll x){
return {x};
}
node merge(node a,node b){
return{a.sum+b.sum};
};
public:
int size;
V<node>query;
void init(int n){
size=1;
while(size<n)size*=2;
query.assign(2*size,neutral);
}
void build(V<ll>&a,int x,int lx,int rx){
if(rx-lx==1){
if(lx<a.size())
query[x]=single(a[lx]);
return;
}
int m=(lx+rx)/2;
build(a,2*x+1,lx,m);
build(a,2*x+2,m,rx);
query[x]=merge(query[2*x+1],query[2*x+2]);
}
void build(V<ll>a){
build(a,0,0,size);
}
void set(ll i,ll v,int x,int lx,int rx){
if(rx-lx==1){
query[x]=single(v);
return;
}
int m=(lx+rx)/2;
if(i<m){
set(i,v,2*x+1,lx,m);
}
else
set(i,v,2*x+2,m,rx);
query[x]=merge(query[2*x+1],query[2*x+2]);
}
void set(ll i,ll v){
set(i,v,0,0,size);
}
node calc(ll l,ll r,int x,int lx,int rx){
if(l<=lx && rx<=r)
return query[x];
if(lx>=r || rx<=l)
return neutral;
int m=(lx+rx)/2;
node a=calc(l,r,2*x+1,lx,m);
node b=calc(l,r,2*x+2,m,rx);
return merge(a,b);
}
ll calc(ll l,ll r){
return calc(l,r,0,0,size).sum;
}
};
struct querie{
ll f;
ll s;
ll k;
ll index;
};
bool maker1(querie a,querie b){
return a.f<=b.f;
}
bool maker2(querie a,querie b){
return a.s<=b.s;
}
void solve() {
ll n;
cin>>n;
ll A[n];
V<ll>compressor;
map<ll,ll>mp;
for(int i=0;i<n;i++) {
cin >> A[i];
compressor.pb(A[i]);
}
ll q;cin>>q;
querie Q[q];
for(int i=0;i<q;i++){
cin>>Q[i].f>>Q[i].s>>Q[i].k;
Q[i].f--,Q[i].s--;
compressor.pb(Q[i].k);
Q[i].index=i;
}
//start coordinate compression
sort(all(compressor));
auto it=unique(all(compressor));
compressor.resize(distance(compressor.begin(),it));
for(int i=0;i<compressor.size();i++){
mp[compressor[i]]=i;
}
for(int i=0;i<n;i++)
A[i]=mp[A[i]];
for(int i=0;i<q;i++)
Q[i].k=mp[Q[i].k];
//finish coordinate compression
//start left part computation
segtree tree;
tree.init(3e6);
P<ll,ll>ans[q];
V<ll>a(3e6);
for(int i=0;i<n;i++){
a[A[i]]++;
}
tree.build(a);
sort(Q,Q+q, maker1);
int j=0;
for(int i=0;i<n;i++){
while(Q[j].f==i && j<q){
ans[Q[j].index].F=tree.calc(Q[j].k+1,3e6);
j++;
}
a[A[i]]--;
tree.set(A[i],a[A[i]]);
}
for(int i=0;i<n;i++){
a[A[i]]++;
}
tree.build(a);
sort(Q,Q+q, maker2);
j=0;
for(int i=0;i<n;i++){
a[A[i]]--;
tree.set(A[i],a[A[i]]);
while(Q[j].s==i && j<q){
ans[Q[j].index].S=tree.calc(Q[j].k+1,3e6);
j++;
}
}
for(int i=0;i<q;i++){
cout<<ans[i].F-ans[i].S<<endl;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
//file();
solve();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNpbmNsdWRlIDxleHQvcGJfZHMvYXNzb2NfY29udGFpbmVyLmhwcD4KdXNpbmcgbmFtZXNwYWNlIF9fZ251X3BiZHM7CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0eXBlZGVmIHRyZWU8aW50LG51bGxfdHlwZSxsZXNzPGludD4scmJfdHJlZV90YWcsCiAgICAgICAgdHJlZV9vcmRlcl9z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l7CiAgICAgICAgaWYocngtbHg9PTEpewogICAgICAgICAgICBpZihseDxhLnNpemUoKSkKICAgICAgICAgICAgICAgIHF1ZXJ5W3hdPXNpbmdsZShhW2x4XSk7CiAgICAgICAgICAgIHJldHVybjsKICAgICAgICB9CiAgICAgICAgaW50IG09KGx4K3J4KS8yOwogICAgICAgIGJ1aWxkKGEsMip4KzEsbHgsbSk7CiAgICAgICAgYnVpbGQoYSwyKngrMixtLHJ4KTsKICAgICAgICBxdWVyeVt4XT1tZXJnZShxdWVyeVsyKngrMV0scXVlcnlbMip4KzJdKTsKICAgIH0KICAgIHZvaWQgYnVpbGQoVjxsbD5hKXsKICAgICAgICBidWlsZChhLDAsMCxzaXplKTsKICAgIH0KICAgIHZvaWQgc2V0KGxsIGksbGwgdixpbnQgeCxpbnQgbHgsaW50IHJ4KXsKICAgICAgICBpZihyeC1seD09MSl7CiAgICAgICAgICAgIHF1ZXJ5W3hdPXNpbmdsZSh2KTsKICAgICAgICAgICAgcmV0dXJuOwogICAgICAgIH0KICAgICAgICBpbnQgbT0obHgrcngpLzI7CiAgICAgICAgaWYoaTxtKXsKICAgICAgICAgICAgc2V0KGksdiwyKngrMSxseCxtKTsKICAgICAgICB9CiAgICAgICAgZWxzZQogICAgICAgICAgICBzZXQoaSx2LDIqeCsyLG0scngpOwogICAgICAgIHF1ZXJ5W3hdPW1lcmdlKHF1ZXJ5WzIqeCsxXSxxdWVyeVsyKngrMl0pOwogICAgfQogICAgdm9pZCBzZXQobGwgaSxsbCB2KXsKICAgICAgICBzZXQoaSx2LDAsMCxzaXplKTsKICAgIH0KICAgIG5vZGUgIGNhbGMobGwgbCxsbCByLGludCB4LGludCBseCxpbnQgcngpewogICAgICAgIGlmKGw8PWx4ICYmIHJ4PD1yKQogICAgICAgICAgICByZXR1cm4gcXVlcnlbeF07CiAgICAgICAgaWYobHg+PXIgfHwgcng8PWwpCiAgICAgICAgICAgIHJldHVybiBuZXV0cmFsOwogICAgICAgIGludCBtPShseCtye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aW4oKSxpdCkpOwogICBmb3IoaW50IGk9MDtpPGNvbXByZXNzb3Iuc2l6ZSgpO2krKyl7CiAgICAgICBtcFtjb21wcmVzc29yW2ldXT1pOwogICB9CiAgIGZvcihpbnQgaT0wO2k8bjtpKyspCiAgICAgICBBW2ldPW1wW0FbaV1dOwogICBmb3IoaW50IGk9MDtpPHE7aSsrKQogICAgICAgUVtpXS5rPW1wW1FbaV0ua107CiAgIC8vZmluaXNoIGNvb3JkaW5hdGUgY29tcHJlc3Npb24KICAgLy9zdGFydCBsZWZ0IHBhcnQgY29tcHV0YXRpb24KICAgIHNlZ3RyZWUgdHJlZTsKICAgIHRyZWUuaW5pdCgzZTYpOwogICAgUDxsbCxsbD5hbnNbcV07CiAgICBWPGxsPmEoM2U2KTsKICAgIGZvcihpbnQgaT0wO2k8bjtpKyspewogICAgICAgIGFbQVtpXV0rKzsKICAgIH0KICAgIHRyZWUuYnVpbGQoYSk7CiAgICBzb3J0KFEsUStxLCBtYWtlcjEpOwogICAgaW50IGo9MDsKICAgIGZvcihpbnQgaT0wO2k8bjtpKyspewogICAgICAgIHdoaWxlKFFbal0uZj09aSAmJiBqPHEpewogICAgICAgICAgICBhbnNbUVtqXS5pbmRleF0uRj10cmVlLmNhbGMoUVtqXS5rKzEsM2U2KTsKICAgICAgICAgICAgaisrOwogICAgICAgIH0KICAgICAgICBhW0FbaV1dLS07CiAgICAgICAgdHJlZS5zZXQoQVtpXSxhW0FbaV1dKTsKICAgIH0KICAgIGZvcihpbnQgaT0wO2k8bjtpKyspewogICAgICAgIGFbQVtpXV0rKzsKICAgIH0KICAgIHRyZWUuYnVpbGQoYSk7CiAgICBzb3J0KFEsUStxLCBtYWtlcjIpOwogICAgaj0wOwogICAgZm9yKGludCBpPTA7aTxuO2krKyl7CiAgICAgICAgYVtBW2ldXS0tOwogICAgICAgIHRyZWUuc2V0KEFbaV0sYVtBW2ldXSk7CiAgICAgICAgd2hpbGUoUVtqXS5zPT1pICAmJiBqPHEpewogICAgICAgICAgICBhbnNbUVtqXS5pbmRleF0uUz10cmVlLmNhbGMoUVtqXS5rKzEsM2U2KTsKICAgICAgICAgICAgaisrOwogICAgICAgIH0KICAgIH0KICAgIGZvcihpbnQgaT0wO2k8cTtpKyspewogICAgICAgIGNvdXQ8PGFuc1tpXS5GLWFuc1tpXS5TPDxlbmRsOwogICAgfQp9CgoKaW50IG1haW4oKSB7CiAgICBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTsKICAgIGNpbi50aWUoTlVMTCk7CiAgICBjb3V0LnRpZShOVUxMKTsKICAgIC8vZmlsZSgpOwogICAgc29sdmUoKTsKICAgIHJldHVybiAwOwp9