#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pii pair<int, int>
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define RREP(i, a, b) for (int i = a; i >= b; i--)
#define endl "\n"
#define all(x) (x).begin(), (x).end()
#define pi 3.141592653589793238
#define maxN 30001
#define INF 1000000000
#define mod 1000000007
#define printd(x) cout << fixed << setprecision(10) << x
// int dx[] = {-2, -1, 1, 2, 2, 1, -1, -2};
// int dy[] = {1, 2, 2, 1, -1, -2, -2, -1};
// int dx[] = {-1, 0, 1, 0, 1, -1, 1, -1};
// int dy[] = {0, -1, 0, 1, -1, -1, 1, 1};
int n, q;
int arr[maxN];
int rev[maxN];
map<int, int> mp;
int timer = 0;
int bs(int k)
{
// index where arr[index] <= k
int start = 1, end = timer;
while (start <= end)
{
int mid = (start + end) / 2;
if (rev[mid] <= k && (mid == end || rev[mid + 1] > k))
return mid;
else if (rev[mid] <= k)
start = mid + 1;
else
end = mid - 1;
}
return 0; // then answer = size of range
}
template <class T>
class PersistentSegmentTree
{
struct Node
{
Node *left, *right;
T val;
Node(Node *l = NULL, Node *r = NULL, T c = 0)
{
left = l, right = r, val = c;
}
};
int n;
vector<Node *> roots;
public:
PersistentSegmentTree() {}
Node *build(int ss, int se)
{
if (ss == se)
{
return new Node();
}
else
{
Node *root = new Node();
int mid = (ss + se) / 2;
root->left = build(ss, mid);
root->right = build(mid + 1, se);
return root;
}
}
PersistentSegmentTree(int N)
{
n = N;
roots.push_back(build(1, n));
}
void _update_(Node *node, Node *prev, int ss, int se, int qi, T val)
{
if (ss == se)
{
node->val += val;
return;
}
int mid = (ss + se) / 2;
if (qi <= mid)
{
node->right = prev->right;
node->left = new Node();
_update_(node->left, prev->left, ss, mid, qi, val);
}
else
{
node->left = prev->left;
node->right = new Node();
_update_(node->right, prev->right, mid + 1, se, qi, val);
}
node->val = node->left->val + node->right->val;
}
void update(int index, T val)
{
Node *r = new Node();
_update_(r, roots.back(), 1, n, index, val);
roots.push_back(r);
}
T _query_(Node *a, Node *b, int ss, int se, int qs, int qe)
{
if (qs > se || qe < ss)
return 0;
if (qs <= ss && qe >= se)
return a->val - b->val;
int mid = (ss + se) / 2;
return _query_(a->left, b->left, ss, mid, qs, qe) + _query_(a->right, b->right, mid + 1, se, qs, qe);
}
T query(int l, int r, int qs)
{
return _query_(roots[r], roots[l - 1], 1, n, qs, timer);
}
};
void solve()
{
cin >> n;
PersistentSegmentTree<ll> segTree(n);
REP(i, 1, n)
{
cin >> arr[i];
mp[arr[i]];
}
for (auto &e : mp)
e.second = ++timer;
REP(i, 1, n)
{
int temp = arr[i];
arr[i] = mp[arr[i]];
rev[arr[i]] = temp;
segTree.update(arr[i], 1);
}
cin >> q;
REP(x, 1, q)
{
int i, j, k;
cin >> i >> j >> k;
k = bs(k);
cout << segTree.query(i, j, k + 1) << endl;
}
}
int main(int argc, char const *argv[])
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
int t = 1;
// cin >> t;
REP(tc, 1, t)
{
// cout<<"Case "<<tc<<":"<<endl;
solve();
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgbGwgbG9uZyBsb25nIGludAojZGVmaW5lIHBpaSBwYWlyPGludCwgaW50PgojZGVmaW5lIFJFUChpLCBhLCBiKSBmb3IgKGludCBpID0gYTsgaSA8PSBiOyBpKyspCiNkZWZpbmUgUlJFUChpLCBhLCBiKSBmb3IgKGludCBpID0gYTsgaSA+PSBiOyBpLS0pCiNkZWZpbmUgZW5kbCAiXG4iCiNkZWZpbmUgYWxsKHgpICh4KS5iZWdpbigpLCAoeCkuZW5kKCkKI2RlZmluZSBwaSAzLjE0MTU5MjY1MzU4OTc5MzIzOAoKI2RlZmluZSBtYXhOIDMwMDAxCiNkZWZpbmUgSU5GIDEwMDAwMDAwMDAKI2RlZmluZSBtb2QgMTAwMD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Ob2RlKE5vZGUgKmwgPSBOVUxMLCBOb2RlICpyID0gTlVMTCwgVCBjID0gMCkKICAgICAgICB7CiAgICAgICAgICAgIGxlZnQgPSBsLCByaWdodCA9IHIsIHZhbCA9IGM7CiAgICAgICAgfQogICAgfTsKCiAgICBpbnQgbjsKICAgIHZlY3RvcjxOb2RlICo+IHJvb3RzOwoKcHVibGljOgogICAgUGVyc2lzdGVudFNlZ21lbnRUcmVlKCkge30KCiAgICBOb2RlICpidWlsZChpbnQgc3MsIGludCBzZSkKICAgIHsKICAgICAgICBpZiAoc3MgPT0gc2UpCiAgICAgICAgewogICAgICAgICAgICByZXR1cm4gbmV3IE5vZGUoKTsKICAgICAgICB9CiAgICAgICAgZWxzZQogICAgICAgIHsKICAgICAgICAgICAgTm9kZSAqcm9vdCA9IG5ldyBOb2RlKCk7CiAgICAgICAgICAgIGludCBtaWQgPSAoc3MgKyBzZSkgLyAyOwogICAgICAgICAgICByb290LT5sZWZ0ID0gYnVpbGQoc3MsIG1pZCk7CiAgICAgICAgICAgIHJvb3QtPnJpZ2h0ID0gYnVpbGQobWlkICsgMSwgc2UpOwogICAgICAgICAgICByZXR1cm4gcm9vdDsKICAgICAgICB9CiAgICB9CgogICAgUGVyc2lzdGVudFNlZ21lbnRUcmVlKGludCBOKQogICAgewogICAgICAgIG4gPSBOOwogICAgICAgIHJvb3RzLnB1c2hfYmFjayhidWlsZCgxLCBuKSk7CiAgICB9CgogICAgdm9pZCBfdXBkYXRlXyhOb2RlICpub2RlLCBOb2RlICpwcmV2LCBpbnQgc3MsIGludCBzZSwgaW50IHFpLCBUIHZhbCkKICAgIHsKICAgICAgICBpZiAoc3MgPT0gc2UpCiAgICAgICAgewogICAgICAgICAgICBub2RlLT52YWwgKz0gdmFsOwogICAgICAgICAgICByZXR1cm47CiAgICAgICAgfQoKICAgICAgICBpbnQgbWlkID0gKHNzICsgc2UpIC8gMjsKCiAgICAgICAgaWYgKHFpIDw9IG1pZCkKICAgICAgICB7CiAgICAg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