Solution
= Solution
Precompute the 26 cumulative counts $C[j]=\#\{i:a_i\leq j\}$ using <prefix sums>. Then
$$
\boxed{\#\{i:c<a_i\leq d\}=C[d]-C[c]\quad\text{if }c<d,}
$$
and the answer is zero if $c\geq d$. Counting and prefix summation take $O(n+26)=O(n)$ preprocessing time and constant storage for this fixed alphabet. Each query uses two indexed reads and one subtraction, hence $O(1)$ time. The guard for $c>d$ prevents a negative answer for an empty interval.