Tutorial
The maximum possible value in any query is . This means the square root of the maximum is at most 10^6.
Because is up to 10^5, checking ranges individually for each query is too slow. Instead, we can precompute all valid Echoing Squares globally. We iterate a base integer from up to 10^6, calculate V = i^2, and verify the four problem conditions. If satisfies all of them, we append it to a list.
Since we check base integers in increasing order, our list of Echoing Squares will naturally be sorted.
For each of the queries, we simply need to count how many values in our precomputed list fall within . We can achieve this efficiently using binary search (such as lower_bound and upper_bound in C++). The total count for a query is the difference between the upper bound and lower bound iterators.
Complexity Time Complexity: \mathcal{O}(\sqrt{\max R} \cdot \log_{10}(\max R)) for the one-time precomputation, and \mathcal{O}(Q \log K) to answer all queries, where is the total number of valid Echoing Squares found.
Space Complexity: auxiliary space to store the valid numbers.