luogu#P4919. Marisa采蘑菇

Marisa采蘑菇

Background

Marisa\text {Marisa} is a girl who loves magic, and the high-firepower and flashy Bagua Furnace is her commonly used weapon. As time goes on, Marisa\text {Marisa} also wants to upgrade the firepower of her Bagua Furnace, so she decides to go to the Magic Forest to pick mushrooms to obtain materials for experiments.

Problem Description

Marisa\text {Marisa} arrives in the forest and sees a row of nn colorful mushrooms. Their colors are a1,a2,,ana_1,a_2,\cdots,a_n. Since she is very picky, she will only pick those “magic mushrooms”.

A mushroom is called a “magic mushroom” if and only if it is within a given interval, and within this given interval, the difference between the number of mushrooms with the same color as it (including itself) and the number of mushrooms of that color outside this given interval is less than or equal to a given constant kk.

Now Marisa\text {Marisa} will make mm queries. In the ii-th query, you need to answer how many different colors of “magic mushrooms” there are in [li,ri][l_i,r_i].

Input Format

The first line contains three integers n,m,kn,m,k.

The second line contains nn positive integers, representing the color aia_i of each mushroom.

Then follow mm lines, each containing two positive integers li,ril_i,r_i, representing the left endpoint and right endpoint of the interval queried by Marisa\text{Marisa}. The data guarantees that 0<lirin0 < l_i \le r_i \le n.

Output Format

Output mm lines. Each line contains an integer xx, representing the number of different colors of “magic mushrooms” in the query interval.

6 3 2
2 3 2 4 1 2
1 2
2 4
1 6
2
3
3

Hint

Sample Explanation:

Let the constant k=2k=2. For the interval [1,2][1,2]:

a1=2a_1=2. Mushrooms of color 22 appear 11 time inside [1,2][1,2], and 22 times outside the interval. The difference is 12=1<2|1-2|=1<2.

a2=3a_2=3. Mushrooms of color 33 appear 11 time inside [1,2][1,2], and 00 times outside the interval. The difference is 10=1<2|1-0|=1<2.

So there are two different colors of magic mushrooms in [1,2][1,2].

Constraints:

For all testdata, 1ai1061\le a_i \le 10^6, 1lirin1 \le l_i \le r_i \le n.

For 20%20\% of the testdata, 1n,m1001 \le n,m \le 100, 0k50 \le k \le 5.

For 50%50\% of the testdata, 1n,m1051 \le n,m \le 10^5, 0k1000 \le k \le 100.

For 100%100\% of the testdata, 1n,m1061 \le n,m \le 10^6, 0k1040 \le k \le 10^4.

ps: Please pay attention to input efficiency.

Translated by ChatGPT 5