Răspuns:
#include <iostream>
#include <algorithm>
using namespace std;
int q[200001], r[200001];
int main()
{
int n, j, x, y, T, i, st, dr, s, d, m;
cin >> n; cin >> T;
for (i=1; i<=n; ++i) cin >> q[i];
sort(q+1, q+n+1);
for (j=1; j<=T; ++j)
{
cin >> x >> y;
if (x>q[n] || y<q[1]) r[j]=0;
else {
st=1, dr=n;
while (st<dr)
{
m = (st + dr) / 2;
if (q[m] < x) st = m + 1;
else dr = m;
}
s=st;
st=1, dr=n;
while (st<dr)
{
m = (st + dr) / 2;
if (q[m] <= y) st = m + 1;
else dr = m;
}
if ( q[st]>y) d=st-1;
else d=st;
r[j]=d-s+1;
}
}
for (i=1; i<=T; ++i) cout << r[i]<<"\n";
return 0;
}
Explicație: