We are asked to solve a Range Count Query problem. Given an array of N numbers (up to 100k) and Q queries (up to 100k). Each query asks for the number of elements in the array that are between l and r inclusive (i.e., l <= a_i <= r). Classic problem: offline sorting + Fenwick tree or BIT or use sorting and binary search with prefix sums after coordinate compression. Since queries are offline and we have high constraints but N and Q are 1e5, we can compress the values (a_i and l, r) or just sort the array and answer each query using binary search: count = upper_bound(r) - lower_bound(l). That would be O((N+Q) log N). Since N and Q are both 1e5, this is efficient. But note that values go up to 1e9, but that's fine because we can sort array and use binary search.
So the simplest solution: read N, Q; read array a; sort a; for each query read l, r; find left = lower_bound(a.begin(), a.end(), l) - a.begin(); find right = upper_bound(a.begin(), a.end(), r) - a.begin(); answer = right - left; output.
But wait: The sample output says "Four" for second query? Actually sample output given in problem: "3\nFour\n0\n" but likely it's "4"? The description says "Output example: 3\nFour\n0\n". That is probably a translation error; it should be "4". So we output numbers.
Edge cases: duplicates are fine.
Thus solution is straightforward.
But let me double-check constraints: N, Q up to 1e5, values up to 1e9. So sorting O(N log N) and each query O(log N) works.
We'll implement in C++ for speed.
Potential issues: large input, so use fast IO: ios::sync_with_stdio(false); cin.tie(NULL);. And use vector for array.
Implementation steps:
- Read N, Q.
- Read array a (size N).
- Sort a.
- For each query i=0..Q-1:
- Read l, r.
- int left = lower_bound(a.begin(), a.end(), l) - a.begin();
- int right = upper_bound(a.begin(), a.end(), r) - a.begin();
- int ans = right - left;
- Print ans on its own line.
That's it.
But the problem statement says: "On line i, output the answer to the i-th query, that is, the number of j such as l_i \ leq a_j \ leq r_i." So yes.
Let me just verify sample:
N=6, a = [8,6,9,1,2,1], sorted => [1,1,2,6,8,9]
Query1: l=2, r=8 => lower_bound(2) index=2 (first >=2), upper_bound(8) index=5 (first >8) => count=3 (2,6,8? Actually values 2,6,8? 8 is included because <=8. Yes 2,6,8: 3 numbers)
Query2: l=1, r=7 => lower_bound(1)=0, upper_bound(7)=4 (first >7 is 8 at index4) => count=4 (1,1,2,6) => 4.
Query3: l=3, r=5 => lower_bound(3)=3 (first >=3 is 6 at index3), upper_bound(5)=3 (first >5 is 6 at index3) => count=0.
Matches.
Thus solution is trivial.
We must be careful: The problem might be designed to teach Fenwick or segment tree, but binary search is enough.
But sometimes if we have updates, we'd need BIT, but here no updates.
Thus answer: use sorted array and binary search.
Potential pitfalls: Using int for N and Q up to 1e5, but indices can be up to N, int is fine. Values up to 1e9, fits in 32-bit signed int? 1e9 is fine for int (max ~2e9). But safer to use long long for values, but not needed.
We'll use vector a.
Time complexity: O(N log N + Q log N).
Memory: O(N).
Thus final code:
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, Q;
cin >> N >> Q;
vector<int> a(N);
for (int i = 0; i < N; ++i) cin >> a[i];
sort(a.begin(), a.end());
for (int i = 0; i < Q; ++i) {
int l, r;
cin >> l >> r;
auto left = lower_bound(a.begin(), a.end(), l) - a.begin();
auto right = upper_bound(a.begin(), a.end(), r) - a.begin();
cout << right - left << "\n";
}
return 0;
}
Alternatively, we could compress and use prefix sums, but not needed.
Therefore answer is ready. ```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, Q;
cin >> N >> Q;
vector<int> a(N);
for (int i = 0; i < N; ++i) cin >> a[i];
sort(a.begin(), a.end());
for (int i = 0; i < Q; ++i) {
int l, r;
cin >> l >> r;
auto left = lower_bound(a.begin(), a.end(), l) - a.begin();
auto right = upper_bound(a.begin(), a.end(), r) - a.begin();
cout << right - left << '\n';
}
return 0;
}