#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;
class SegmentTreeFC {
int sz;
vector< vector<int> > t, tl, tr;
void build(int v, int vl, int vr, const vector<int> &a) {
if (vl == vr) {
t[v].push_back(a[vl]);
return;
}
int vm = vl + (vr - vl) / 2;
build(2 * v + 1, vl, vm, a);
build(2 * v + 2, vm + 1, vr, a);
int l = 2 * v + 1, r = 2 * v + 2;
int ls = t[l].size(), rs = t[r].size(), ts = ls + rs;
t[v].resize(ts);
tl[v].resize(ts + 1);
tr[v].resize(ts + 1);
tl[v][ts] = ls;
tr[v][ts] = rs;
for (int i = ts - 1, li = ls - 1, ri = rs - 1; i >= 0; i--) {
if (li < 0)
t[v][i] = t[r][ri--];
else if (ri < 0)
t[v][i] = t[l][li--];
else if (t[l][li] > t[r][ri])
t[v][i] = t[l][li--];
else
t[v][i] = t[r][ri--];
tl[v][i] = (li >= 0 && t[l][li] >= t[v][i] ? li : li + 1);
tr[v][i] = (ri >= 0 && t[r][ri] >= t[v][i] ? ri : ri + 1);
}
}
int query(int v, int vl, int vr, int l, int r, int k, int ub) const {
if (ub == -1)
ub = upper_bound(t[v].begin(), t[v].end(), k) - t[v].begin();
if (r < vl || vr < l)
return 0;
if (l <= vl && vr <= r)
return vr - vl + 1 - ub;
int vm = vl + (vr - vl) / 2;
int ql = query(2 * v + 1, vl, vm, l, r, k, tl[v][ub]);
int qr = query(2 * v + 2, vm + 1, vr, l, r, k, tr[v][ub]);
return ql + qr;
}
public:
SegmentTreeFC(const vector<int> &a) {
sz = a.size();
t.resize(4 * sz);
tl.resize(4 * sz);
tr.resize(4 * sz);
build(0, 0, sz - 1, a);
}
int query(int l, int r, int k) const {
return query(0, 0, sz - 1, l, r, k, -1);
}
};
int main() {
int aSize;
scanf("%d", &aSize);
vector<int> a(aSize);
for (int i = 0; i < aSize; i++)
scanf("%d", &a[i]);
SegmentTreeFC st(a);
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(l - 1, r - 1, k));
}
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxhbGdvcml0aG0+CiNpbmNsdWRlIDx2ZWN0b3I+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgoKY2xhc3MgU2VnbWVudFRyZWVGQyB7CgogICAgaW50IHN6OwogICAgdmVjdG9yPCB2ZWN0b3I8aW50PiA+IHQsIHRsLCB0cjsKCiAgICB2b2lkIGJ1aWxkKGludCB2LCBpbnQgdmwsIGludCB2ciwgY29uc3QgdmVjdG9yPGludD4gJmEpIHsKICAgICAgICBpZiAodmwgPT0gdnIpIHsKICAgICAgICAgICAgdFt2XS5wdXNoX2JhY2soYVt2bF0pOwogICAgICAgICAgICByZXR1cm47CiAgICAgICAgfQogICAgICAgIGludCB2bSA9IHZsICsgKHZyIC0gdmwpIC8gMjsKICAgICAgICBidWlsZCgyICogdiArIDEsIHZsLCB2bSwgYSk7CiAgICAgICAgYnVpbGQoMiAqIHYgKyAyLCB2bSArIDEsIHZyLCBhKTsKCiAgICAgICAgaW50IGwgPSAyICogdiArIDEsIHIgPSAyICogdiArIDI7CiAgICAgICAgaW50IGxzID0gdFtsXS5zaXplKCksIHJzID0gdFtyXS5zaXplKCksIHRzID0gbHMgKyByczsKICAgICAgICB0W3ZdLnJlc2l6ZSh0cyk7CiAgICAgICAgdGxbdl0ucmVzaXplKHRzICsgMSk7CiAgICAgICAgdHJbdl0ucmVzaXplKHRzICsgMSk7CiAgICAgICAgdGxbdl1bdHNdID0gbHM7CiAgICAgICAgdHJbdl1bdHNdID0gcnM7CiAgICAgICAgZm9yIChpbnQgaSA9IHRzIC0gMSwgbGkgPSBscyAtIDEsIHJpID0gcnMgLSAxOyBpID49IDA7IGktLSkgewogICAgICAgICAgICBpZiAobGkgPCAwKQogICAgICAgICAgICAgICAgdFt2XVtpXSA9IHRbcl1bcmktLV07CiAgICAgICAgICAgIGVsc2UgaWYgKHJpIDwgMCkKICAgICAgICAgICAgICAgIHRbdl1baV0gPSB0W2xdW2xpLS1dOwogICAgICAgICAgICBlbHNlIGlmICh0W2xdW2xpXSA+IHRbcl1bcmldKQogICAgICAgICAgICAgICAgdFt2XVtpXSA9IHRbbF1bbGktLV07CiAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgIHRbdl1baV0gPSB0W3JdW3JpLS1dOwogICAgICAgICAgICB0bFt2XVtpXSA9IChsaSA+PSAwICYmIHRbbF1bbGldID49IHRbdl1baV0gPyBsaSA6IGxpICsgMSk7CiAgICAgICAgICAgIHRyW3ZdW2ldID0gKHJpID49IDAgJiYgdFtyXVtyaV0gPj0gdFt2XVtpXSA/IHJpIDogcmkgKyAxKTsKICAgICAgICB9CiAgICB9CgogICAgaW50IHF1ZXJ5KGludCB2LCBpbnQgdmwsIGludCB2ciwgaW50IGwsIGludCByLCBpbnQgaywgaW50IHViKSBjb25zdCB7CiAgICAgICAgaWYgKHViID09IC0xKQogICAgICAgICAgICB1YiA9IHVwcGVyX2JvdW5kKHRbdl0uYmVnaW4oKSwgdFt2XS5lbmQoKSwgaykgLSB0W3ZdLmJlZ2luKCk7CiAgICAgICAgaWYgKHIgPCB2bCB8fCB2ciA8IGwpCiAgICAgICAgICAgIHJldHVybiAwOwogICAgICAgIGlmIChsIDw9IHZsICYmIHZyIDw9IHIpCiAgICAgICAgICAgIHJldHVybiB2ciAtIHZsICsgMSAtIHViOwogICAgICAgIGludCB2bSA9IHZsICsgKHZyIC0gdmwpIC8gMjsKICAgICAgICBpbnQgcWwgPSBxdWVyeSgyICogdiArIDEsIHZsLCB2bSwgbCwgciwgaywgdGxbdl1bdWJdKTsKICAgICAgICBpbnQgcXIgPSBxdWVyeSgyICogdiArIDIsIHZtICsgMSwgdnIsIGwsIHIsIGssIHRyW3ZdW3ViXSk7CiAgICAgICAgcmV0dXJuIHFsICsgcXI7CiAgICB9CgpwdWJsaWM6CgogICAgU2VnbWVudFRyZWVGQyhjb25zd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