Number of Balloons

3.4

10 votes
Advanced Data Structures, Data Structures, Medium, Segment Trees
Problem

You have arranged balloons in a linear order. Every balloon is colored and the ith balloon's color is represented by Ci. Here, the color of balloons is depicted with numbers. 

The number k is not a suitable number, therefore you decide to use it for the less number of times. You do not contain any range of ballons in which a color repeats exactly k times. If the displayed balloons are numbered from b0,b1,b2,...,bn1, then the range of balloons from l to r is bl,bl+1,bl+2,...,br.

You are provided with some specific ranges and your task is to determine the number of colors that get repeated for exactly k times in each range that is provided.

Input format

  • First line: Three integers n, k, and q (1n, k, q100000). Here, n is the number of balloons, k is the inappropriate number, and q is the number of queries.
  • Second line: Contains n integers depicting the color of balloons (1ci100010001000)
  • Each q lines: Contains two integers l and r (0l, r<n)

Output format

Print the number of colors that are repeated exactly k times. If the answer to the previous query is ans (answer for the first query is 0), then the range of the answer should be min((l+ans)%n,(r+ans)%n) and max((l+ans)%n,(r+ans)%n).

.

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

In the first query 0,4 is the range so the numbers in this range are 1,2,1,3,3 ,so 1 and 3 are used exactly 2 time.(because it’s the first query ans=0)

(ans+l)%n=(0+0)%5=0

(ans+r)%n=(0+4)%5=4

In the second query 0,2 is the range so the numbers in this range are 1,2,1 ,so 1 is used exactly 2 time.

(ans+l)%n=(2+0)%5=2

(ans+r)%n=(2+3)%5=0

In the third query 1,3 is the range so the numbers in this range are 2,1,3 so none of them are used exactly 2 time.

(ans+l)%n=(1+2)%5=3

(ans+r)%n=(1+0)%5=1

Editor Image

?