Algorithms and Data Structures

  • Profound Academy

    • Status
      • 1
        Implementation
      • 2
        Bitwise operations
      • 3
        Prefix Sums
      • 4
        Sliding window / Two pointers
      • 5
        Modular Arithmetic
      • 6
        Number Theory
      • 7
        Binary Search
      • 8
        Basic Sorting
      • 9
        Greedy Algorithms
      • 10
        Basic Dynamic Programming
      • 11
        Recursion
      • 12
        Linked LIst
      • 13
        Queue & Stack
      • 14
        Binary tree + BST
      • 15
        Divide & Conquer + Advanced Sorting
      • 16
        Heap
      • 17
        Hashing
      • 18
        Graph Representation
      • 19
        BFS

  • Perfect squares

    In the magic universe of square roots (), a number is attractive if it’s a perfect square (1, 4, 9, 16. 25, 36, 49, etc). Perfect squares are the squares of integers (4 is a square of 2, 25 is a square of 5, etc).
    Given n numbers and q queries of the form “How many attractive numbers are there in the range ?”, you are asked to write a program that would answer those questions as quickly as possible.

    Input

    The first line of the input contains two integers - n the number of elements in the array (1 ≤ n ≤ ) and the number of queries q (1 ≤ q ≤ ).
    The next line contains n integers separated by a space, that represent the elements of the array .
    Each of the next q lines contains 2 integers and (0 ≤ < n) representing the query range (inclusive).

    Output

    The program should print q lines, each one having the number of attractive numbers in the range .

    Examples

    Input
    Output
    6 4 9 5 2 4 16 3 0 5 0 1 1 2 2 5
    3 1 0 2
     

    Constraints

    Time limit: 3 seconds

    Memory limit: 512 MB

    Output limit: 5 MB

    To check your solution you need to sign in
    Sign in to continue