#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;
class PersistentSegmentTree {
int sz;
struct Node {
int s;
Node *l, *r;
bool ownL, ownR;
Node() : s(0), l(0), r(0), ownL(0), ownR(0) {}
Node(const Node &that) : s(that.s), l(that.l), r(that.r), ownL(0), ownR(0) {}
};
vector<Node *> root;
int query(Node *v, int vl, int vr, int l, int r) const {
if (!v || r < vl || vr < l)
return 0;
if (l <= vl && vr <= r)
return v->s;
int vm = vl + (vr - vl) / 2;
int ql = query(v->l, vl, vm, l, r);
int qr = query(v->r, vm + 1, vr, l, r);
return ql + qr;
}
Node *modify(Node *v, int vl, int vr, int pos, int val) {
Node *nv = v ? new Node(*v) : new Node();
if (vl == vr) {
nv->s += val;
return nv;
}
int vm = vl + (vr - vl) / 2;
if (pos <= vm) {
nv->ownL = 1;
nv->l = modify(nv->l, vl, vm, pos, val);
} else {
nv->ownR = 1;
nv->r = modify(nv->r, vm + 1, vr, pos, val);
}
nv->s = (nv->l ? nv->l->s : 0) + (nv->r ? nv->r->s : 0);
return nv;
}
void destroy(Node *v) {
if (v->ownL)
destroy(v->l);
if (v->ownR)
destroy(v->r);
delete v;
}
public:
PersistentSegmentTree(int size) {
sz = size;
root.push_back(new Node());
}
~PersistentSegmentTree() {
for (int i = 0; i < root.size(); i++)
destroy(root[i]);
}
int size() {
return sz;
}
int query(int version, int l, int r) const {
return query(root[version], 0, sz - 1, l, r);
}
void modify(int pos, int val) {
root.push_back(modify(root.back(), 0, sz - 1, pos, val));
}
};
int main() {
int aSize, aMax = 0;
scanf("%d", &aSize);
vector<int> a(aSize);
for (int i = 0; i < aSize; i++) {
scanf("%d", &a[i]);
aMax = max(aMax, a[i]);
}
PersistentSegmentTree st(aMax + 1);
for (int i = 0; i < aSize; i++)
st.modify(a[i], 1);
int queriesCount;
scanf("%d", &queriesCount);
for (int i = 0; i < queriesCount; i++) {
int l, r, k;
scanf("%d%d%d", &l, &r, &k);
printf("%d\n", st.query(r, k + 1, st.size() - 1) - st.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nYpIDogbmV3IE5vZGUoKTsKICAgICAgICBpZiAodmwgPT0gdnIpIHsKICAgICAgICAgICAgbnYtPnMgKz0gdmFsOwogICAgICAgICAgICByZXR1cm4gbnY7CiAgICAgICAgfQogICAgICAgIGludCB2bSA9IHZsICsgKHZyIC0gdmwpIC8gMjsKICAgICAgICBpZiAocG9zIDw9IHZtKSB7CiAgICAgICAgICAgIG52LT5vd25MID0gMTsKICAgICAgICAgICAgbnYtPmwgPSBtb2RpZnkobnYtPmwsIHZsLCB2bSwgcG9zLCB2YWwpOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIG52LT5vd25SID0gMTsKICAgICAgICAgICAgbnYtPnIgPSBtb2RpZnkobnYtPnIsIHZtICsgMSwgdnIsIHBvcywgdmFsKTsKICAgICAgICB9CiAgICAgICAgbnYtPnMgPSAobnYtPmwgPyBudi0+bC0+cyA6IDApICsgKG52LT5yID8gbnYtPnItPnMgOiAwKTsKICAgICAgICByZXR1cm4gbnY7CiAgICB9CgogICAgdm9pZCBkZXN0cm95KE5vZGUgKnYpIHsKICAgICAgICBpZiAodi0+b3duTCkKICAgICAgICAgICAgZGVzdHJveSh2LT5sKTsKICAgICAgICBpZiAodi0+b3duUikKICAgICAgICAgICAgZGVzdHJveSh2LT5yKTsKICAgICAgICBkZWxldGUgdjsKICAgIH0KCnB1YmxpYzoKCiAgICBQZXJzaXN0ZW50U2VnbWVudFRyZWUoaW50IHNpemUpIHsKICAgICAgICBzeiA9IHNpemU7CiAgICAgICAgcm9vdC5wdXNoX2JhY2sobmV3IE5vZGUoKSk7CiAgICB9CgogICAgflBlcnNpc3RlbnRTZWdtZW50VHJlZSgpIHsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IHJvb3Quc2l6ZSgpOyBpKyspCiAgICAgICAgICAgIGRlc3Ryb3kocm9vdFtpXSk7CiAgICB9CgogICAgaW50IHNpemUoKSB7CiAgICAgICAgcmV0dXJuIHN6OwogICAgfQoKICAgIGludCBxdWVyeSh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