A special sequence

2.5

29 votes
C++, Basic Programming, Bit Manipulation, Basics of Bit Manipulation, Algorithms, Binary Search
Problem

An array A contains integers with the following constraints:

  • A contains elements in sorted order.
  • Integer i occurs i×floor(sqrt(i))+ceil(i/2) times in the array. 
  • All elements are greater than or equal to 1.

You are given Q queries of type:

  • L R: Find the number of distinct values present in subarray A[L...R]

Note: 1-based indexing is followed.

Input format

  • The first line contains an integer Q denoting the number of queries.
  • Next Q lines contains two space-separated integers L R, denoting the query.

Output format

For each query in a new line, print the required number of distinct values.

Constraints

1Q1051LR1013

 

Time Limit: 3
Memory Limit: 256
Source Limit:
Explanation

First few elements of the array A are 1,1,2,2,2,3,3,3,3,3,...

  • For Query 1:-
    • Number of distinct elements in subarray A[1...3] is 2.
  • For Query 2:-
    • Number of distinct elements in subarray A[1...6] is 3.
Editor Image

?