#include <bits/stdc++.h>
using namespace std;
#define maxn 200010
#define gc getchar_unlocked
#define ll long long
//fast I/O
void scanint(int &x)
{
register int c = gc();
x = 0;
bool neg=false;
if(c=='-')
neg=true;
for(;(c<48 || c>57);c = gc());
for(;c>47 && c<58;c = gc()) {x = (x<<1) + (x<<3) + c - 48;}
if(neg)
x=-x;
}
struct node{
int cnt;
node *left;
node *right;
node(){
this->cnt=0;
this->left=this;
this->right=this;
}
node(int cnt, node *left, node *right):cnt(cnt), left(left), right(right){}
node *insert(int L, int R, int val);
//cnt stores no. of elements, left points to left child and right points to right child
};
node *null; //Base node
node *tree[maxn]; //persistent segment tree
node *node::insert(int L, int R, int val)
{
if(L==R)
return new node(this->cnt+1, null, null); //insert no. in node
else{
int mid = (L+R)>>1;
if(val<=mid) //val lies in left child, update left
return new node(this->cnt+1, this->left->insert(L, mid, val), this->right);
else // val lies in right child, update right
return new node(this->cnt+1, this->left, this->right->insert(mid+1, R, val));
}
}
int query(node *low, node *high, int L, int R, int val) // returns all numbers in given segment that are less than or equal to val
{
if(L==R)
return (high->cnt - low->cnt); //can't divide further, return all elements in segment
int mid = (L+R)>>1;
int lsum = high->left->cnt - low->left->cnt; // total elements in the left child of segment
if(val<=mid)
return query(low->left, high->left, L, mid, val); //val lies in left half, call query on left
else
return lsum+query(low->right, high->right, mid+1, R, val); //val lies in right half, add all elements in left half and call query on right half
}
int main()
{
int n, m, i, j, l, r, k;
ll curinv, ans, w, x, y, z, q1, q2;
scanint(n);//no. of elements
map<int, int> m1;//for coordinate compression
int arr[n+10], sor[n+10];//arr[] stores values, sor[] stores sorted order of values
tree[0] = new node();
for(i=0; i<n; i++){
scanint(arr[i]);
m1[arr[i]];
}
map<int, int>::iterator it;
int ind=1;
for(it=m1.begin(); it!=m1.end(); ++it){
m1[it->first] = ind; // coordinate compression and sorting
sor[ind] = it->first;
ind++;
}
/*for(i=1; i<=n; i++)
cout << sor[i] << " ";
cout << endl;
for(i=0; i<n; i++)
cout << m1[arr[i]] << " ";
cout << endl;*/
for(i=1; i<=n; i++)
tree[i] = tree[i-1]->insert(0, ind, m1[arr[i-1]]); // inserting val in tree node
scanint(m); // no. of queries
while(m--){
scanint(l); // left index of query
scanint(r); // right index
scanint(k); //
/* if(l>r){
int temp = l;
l = r;
r = temp;
}*/
int pos = upper_bound(sor, sor+n+1, k)-(sor); // deciding rank of k in current arr[], kind of doing coordinate compression on k
//cout << pos << endl;
k = m1[sor[pos-1]]; // new k after compression
// cout << k << endl;
// cout << query(tree[l-1], tree[r], 0, ind, k) << endl;
ans=(r-l+1-query(tree[l-1], tree[r], 0, ind, k)); //no. of elements greater = total elemnts-no. of elements less than or equal to k
printf("%d\n", ans);
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKI2RlZmluZSBtYXhuIDIwMDAxMAojZGVmaW5lIGdjIGdldGNoYXJfdW5sb2NrZWQKI2RlZmluZSBsbCBsb25nIGxvbmcKLy9mYXN0IEkvTwp2b2lkIHNjYW5pbnQoaW50ICZ4KQp7CiAgICByZWdpc3RlciBpbnQgYyA9IGdjKCk7CiAgICB4ID0gMDsKICAgIGJvb2wgbmVnPWZhbHNlOwogICAgaWYoYz09Jy0nKQogICAgICAgIG5lZz10cnVlOwogICAgZm9yKDsoYzw0OCB8fCBjPjU3KTtjID0gZ2MoKSk7CiAgICBmb3IoO2M+NDcgJiYgYzw1ODtjID0gZ2MoKSkge3ggPSAoeDw8MSkgKyAoeDw8MykgKyBjIC0gNDg7fQogICAgaWYobmVnKQogICAgICAgIHg9LXg7Cn0KCnN0cnVjdCBub2RlewogICAgaW50IGNudDsKICAgIG5vZGUgKmxlZnQ7CiAgICBub2RlICpyaWdodDsKICAgIG5vZGUoKXsKICAgICAgICB0aGlzLT5jbnQ9MDsKICAgICAgICB0aGlzLT5sZWZ0PXRoaXM7CiAgICAgICAgdGhpcy0+cmlnaHQ9dGhpczsKICAgIH0KICAgIG5vZGUo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0ZWQgb3JkZXIgb2YgdmFsdWVzCiAgICB0cmVlWzBdID0gbmV3IG5vZGUoKTsKICAgIGZvcihpPTA7IGk8bjsgaSsrKXsKICAgICAgICBzY2FuaW50KGFycltpXSk7CiAgICAgICAgbTFbYXJyW2ldXTsKICAgIH0KICAgIG1hcDxpbnQsIGludD46Oml0ZXJhdG9yIGl0OwogICAgaW50IGluZD0xOwogICAgZm9yKGl0PW0xLmJlZ2luKCk7IGl0IT1tMS5lbmQoKTsgKytpdCl7CiAgICAgICAgbTFbaXQtPmZpcnN0XSA9IGluZDsgICAgICAgICAgICAgICAgLy8gY29vcmRpbmF0ZSBjb21wcmVzc2lvbiBhbmQgc29ydGluZwogICAgICAgIHNvcltpbmRdID0gaXQtPmZpcnN0OwogICAgICAgIGluZCsrOwogICAgfQogICAgLypmb3IoaT0xOyBpPD1uOyBpKyspCiAgICAgICAgY291dCA8PCBzb3JbaV0gPDwgIiAiOwogICAgY291dCA8PCBlbmRsOwogICAgZm9yKGk9MDsgaTxuOyBpKyspCiAgICAgICAgY291dCA8PCBtMVthcnJbaV1dIDw8ICIgIjsKICAgIGNvdXQgPDwgZW5kbDsqLwogICAgZm9yKGk9MTsgaTw9bjsgaSsrKQogICAgICAgIHRyZWVbaV0gPSB0cmVlW2ktMV0tPmluc2VydCgwLCBpbmQsIG0xW2FycltpLTFdXSk7IC8vIGluc2VydGluZyB2YWwgaW4gdHJlZSBub2RlCiAgICBzY2FuaW50KG0pOyAvLyBuby4gb2YgcXVlcmllcwogICAgd2hpbGUobS0tKXsKICAgICAgICBzY2FuaW50KGwpOyAvLyBsZWZ0IGluZGV4IG9mIHF1ZXJ5CiAgICAgICAgc2NhbmludChyKTsgLy8gcmlnaHQgaW5kZXgKICAgICAgICBzY2FuaW50KGspOyAvLwogICAgICAgLyogaWYobD5yKXsKICAgICAgICAgICAgaW50IHRlbXAgPSBsOwogICAgICAgICAgICBsID0gcjsKICAgICAgICAgICAgciA9IHRlbXA7CiAgICAgICAgfSovCiAgICAgICAgaW50IHBvcyA9IHVwcGVyX2JvdW5kKHNvciwgc29yK24rMSwgayktKHNvcik7IC8vIGRlY2lkaW5nIHJhbmsgb2YgayBpbiBjdXJyZW50IGFycltdLCBraW5kIG9mIGRvaW5nIGNvb3JkaW5hdGUgY29tcHJlc3Npb24gb24gawogICAgICAgIC8vY291dCA8PCBwb3MgPDwgZW5kbDsKICAgICAgICBrID0gbTFbc29yW3Bvcy0xXV07IC8vIG5ldyBrIGFmdGVyIGNvbXByZXNzaW9uCiAgICAgICAvLyBjb3V0IDw8IGsgPDwgZW5kbDsKICAgICAgIC8vIGNvdXQgPDwgcXVlcnkodHJlZVtsLTFdLCB0cmVlW3JdLCAwLCBpbmQsIGspIDw8IGVuZGw7CiAgICAgICAgYW5zPShyLWwrMS1xdWVyeSh0cmVlW2wtMV0sIHRyZWVbcl0sIDAsIGluZCwgaykpOyAvL25vLiBvZiBlbGVtZW50cyBncmVhdGVyID0gdG90YWwgZWxlbW50cy1uby4gb2YgZWxlbWVudHMgbGVzcyB0aGFuIG9yIGVxdWFsIHRvIGsKICAgICAgICBwcmludGYoIiVkXG4iLCBhbnMpOwogICAgfQogICAgcmV0dXJuIDA7Cn0K