#include <bits/stdc++.h>
using namespace std;
struct Query {
int l, r, val;
int ans;
};
const int maxn = 30010;
const int maxq = 200010;
int n, q;
pair<int, int> a[maxn];
Query qr[maxq];
vector<int> val_it[maxn * 2];
vector<Query*> qr_it[maxn * 2];
int main() {
#ifdef LOCAL
freopen("main.inp", "r", stdin);
freopen("main.out", "w", stdout);
#endif
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> a[i].first;
a[i].second = i;
}
cin >> q;
vector<Query*> temp_qr;
for (int i = 0; i < q; ++i) {
cin >> qr[i].l >> qr[i].r >> qr[i].val;
--qr[i].l;
qr[i].ans = 0;
temp_qr.push_back(qr + i);
}
sort(a, a + n, greater<>());
sort(temp_qr.begin(), temp_qr.end(), [](const Query* u, const Query* v) { return u->val > v->val; });
for (int i = 0; i < n; ++i) {
for (int p = a[i].second + n; p > 0; p >>= 1)
val_it[p].push_back(a[i].first);
}
for (auto cur_qr: temp_qr) {
for (int l = cur_qr->l + n, r = cur_qr->r + n; l < r; l >>= 1, r >>= 1) {
if (l & 1) qr_it[l++].push_back(cur_qr);
if (r & 1) qr_it[--r].push_back(cur_qr);
}
}
for (int root = 1; root < 2 * n; ++root) {
auto& vi = val_it[root];
auto& qi = qr_it[root];
if (vi.empty() or qi.empty()) continue;
int ptr_v = 0;
for (auto cur_qr: qi) {
while (ptr_v < (int)vi.size() and vi[ptr_v] > cur_qr->val) ++ ptr_v;
cur_qr->ans += ptr_v;
}
}
for (int i = 0; i < q; ++i) cout << qr[i].ans << '\n';
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKc3RydWN0IFF1ZXJ5IHsKICAgIGludCBsLCByLCB2YWw7CiAgICBpbnQgYW5zOwp9OwoKY29uc3QgaW50IG1heG4gPSAzMDAxMDsKY29uc3QgaW50IG1heHEgPSAyMDAwMTA7CmludCBuLCBxOwpwYWlyPGludC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dLnB1c2hfYmFjayhhW2ldLmZpcnN0KTsKICAgIH0KICAgIAogICAgZm9yIChhdXRvIGN1cl9xcjogdGVtcF9xcikgewogICAgICAgIGZvciAoaW50IGwgPSBjdXJfcXItPmwgKyBuLCByID0gY3VyX3FyLT5yICsgbjsgbCA8IHI7IGwgPj49IDEsIHIgPj49IDEpIHsKICAgICAgICAgICAgaWYgKGwgJiAxKSBxcl9pdFtsKytdLnB1c2hfYmFjayhjdXJfcXIpOwogICAgICAgICAgICBpZiAociAmIDEpIHFyX2l0Wy0tcl0ucHVzaF9iYWNrKGN1cl9xcik7CiAgICAgICAgfQogICAgfQogICAgCiAgICBmb3IgKGludCByb290ID0gMTsgcm9vdCA8IDIgKiBuOyArK3Jvb3QpIHsKICAgICAgICBhdXRvJiB2aSA9IHZhbF9pdFtyb290XTsKICAgICAgICBhdXRvJiBxaSA9IHFyX2l0W3Jvb3RdOwogICAgICAgIGlmICh2aS5lbXB0eSgpIG9yIHFpLmVtcHR5KCkpIGNvbnRpbnVlOwogICAgICAgIGludCBwdHJfdiA9IDA7CiAgICAgICAgZm9yIChhdXRvIGN1cl9xcjogcWkpIHsKICAgICAgICAgICAgd2hpbGUgKHB0cl92IDwgKGludCl2aS5zaXplKCkgYW5kIHZpW3B0cl92XSA+IGN1cl9xci0+dmFsKSArKyBwdHJfdjsKICAgICAgICAgICAgY3VyX3FyLT5hbnMgKz0gcHRyX3Y7CiAgICAgICAgfQogICAgfQogICAgCiAgICBmb3IgKGludCBpID0gMDsgaSA8IHE7ICsraSkgY291dCA8PCBxcltpXS5hbnMgPDwgJ1xuJzsKICAgIAogICAgcmV0dXJuIDA7Cn0K