Number of Balloons
Practice
3.4 (10 votes)
Advanced data structures
Data structures
Medium
Segment trees
Problem
89% Success 3212 Attempts 50 Points 2s Time Limit 256MB Memory 1024 KB Max Code

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 (\(0\le l,\ r)

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).

.

Sample Input
5 2 3
1 2 1 3 3
0 4
0 3
2 0
Sample Output
2
1
0
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

Code Editor

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Submissions
Please login to view your submissions
Similar Problems
Points:50
1 votes
Tags:
MediumSegment Trees
Points:50
12 votes
Tags:
Advanced Data StructuresData StructuresSegment Trees
Points:50
1 votes
Tags:
ApprovedData StructuresMediumSegment Trees
Editorial

Login to unlock the editorial